PDA

Click to See Complete Forum and Search --> : [RESOLVED] Algo to detect Light or Dark color


iPrank
Mar 15th, 2006, 02:46 AM
If you change desktop background to a lighter color, Windows autometically changes icon font color to black. If you change it to a darker color, it changes the font color to white.

How it detects which color is lighter and which color is darker ?

penagate
Mar 15th, 2006, 02:48 AM
Colours are stored simply as integer values. You can either test if the sum of all colour parts (the whole number, in other words) is less than a threshold value, or you can break it in to its RGB components and test each of those individually.

ComputerJy
Mar 15th, 2006, 02:51 AM
I think testing the "hue" of the color is easier!

penagate
Mar 15th, 2006, 02:52 AM
Saturation, rather. But colours on Windows are stored as RGB, so it's less efficient to convert to an HSB value first ;)

To split a compound DWORD RGB value:

red = rgbValue & 0xff
green = (rgbValue & 0xff00) \ 0xff
blue = rgbValue \ 0xff00

iPrank
Mar 15th, 2006, 03:04 AM
Yes. I know about RGB extraction.
But I guess testing the sum of all colour parts will not help here.

For example, change desktop color to following RGB values:

RGB(100,255,100) => Text color changes to Black
RGB(255,100,100) => Text color changes to Black
RGB(100,100,255) => Text color changes to White

There must be some other algo that checks which color is darker to human eye.

...And how do you check hue/saturation through VB code ? (.NET has them in COLOR sructure, but what is in VB ?)

penagate
Mar 15th, 2006, 03:27 AM
Well, you could test based upon individual thresholds for red green and blue.

This page (http://www.geocities.com/branco_medeiros/computing/VB00001.htm) illustrates how to convert from RGB to HSB.

iPrank
Mar 15th, 2006, 06:11 AM
Great !!! Thanks. :thumb:
I guess Luminance will do it. It will be easier to check.

btw, Lum and Brightness are same. Aren't they ?

penagate
Mar 15th, 2006, 06:12 AM
One and, yes.

iPrank
Mar 15th, 2006, 06:19 AM
Thanks ! :)