RGB question..
-
Hello, I have a GUI application where the user can set the background color for a plotting control. My question is how to select a foreground(text) color that will "contrast" with the user selected background?
COLORREF bg = GetUserPlotColor();
COLORREF fg = F(bg);That is, what is the code for the
F()
body? TIA.-- **Ricky Marek** (_AKA: rbid_)
-- "Things are only impossible until they are not" --- Jean-Luc Picard My articles -
Hello, I have a GUI application where the user can set the background color for a plotting control. My question is how to select a foreground(text) color that will "contrast" with the user selected background?
COLORREF bg = GetUserPlotColor();
COLORREF fg = F(bg);That is, what is the code for the
F()
body? TIA.-- **Ricky Marek** (_AKA: rbid_)
-- "Things are only impossible until they are not" --- Jean-Luc Picard My articlesI'm no colour expert, but I did encounter this while writing a custom-drawn tree control a while back. Have a look at this[^] article that should point you in the right direction. However, in the end I think I settled for a simple XOR technique, where I XOR'ed my selection colour with white (#FFFFFF), to get the corresponding contrasting text colour. This is definitely the easy route, and will probably give unsatisfactory results on certain colours, but it works for me. By the way, I found this[^] little web applet useful.
I Dream of Absolute Zero
-
Hello, I have a GUI application where the user can set the background color for a plotting control. My question is how to select a foreground(text) color that will "contrast" with the user selected background?
COLORREF bg = GetUserPlotColor();
COLORREF fg = F(bg);That is, what is the code for the
F()
body? TIA.-- **Ricky Marek** (_AKA: rbid_)
-- "Things are only impossible until they are not" --- Jean-Luc Picard My articlesThis is an old function that generated a suitable shadow color for drawing 3D elements. If you reverese it, it should do what you want:
COLORREF MakeShadowColor(COLORREF rgbColor)
{
COLORREF rgbRetval;// common color conversions...
if (rgbColor == RGB(255,255,128)) // lyellow
{
rgbRetval = RGB(128,128,0);
}
else if (rgbColor == RGB(192,192,192)) // lgrey
{
rgbRetval = RGB(128,128,128);
}
else if (rgbColor == RGB(128,255,255)) // lcyan
{
rgbRetval = RGB(0,0,255);
}
else if (rgbColor == RGB(0,0,0)) // black
{
rgbRetval = RGB(128,128,128);
}
else
{
short iR = (GetRValue(rgbColor)+1)/2;
short iG = (GetGValue(rgbColor)+1)/2;
short iB = (GetBValue(rgbColor)+1)/2;rgbRetval = RGB(iR,iG,iB); }
return rgbRetval;
}onwards and upwards...
-
I'm no colour expert, but I did encounter this while writing a custom-drawn tree control a while back. Have a look at this[^] article that should point you in the right direction. However, in the end I think I settled for a simple XOR technique, where I XOR'ed my selection colour with white (#FFFFFF), to get the corresponding contrasting text colour. This is definitely the easy route, and will probably give unsatisfactory results on certain colours, but it works for me. By the way, I found this[^] little web applet useful.
I Dream of Absolute Zero
I have played around a bit with inverting frame-grabbed images, and I found that the XOR technique usually works nicely. Where it will not work well is with grey-scales which are close to mid-range, for example, if you have RGB-15 (5-bits of each colour, high order bit unused), where the R, G, and B values are all half-range = 15 decimal, i.e. the pixel is 0011 1101 1110 1111, XORing will give 1100 0010 0001 0000, i.e R, G, and B are all 10000 binary = 16 decimal. For cases like this, the XORed text will be very difficult to make out. To get around this, you could saturate (force to maximum or minimum) each colour component, then XOR / invert the result. For my example, the 15 decimal component values are forced to 0, then inverted to give 11111 binary, which should be visible.