[R] detect brightness of hex color value in R / convert from hex to hsl/hsv space how?

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Fri Jan 29 11:36:32 CET 2010


On Fri, 29 Jan 2010, Mark Heckmann wrote:

> To the R color experts:
> I need to detect if a chosen background color (as hex e.g. #910322) is
> light or dark.
> If it is dark I need to ovelay it with light text and vice versa.

You can use the "colorspace" package for that. hex2RGB() converts a hex 
string to "RGB" coordinates which in turn can be easily converted to 
systems like "HSV" or (preferably) HCL (called "polarLUV" in colorspace).

R> as(hex2RGB("#910322"), "polarLUV")
             L        C        H
[1,] 31.61831 95.14145 6.913428

"L" corresponds to the luminance (whether a color is light or dark), "C" 
is chroma (how colorful the color is compared to a gray with the same 
luminance), and "H" is the hue.

hth,
Z

> Thus I would like to implement the following pseudo code:
>
> if (brightness(color) > somevalue) textcolor= dark else textcolor=red
>
> I am not too familiar with color systems. My idea was to convert the
> hex value to hsv / hsl space and extract the v or l value.
> 1) I am not sure if this is the way to go.
> 2) I do not succeed in it. convertColor {grDevices} or make.rgb
> {grDevices} did not help me with that. How can I convert hex to hsv/
> hsl space
>
> How would you detect the (perceived) color brightness?
>
> Thanks
> Mark
>
> ???????????????????????????????????????
> Mark Heckmann
> Dipl. Wirt.-Ing. cand. Psych.
> Vorstra?e 93 B01
> 28359 Bremen
> Blog: www.markheckmann.de
> R-Blog: http://ryouready.wordpress.com
>
>
>
>
>
> 	[[alternative HTML version deleted]]
>
>



More information about the R-help mailing list