Skip to content

Selecting complementary colours

11 messages · Deepayan Sarkar, Achim Zeileis, John Fox +4 more

#
Dear r-helpers,

I wonder whether, given the "#rrggbb" representation of a colour, there is a
simple way to select the complementary colour, also expressed as a "#rrggbb"
string.

Any suggestions would be appreciated.

John

--------------------------------
John Fox, Professor
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox
#
On 5/21/07, John Fox <jfox at mcmaster.ca> wrote:
You want rgb2col. The following should work for any standard color
specification:
[1] "#BE961E"

-Deepayan
#
On Mon, 21 May 2007, John Fox wrote:

            
Is the complementary color uniquely defined? My understanding is that you
can take opposite colors on a color wheel, but there are of course various
color wheels available. With "colorspace" you can experiment with this,
e.g.:
  x <- "#81A9D0"
  y_hcl <- as(hex2RGB(x), "polarLUV")
  y_hcl at coords[1, "H"] <- y_hcl at coords[1, "H"] + 180
  y_hcl <- hex(y_hcl)
which is a bit more balanced than
  y_hsv <- as(hex2RGB(x), "HSV")
  y_hsv at coords[1, "H"] <- y_hsv at coords[1, "H"] + 180
  y_hsv <- hex(y_hsv)

hth,
Z
#
Dear Deepayan,

I actually thought of the equivalent of this approach, but it doesn't do
quite what I want.

In retrospect, I didn't specify the problem clearly: What I want to be able
to do is to place text on a background of arbitrary (but known RGB) colour
so that the text is legible. I guess that this is better described as a
"contrasting" than a "complementary" colour.

Your solution, for example breaks down for grays:
[1] "#777777"

Thank you for the suggestion.

John

--------------------------------
John Fox, Professor
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox 
--------------------------------
#
Dear Achim,

As I mentioned in my response to Deepayan's suggestion, I didn't specify the
original problem clearly: The object is to get contrasting colours, so that
when one is plotted over the other, the two will be readily distinguishable.
Your suggestions don't do this for neutral colours:
[1] "#888888"
[1] "#888888"

Thank you for trying to help.

John

--------------------------------
John Fox, Professor
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox 
--------------------------------
#
Hi All,

Complementary colors will not insure legibility. If the text and the  
background are equiluminant, visibility will be severely impaired.
On May 21, 2007, at 8:22 PM, John Fox wrote:

            
_____________________________
Professor Michael Kubovy
University of Virginia
Department of Psychology
USPS:     P.O.Box 400400    Charlottesville, VA 22904-4400
Parcels:    Room 102        Gilmer Hall
         McCormick Road    Charlottesville, VA 22903
Office:    B011    +1-434-982-4729
Lab:        B019    +1-434-982-4751
Fax:        +1-434-982-4766
WWW:    http://www.people.virginia.edu/~mk9y/
#
On Mon, 21 May 2007, John Fox wrote:

            
John,

There may be no unique solution. (For gray, for example.)

I am not sure (in terms of color theory) that maximizing in rgb space 
really is the right thing to do, but perhaps this will help you:
[1] "#000000"
[1] "#0000FF"
[1] "#00FF00"

Regards,

Chuck
Charles C. Berry                        (858) 534-2098
                                          Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	         UC San Diego
http://biostat.ucsd.edu/~cberry/         La Jolla, San Diego 92093-0901
#
Dear Chuck,

This solution works reasonably well for me. Although it occasionally
produces an error, I'm able to trap that.

Thank you -- and to everyone else who responded.

John

--------------------------------
John Fox, Professor
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox 
--------------------------------
#
On Mon, 21 May 2007, John Fox wrote:
Since luminance contrasts are necessary and sufficient for readable text, 
you could use white for dark colors and black for light colors.

Luminance is roughly proportional to  0.2*(R^2.4)+0.6*(G^2.4), suggesting 
something like

lightdark<-function (color)
{
     rgb <- col2rgb(color)/255
     L <- c(0.2, 0.6, 0) %*% rgb
     ifelse(L >= 0.2, "#000060", "#FFFFA0")
}

This uses a pale yellow for dark backgrounds and a dark blue for light 
backgrounds, and it seems to work reasonably well.

 	-thomas
#
"John Fox" <jfox at mcmaster.ca> wrote in message 
news:20070522002251.UJON1671.tomts5-srv.bellnexxia.net at JohnDesktop8300...
A simple approach to contrast is to compute a mean intensity by taking the 
mean of the three  RGB components (should be 0..255) and then going with 
either "black" or "white" -- whichever one  is "farthest" away.

Look at the Color Chart with contrasting numbers
http://research.stowers-institute.org/efg/R/Color/Chart/index.htm
or
http://research.stowers-institute.org/efg/R/Color/Chart/ColorChart.pdf

efg

Earl F. Glynn
Stowers Institute for Medical Research
#
Dear Thomas,

This seems simpler than the solution that I used, so I'll give it a
try.

Thanks,
 John

On Tue, 22 May 2007 09:01:01 -0700 (PDT)
Thomas Lumley <tlumley at u.washington.edu> wrote:
--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada
http://socserv.mcmaster.ca/jfox/