col2rgb              package:grDevices              R Documentation

_C_o_l_o_r _t_o _R_G_B _C_o_n_v_e_r_s_i_o_n

_D_e_s_c_r_i_p_t_i_o_n:

     "Any R color" to RGB (red/green/blue) conversion.

_U_s_a_g_e:

     col2rgb(col, alpha = FALSE)

_A_r_g_u_m_e_n_t_s:

     col: vector of any of the three kind of R colors, i.e., either a
          color name (an element of 'colors()'), a hexadecimal string
          of the form '"#rrggbb"', or an integer 'i' meaning
          'palette()[i]'.

   alpha: logical value indicating whether alpha channel values should
          be returned.

_D_e_t_a_i_l_s:

     For integer colors, '0' is shorthand for the current 'par("bg")',
     and 'NA' means "nothing" which effectively does not draw the
     corresponding item.

     For character colors, '"NA"' is equivalent to 'NA' above.

_V_a_l_u_e:

     an integer matrix with three or four rows and number of columns
     the length (and names if any) as 'col'.

_A_u_t_h_o_r(_s):

     Martin Maechler

_S_e_e _A_l_s_o:

     'rgb', 'colors', 'palette', etc.

_E_x_a_m_p_l_e_s:

     col2rgb("peachpuff")
     col2rgb(c(blu = "royalblue", reddish = "tomato")) # names kept

     col2rgb(1:8)# the ones from the palette() :

     col2rgb(paste("gold", 1:4, sep=""))

     col2rgb("#08a0ff")
     ## all three kind of colors mixed :
     col2rgb(c(red="red", palette= 1:3, hex="#abcdef"))

     ##-- NON-INTRODUCTORY examples --

     grC <- col2rgb(paste("gray",0:100,sep=""))
     table(print(diff(grC["red",])))# '2' or '3': almost equidistant
     ## The 'named' grays are in between {"slate gray" is not gray, strictly}
     col2rgb(c(g66="gray66", darkg= "dark gray", g67="gray67",
               g74="gray74", gray =      "gray", g75="gray75",
               g82="gray82", light="light gray", g83="gray83"))

     crgb <- col2rgb(cc <- colors())
     colnames(crgb) <- cc
     t(crgb)## The whole table

     ccodes <- c(256^(2:0) %*% crgb)## = internal codes
     ## How many names are 'aliases' of each other:
     table(tcc <- table(ccodes))
     length(uc <- unique(sort(ccodes))) # 502
     ## All the multiply named colors:
     mult <- uc[tcc >= 2]
     cl <- lapply(mult, function(m) cc[ccodes == m])
     names(cl) <- apply(col2rgb(sapply(cl, function(x)x[1])),
                        2, function(n)paste(n, collapse=","))
     utils::str(cl)
     ## Not run: 
      if(require(xgobi)) { ## Look at the color cube dynamically :
        tc <- t(crgb[, !duplicated(ccodes)])
        table(is.gray <- tc[,1] == tc[,2] & tc[,2] == tc[,3])# (397, 105)
        xgobi(tc, color = c("gold", "gray")[1 + is.gray])
      }
     ## End(Not run)

