當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R col2rgb 顏色到 RGB 轉換


R語言 col2rgb 位於 grDevices 包(package)。

說明

R顏色到 RGB(紅/綠/藍)轉換。

用法

col2rgb(col, alpha = FALSE)

參數

col

三種向量中的任意一種R顏色規格,即顏色名稱(如colors()),形式為十六進製字符串"#rrggbb"或者"#rrggbbaa"(參考rgb),或正整數i意義palette()[i].

alpha

指示是否應返回 Alpha 通道(不透明度)值的邏輯值。

細節

NA (整數或字符)和 "NA" 表示透明,也可以指定為 "transparent"

不屬於以下類型之一的 col 值將被強製:實向量被強製為整數,其他類型被強製為字符。 (因子被強製轉換為字符:在所有其他情況下,進行強製轉換時都會忽略該類。)

col 的零值和負值是錯誤。

具有三行或四行(對於 alpha = TRUE )且列數為 col 長度的整數矩陣。如果 col 有名稱,這些名稱將用作返回值的列名稱。

例子

col2rgb("peachpuff")
col2rgb(c(blu = "royalblue", reddish = "tomato"))  # note: colnames

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

col2rgb(paste0("gold", 1:4))

col2rgb("#08a0ff")
## all three kinds of color specifications:
col2rgb(c(red = "red", hex = "#abcdef"))
col2rgb(c(palette = 1:3))

##-- NON-INTRODUCTORY examples --

grC <- col2rgb(paste0("gray", 0:100))
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

## How many names are 'aliases' of each other?
ccodes <- c(256^(2:0) %*% crgb)
cl <- split(cc, ccodes)
length(cl) # 502 distinct colors
table(tcc <- lengths(cl))
## All the multiply named colors:
clmult <- cl[tcc >= 2]
names(clmult) <- sapply(clmult, function(x) paste(crgb[,x[1]], collapse = ","))
utils::str(clmult)

## Look at the color cube:
tc <- t(crgb[, !duplicated(ccodes)])
cNms <- rownames(tc)
if(requireNamespace("lattice", quietly = TRUE))
    lattice::cloud(blue ~ red + green, data = as.data.frame(tc), col = cNms)
## The 8 corners of the color cube:
isC <- rowSums(tc == 0 | tc == 255) == 3
cNms[isC] # "white" "black" "blue" "cyan" "green" "magenta" "red" "yellow"

table(is.gray <- tc[,1] == tc[,2] & tc[,2] == tc[,3])  # (397, 105)

## Not run: ## Look at the color cube dynamically:
 if(require("rgl")) {
   open3d(windowRect = c(50,50, 950, 950)) # large, so we see details
   plot3d (tc, col = cNms, size = 11) # --> rotate w/ mouse; enlarged corners:
   points3d(tc[isC,], col = cNms[isC], size=22)
   bg3d("darkgray") # (to "see more"); rotate around gray-axis:
   play3d(spin3d(axis = c(1, 1, 1), rpm = 2), duration = 30)
   if(FALSE) # add all names {zoom in with 2nd mouse button!}
     text3d(tc[!is.gray,], texts = cNms[!is.gray],
                             col = cNms[!is.gray], adj=-1/4, cex = 1/2)
   if(FALSE) { ## next version of {rgl}
     hover3d(tc, labels = cNms)
     message("Move mouse over plot to identify points.")
   } else { ##  click on blob to see colors()' name:
     identify3d(tc, labels=cNms)
   }
 }

## End(Not run)

作者

Martin Maechler and the R core team.

也可以看看

rgbcolorspalette

更新、更靈活的接口 convertColor()

相關用法


注:本文由純淨天空篩選整理自R-devel大神的英文原創作品 Color to RGB Conversion。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。