col2rgb                 package:base                 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:

_U_s_a_g_e:

     col2rgb(col)

_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]'.

_V_a_l_u_e:

     an integer matrix with three 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:7)# the ones from the palette()
     stopifnot(col2rgb(0) ==  # col = 0 is the background color:
         print(col2rgb(par("bg"))))

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

     stopifnot(col2rgb("#08a0ff") == c(8, 160, 255))
     ## 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=""))
     stopifnot(grC["red",] == grC["green",],
               grC["red",] == grC["blue",],
               grC["red", 1:4] == c(0,3,5,8))
     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=","))
     str(cl)

      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])
      }

