Skip to content

pre summary: mapping of colornames into hsv?

1 message · Jens Oehlschlägel-Akiyoshi

#
Hi Martin,
Great that you follow this. My original intention was to be able to
translate colornames to hsv because this would allow using colornames to cut
out a certain part of the colorwheel for colorcoding (HSV component H, see
my code below)

I think internally we might have colors represented as

Colornames, ColorIntegers, ColorHexcodes, ColorRGBs, ColorHSVs (ColorCMYs?)

however the R-user probably shouldn't see the internal ColorIntegers of the
C-code, as this might lead to confusion with the colornumbers resulting from
the palette settings.

So the R-user should see functions translating

ColorName2ColorHexcode() ColorHexcode2ColorRGB() and ColorRGB2ColorHSV()
and backwards
ColorHSV2ColorRGB() ColorRGB2ColorHexcode(), rather not
ColorHexcode2Colorname() as not every possible color is granted to have a
name.

Regards


Jens


P.S. the folowing function colorcode() I think is more general than
rainbow() in three aspects:
1) works also with not-equidistant colorcodings
2) works around the 'borders' of the colorwheel
3) works both directions along the colorwheel
With the above mentioned translation functions one could specify start= and
end= as colornames, which would make code more readable. Feel free to
include into R.

# 0 = red
# 1/6 = yellow
# 2/6 = green
# 3/6 = cyan
# 4/6 = blue
# 5/6 = violet
# go from red via yellow to green: start=0, stop=2/6
# go from green via yellow to red: start=2/6, stop=0
# go from green via cyan-blue-violet to red: start=2/6, stop=1
# go from red via violet-blue-cyan to green: start=0, stop=-4/6
colorcode <- function (p, start=0, end=2/6, s=1, v=1, ...)
{
   # make p 0..1
   r <- range(p)
   p <- (p - r[1])/diff(r)
   # wrap hue
   up <- start <= end
   start <- start %% 1
   end <- end %% 1
   if (up)
     if (start < end){
       h <- (start + p*(end-start)) %% 1
     }else{
       h <- (start + p*(1+end-start)) %% 1
     }
   else
     if (end < start){
       h <- (start - p*(start-end)) %% 1
     }else{
       h <- (start - p*(1+start-end)) %% 1
     }
   # round against bug if start=1 end=4/3 with p=seq(0, 1, 0.05)
   hsv( round(h, 5), s, v, ...)
}
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._