0
votes

I have a int representation of a color. What I want to do is if this color is too black or too white, I want to set background to a default color. I want to find if the color is too close to black or white using RGB or HSV representation is fine. By too black or too white, I mean to say I can set some threshold depending on the scale. Say there is a scale from 0 to 1 where 0 is black and 1 is white, I want to reject all the colors below 0.2 and above 0.8. I tried the below code but it actually does not work. This does not work since I see some other colors also get rejected which are not shades of black or white. Please help.

public static boolean isWhitish(int color) {
    float[] hsv = new float[3];
    Color.colorToHSV(color, hsv);
    return hsv[2] > 0.8;
}
1
It isn't very clear what you are trying to do. Please clarify and give more information, including what you gave tried.CaptJak
What do you define as 'too close'?user4756884

1 Answers

0
votes

First you will want to get the HEX CODE of your color, then you can use this solution to extract the brightness of that color.

You can then use your (brightness < .2 || brightness > .8) to decide what to do next.

Hope this helps, good luck :-)