Chart of R Colors
Chart of R Colors
Chart of R Colors
Color Chart
Earl F. Glynn
Stowers Institute for Medical Research
24 May 2005
Purpose
This TechNote shows R colors in a way that is intended to aid finding colors by name, or by index in the
colors() vector.
R is such a graphics rich tool, but the textual documentation of R's colors() vector, hinders an end user
from quickly finding colors of interest. The Chart of R Colors in PDF format can be displayed on the screen
research.stowers.org/mcm/efg/R/Color/Chart/ 1/8
4/4/2019 Chart of R Colors
or printed to help locate colors. Such a comparison will show colors that are reproduced reasonably well on
both a display monitor and a particular printer.
In addition to simply displaying the various color in R, this TechNote provides some details of how to work
with color in R.
Background
On many UNIX systems there is a list of color names in the file /usr/lib/X11/rgb.txt (or sometimes /etc/X11).
head /usr/lib/X11/rgb.txt
! $Xorg: rgb.txt,v 1.3 2000/08/17 19:54:00 cpqbld Exp $
255 250 250 snow
248 248 255 ghost white
248 248 255 GhostWhite
245 245 245 white smoke
245 245 245 WhiteSmoke
220 220 220 gainsboro
255 250 240 floral white
255 250 240 FloralWhite
253 245 230 old lace
. . .
For R under Windows, there is a similar rgb.txt file in C:\Program Files\R\rw<version>\etc\rgb.txt, where
<version> is the version of R, like 1091, 2000, or 2010.
Software Requirements
Tested in R 2.1.0
2. Particular color names of interest can be found if their positions in the vector are known, e.g.,
> colors()[c(552,254,26)]
[1] "red" "green" "blue"
> grep("red",colors())
[1] 100 372 373 374 375 376 476 503 504 505 506 507 524 525 526
527 528 552 553
[20] 554 555 556 641 642 643 644 645
> colors()[grep("red",colors())]
[1] "darkred" "indianred" "indianred1" "indianred2"
[5] "indianred3" "indianred4" "mediumvioletred" "orangered"
research.stowers.org/mcm/efg/R/Color/Chart/ 2/8
4/4/2019 Chart of R Colors
> colors()[grep("sky",colors())]
[1] "deepskyblue" "deepskyblue1" "deepskyblue2" "deepskyblue3"
[5] "deepskyblue4" "lightskyblue" "lightskyblue1"
"lightskyblue2"
[9] "lightskyblue3" "lightskyblue4" "skyblue" "skyblue1"
[13] "skyblue2" "skyblue3" "skyblue4"
4. The function col2rgb can be used to extract the RGB (red-green-blue) components of a color, e.g.,
> col2rgb("yellow")
[,1]
red 255
green 255
blue 0
Each of the three RGB color components ranges from 0 to 255, which is interpreted to be 0.0 to 1.0 in RGB
colorspace. With each of the RGB components having 256 possible discrete values, this results in
256*256*256 possible colors, or 16,777,216 colors.
While the RGB component values range from 0 to 255 in decimal, they range from hex 00 to hex FF. Black,
which is RGB = (0,0,0) can be represented in hex as #000000, and white, which is RGB = (255,255,255),
can represented in hex as #FFFFFF.
5. R provides a way to define an RGB triple with each of the color components ranging from 0.0 to 1.0
using the rgb function. For example, yellow can be defined:
The output is in hexadecimal ranging from 00 to FF (i.e., decimal 0 to 255) for each color component. The
0.0 to 1.0 inputs are a bit odd, but are standard in RGB color theory. Since decimal values from 0 to 255 are
common, the rgb function allows a maxColorValue parameter as an alternative:
The R function, GetColorHexAndDecimal, was written to display both hex and decimal values of the color
components for a given color name:
Example:
research.stowers.org/mcm/efg/R/Color/Chart/ 3/8
4/4/2019 Chart of R Colors
> GetColorHexAndDecimal("yellow")
[1] "#FFFF00 255 255 0"
6. Text of a certain color when viewed against certain backgrounds can be very hard to see, e.g., never use
yellow text on a white background since there isn't good contrast between the two. One simple hueristic in
defining a text color for a given background color is to pick the one that is "farthest" away from "black" or
"white". One way to do this is to compute the color intensity, defined as the mean of the RGB triple, and
pick "black" (intensity 0) for text color if the background intensity is greater than 127, or "white" (intensity
255) when the background intensity is less than or equal to 127.
The R function below, SetTextContrastColor, gives a good text color for a given background color
name:
Examples:
> SetTextContrastColor("white")
[1] "black"
> SetTextContrastColor("black")
[1] "white"
> SetTextContrastColor("red")
[1] "white"
> SetTextContrastColor("yellow")
[1] "black"
7. The following R code produces the "R Colors" graphic shown at the top of this page (using
TextContrastColor defined above):
for (j in 0:(rowCount-1))
{
base <- j*colCount
remaining <- length(colors()) - base
RowSize <- ifelse(remaining < colCount, remaining, colCount)
rect((1:RowSize)-0.5,j-0.5, (1:RowSize)+0.5,j+0.5,
border="black",
col=colors()[base + (1:RowSize)])
text((1:RowSize), j, paste(base + (1:RowSize)), cex=0.7,
research.stowers.org/mcm/efg/R/Color/Chart/ 4/8
4/4/2019 Chart of R Colors
col=TextContrastColor[base + (1:RowSize)])
}
8. Alphabetical order is not necessarily a good way to find similar colors. The RGB values of each of the
colors() was converted to hue-saturation-value (HSV) and then sorted by HSV. This approach groups colors
of the same "hue" together a bit better. Here's the code and graphic produced:
for (j in 0:(rowCount-1))
{
for (i in 1:colCount)
{
k <- j*colCount + i
if (k <= length(colors()))
{
rect(i-0.5,j-0.5, i+0.5,j+0.5, border="black", col=colors()[
HueOrder[k] ])
text(i,j, paste(HueOrder[k]), cex=0.7,
col=TextContrastColor[ HueOrder[k] ])
}
}
}
research.stowers.org/mcm/efg/R/Color/Chart/ 5/8
4/4/2019 Chart of R Colors
9. While the color matrices above are useful, a more useful display would include a rectangular area
showing the color, the color index, the color name, and the RGB values, both in hexadecimal, which is often
used in web pages.
The code for this is a bit tedious -- see Item #2 in the ColorChart.R code for complete details. Here is the
first page of the Chart of R colors.
research.stowers.org/mcm/efg/R/Color/Chart/ 6/8
4/4/2019 Chart of R Colors
10. To create a PDF file (named ColorChart.pdf) with all the graphics shown on this page, issue this R
command:
source("http://research.stowers-
institute.org/efg/R/Color/Chart/ColorChart.R")
Updated
6 June 2005
research.stowers.org/mcm/efg/R/Color/Chart/ 8/8