mike_marquet
asked on
Get a correct text color from a back color !
How can I get a correct text color from a back color so that the text is also visible on a dark back color ?
Did you try to XOR your text color with the back color ?
ASKER
If I do this, the text color becomes the same as the back color.
COLORREF clrText = RGB(0,0,0); // default text color is black
COLORREF clrBack = RGB(0,0,0); // background color is black
=> text color must becomes white
clrText = clrText ^ clrBack => black ?
COLORREF clrText = RGB(0,0,0); // default text color is black
COLORREF clrBack = RGB(0,0,0); // background color is black
=> text color must becomes white
clrText = clrText ^ clrBack => black ?
Even in a professional program if you draw white color line to a white background you wont notice it.
But if you want to see it black
clrText = !(clrText ^ clrBack)
But if you want to see it black
clrText = !(clrText ^ clrBack)
ASKER
It doens't works. The text color stays back on a dark background color.
But I have found a solution using the luminance value :
double GetLuminance(COLORREF clr)
{
return 0.3 * GetRValue(clr) + 0.59 * GetGValue(clr) + 0.11 * GetBValue(clr);
}
COLORREF clrText = GetLuminance() < 120.0 ? RGB(255,255,255) : RGB(0,0,0);
But I have found a solution using the luminance value :
double GetLuminance(COLORREF clr)
{
return 0.3 * GetRValue(clr) + 0.59 * GetGValue(clr) + 0.11 * GetBValue(clr);
}
COLORREF clrText = GetLuminance() < 120.0 ? RGB(255,255,255) : RGB(0,0,0);
Sounds to be a good one but I am not sure if 120 will always work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.