[R] Selecting complementary colours

John Fox jfox at mcmaster.ca
Tue May 22 12:52:31 CEST 2007


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 
-------------------------------- 

> -----Original Message-----
> From: Charles C. Berry [mailto:cberry at tajo.ucsd.edu] 
> Sent: Monday, May 21, 2007 8:49 PM
> To: John Fox
> Cc: 'Deepayan Sarkar'; r-help at stat.math.ethz.ch
> Subject: Re: [R] Selecting complementary colours
> 
> On Mon, 21 May 2007, John Fox wrote:
> 
> > 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.
> 
> 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:
> 
> > cval <- function(x,y) -sum((x-y)^2)
> > contrasting <- function(x) 
> > 
> optim(runif(3,0,255),cval,lower=0,upper=255,method="L-BFGS-B",y=x)$par
> > do.call(rgb,as.list(contrasting(col2rgb("gray"))/255))
> [1] "#000000"
> > do.call(rgb,as.list(contrasting(col2rgb("gray"))/255))
> [1] "#0000FF"
> > do.call(rgb,as.list(contrasting(col2rgb("pink"))/255))
> [1] "#00FF00"
> 
> Regards,
> 
> Chuck
> 
> >
> 
> >
> > Your solution, for example breaks down for grays:
> >
> >> mycol <- "#888888"
> >> do.call(rgb, as.list(1 - col2rgb(mycol) / 255))
> > [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
> > --------------------------------
> >
> >> -----Original Message-----
> >> From: r-help-bounces at stat.math.ethz.ch 
> >> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Deepayan 
> >> Sarkar
> >> Sent: Monday, May 21, 2007 6:45 PM
> >> To: John Fox
> >> Cc: r-help at stat.math.ethz.ch
> >> Subject: Re: [R] Selecting complementary colours
> >>
> >> On 5/21/07, John Fox <jfox at mcmaster.ca> wrote:
> >>> 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.
> >>
> >> You want rgb2col. The following should work for any standard color
> >> specification:
> >>
> >>> mycol = "royalblue"
> >>> do.call(rgb, as.list(1 - col2rgb(mycol) / 255))
> >> [1] "#BE961E"
> >>
> >> -Deepayan
> >>
> >> ______________________________________________
> >> R-help at stat.math.ethz.ch mailing list 
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> >> http://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide 
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
> 
> 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
> 
> 
>



More information about the R-help mailing list