Board index » delphi » Detecting color density

Detecting color density


2008-05-06 11:18:09 PM
delphi158
Hi,
I have a label on top of a shape component serving as a background color,
the user can change the shape color but not the text in the label.
Any idea on how to determine if a color in the shape is dark enough to
change the text color in the label to white?
 
 

Re:Detecting color density

You mean like a progressbar ?
DH
 

Re:Detecting color density

Nop, I need to change the color of the label without knowing the color of
the background. That is detecting the background color.
 

Re:Detecting color density

That should work :
in ApplicationEvents.OnIdle
var shape:TColor;
redshape, gshape, bshape:Integer;
begin
color := Graphics.ColorToRGB(shape.Color)
rshape := Windows.GetRValue(color);
gshape := Windows.getGValue(color);
bshape := Windows.getBValue(color);
if (rshape>128) and (gshape>128) and (bshape>128) // if shape is
painted dark
then label.Color := clBlack
else label.Color := clWhite;
...
Done := True;
end;
You can also set a timer and check the colors ! But a timer doesnt wait for
the end of Painting !
DH
 

Re:Detecting color density

Hi Gil,
You could determine the color intensity with this:
Y = 0.299R + 0.587G + 0.114B
if Y is greater than 127, then it is brighter, otherwise it is darker.
Best regards.
Xiaoguang
 

Re:Detecting color density

Perfect!
thanks