Mark Heckmann
2010-Jan-29 10:08 UTC
[R] detect brightness of hex color value in R / convert from hex to hsl/hsv space how?
Ein eingebundener Text mit undefiniertem Zeichensatz wurde abgetrennt. Name: nicht verf?gbar URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100129/2c8f63ce/attachment.pl>
Achim Zeileis
2010-Jan-29 10:36 UTC
[R] detect brightness of hex color value in R / convert from hex to hsl/hsv space how?
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]] > >
Michael Friendly
2010-Jan-29 14:08 UTC
[R] detect brightness of hex color value in R / convert from hex to hsl/hsv space how?
Perhaps some variation of SetTextContrastColor <- function(color) { ifelse( mean(col2rgb(color)) > 127, "black", "white") } 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. > > 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]] > >-- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 4700 Keele Street http://www.math.yorku.ca/SCS/friendly.html Toronto, ONT M3J 1P3 CANADA
Greg Snow
2010-Jan-29 16:44 UTC
[R] detect brightness of hex color value in R / convert from hex to hsl/hsv space how?
There is a col2grey (and col2gray) function in the TeachingDemos package that use a common algorithm to convert colors to grey based on perceived lightness, that may work for you on deciding the color. For placing text on colored backgrounds, look at the shadowtext function (also in TeachingDemos) for another way. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Mark Heckmann > Sent: Friday, January 29, 2010 3:09 AM > To: r-help at r-project.org > Subject: [R] detect brightness of hex color value in R / convert from > hex to hsl/hsv space how? > > 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. > > 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]]