Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid/Java: Determining if text color will blend in with the background?
    primarykey
    data
    text
    <p>I'm introducing "tagging" functionality in my application, and one of the ways I allow tags to be displayed is to set the text to the color the user has selected for each. My application has three themes with backgrounds that are white, black and a notepad-like brown (these could change/grow in the future). I want to be able to display the tag in its native color if it easily contrasts the background, and just use the default text color for each theme otherwise.</p> <p>I've written a helper function to help me determine if the text will be masked, but its not 100% correct (I want it to determine if colors will be masked based on all three of the hsv components, and right now the saturation comparison isn't valid). The code is below.</p> <pre><code> public static boolean colorWillBeMasked(int color, Application app){ float[] hsv = new float[3]; Color.colorToHSV(color, hsv); //note 0, black 1, white 2 int theme = app.api.getThemeView(); System.out.println("h=" +hsv[0]+ ", s=" +hsv[1]+ ", v=" +hsv[2]+", theme="+theme); if(android.R.color.transparent == color) return true; // color is dark if(hsv[2] &lt;= .2){ if(theme == 1) return true; } // color is light else if(hsv[2] &gt;= .8) { if(theme == 2) return true; } return false; } </code></pre> <p>When calling this function with blue, red, transparent, black, yellow and green the output is as follows (respectively):</p> <ul> <li>h=0.0, s=1.0, v=1.0, theme=1</li> <li>h=229.41177, s=1.0, v=1.0, theme=1</li> <li>h=267.6923, s=1.0, v=0.050980393, theme=1</li> <li>h=0.0, s=0.0, v=0.0, theme=1</li> <li>h=59.52941, s=1.0, v=1.0, theme=1</li> <li>h=111.29411, s=1.0, v=1.0, theme=1</li> </ul> <p>My question is: based on hue, saturation and value, how can you determine if text that is colored a certain way will show up on a white background vs a black background or if it will be masked? Please take my algorithm and improve it or help me create a new one.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload