0

I am working with RGB, and I am having problems with this math. Unless I am reading the quote below wrong, I need to take the result of this which is a value that looks like 0.01234 and invert it. bgc = bottom color and fgc = top color. I am having issues inverting the result. The way it is now I get a Color Dodge effect and not a Color Burn effect. I have tried multiplying, dividing, adding, subtracting, and nothing seems to be working. What can I do to invert the result?

(255f - (float)bgc[0]) / (fgc[0] << 8)

This is the full method:

public static int colorBurn(int bg, int fg){
    int[] bgc = Colors.getrgba(bg);
    int[] fgc = Colors.getrgba(fg);
    int r = (bgc[0] == 255 ? bgc[0] : (int)Math.min(0, (255f - (float)bgc[0]) / (fgc[0] << 8)));
    int g = (bgc[1] == 255 ? bgc[1] : (int)Math.min(0, (255f - (float)bgc[1]) / (fgc[1] << 8)));
    int b = (bgc[2] == 255 ? bgc[2] : (int)Math.min(0, (255f - (float)bgc[2]) / (fgc[2] << 8)));
    return Colors.rgba(r, g, b);
}

Here is what Wikipedia says to do:

The Color Burn mode divides the inverted bottom layer by the top layer, and then inverts the result. This darkens the top layer increasing the contrast to reflect the color of the bottom layer. The darker the bottom layer, the more its color is used. Blending with white produces no difference.

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338

1 Answers1

1

I got it:

public static int colorBurn(int bg, int fg){
    int[] bgc = Colors.getrgba(bg);
    int[] fgc = Colors.getrgba(fg);
    int r = (int)Math.min(255, 255 * (1 - (1 - (bgc[0] / 255f)) / (fgc[0] / 255f)));
    int g = (int)Math.min(255, 255 * (1 - (1 - (bgc[1] / 255f)) / (fgc[1] / 255f)));
    int b = (int)Math.min(255, 255 * (1 - (1 - (bgc[2] / 255f)) / (fgc[2] / 255f)));
    r = r < 0 ? 0 : r;
    g = g < 0 ? 0 : g;
    b = b < 0 ? 0 : b;
    return Colors.rgba(r, g, b);
}
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338