c# - What kind of colors blend to make the forecolor distinct from the backcolor? -


this question has answer here:

suppose know backcolor , want change forecolor accordingly make text distinct background. example, if current text color (forecolor) red, change of user, backcolor becomes red or close red (which make text illegible), in such case, want change forecolor using colors blending formula make distinct backcolor , maintain legibility.

hope can help, thanks.

based on information on this blog entry, come following c# code, returns either color.black or color.white best contrast given (background) color:

public color getcontrastingcolor(color backcolor) {     int r = (int)backcolor.r;     int g = (int)backcolor.g;     int b = (int)backcolor.b;      int yiqspace = ((r * 299) + (g * 587) + (b * 114)) / 1000;      if (yiqspace > 131)     {         return color.black;     }     else     {         return color.white;     } } 

the method uses yiq determine if color considered light or dark, , returns constrasting color in return.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -