以前の日記で少し前フリしていた色の話。の続き。
- 色は視認性が高いグラフを書くときに大切な要素
- 色の多用は、グラフの本質を隠してしまう
- 人が理解できる限界は3~4色じゃないだろうか
と、思ってます。
ここでは、Rを起動すると自動的に読み込まれるgrDevicesパッケージの話。
色を作る関数以外に、色を階調に分解したり、他の色空間のパラメータに変換する関数があるという話。
- col2rgb関数
colors関数、6進法の色コードを赤(red)、緑(green)、青(blue)の色強度を出力する。
各色は256階調の数値になる。
> palette() [1] "black" "red" "green3" "blue" "cyan" "magenta" "yellow" [8] "gray" > col2rgb(palette()) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] red 0 255 0 0 0 255 255 190 green 0 0 205 0 255 0 255 190 blue 0 0 0 255 255 255 0 190 > > x [1] "#00FF00" "#14FF00" "#27FF00" "#3BFF00" "#4EFF00" "#62FF00" "#76FF00" [8] "#89FF00" "#9DFF00" "#B1FF00" "#C4FF00" "#D8FF00" "#EBFF00" "#FFFF00" > col2rgb(x) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] red 0 20 39 59 78 98 118 137 157 177 196 216 235 green 255 255 255 255 255 255 255 255 255 255 255 255 255 blue 0 0 0 0 0 0 0 0 0 0 0 0 0 [,14] red 255 green 255 blue 0
- rgb2hsv関数
RGB色空間をHSV色空間の値に変換する関数。
> dat <- col2rgb(palette()) > rgb2hsv(dat) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] h 0 0 0.3333333 0.6666667 0.5 0.8333333 0.1666667 0.000000 s 0 1 1.0000000 1.0000000 1.0 1.0000000 1.0000000 0.000000 v 0 1 0.8039216 1.0000000 1.0 1.0000000 1.0000000 0.745098 > > dat <- col2rgb(x) > rgb2hsv(dat) [,1] [,2] [,3] [,4] [,5] [,6] [,7] h 0.3333333 0.3202614 0.3078431 0.2947712 0.2823529 0.269281 0.2562092 s 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.000000 1.0000000 v 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 1.000000 1.0000000 [,8] [,9] [,10] [,11] [,12] [,13] [,14] h 0.2437908 0.230719 0.2176471 0.2052288 0.1921569 0.1797386 0.1666667 s 1.0000000 1.000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000 v 1.0000000 1.000000 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
- convertColor関数
color で指定する3要素を from の色空間から、to の色空間へ変換する関数。
from、toで指定できる色空間はヘルプに色々書いてある。
Details:
Color spaces are specified by objects of class ‘colorConverter’,
created by ‘colorConverter’ or ‘make.rgb’. Built-in color
spaces may be referenced by strings: ‘"XYZ"’, ‘"sRGB"’,
‘"Apple RGB"’, ‘"CIE RGB"’, ‘"Lab"’, ‘"Luv"’. The
converters for these colour spaces are in the object
‘colorspaces’.
> convertColor(t(dat[,1:2]),from="sRGB",to="Lab") # 行方向に3要素を取る L a.x b [1,] 8344.343 -6973.622 6695.560 [2,] 8346.220 -6956.418 6697.851 > > convertColor(t(dat[,1:2]),from="sRGB",to="Luv") L u v [1,] 8344.343 -7971.693 10194.44 [2,] 8346.220 -7947.891 10193.62