#####################################################################
#
# FILE Rcolors.R
#
#####################################################################
#
#' Plots a map of the default colors for easy reference
#'
#' This will produce two plots: the first one plots the colors
#' by name, and the second one by their respective Hex codes.
#'
#' The code is courtesy of the blog New Energy Research at
#' bxhorn.com
#'
#' @export
Rcolors <- function() {
# 1.Define R Color Data ----
# RGB codes
color.rgb <- t(col2rgb(colors()))
# Hexadecimal codes
color.hex <- rgb(color.rgb[,1], color.rgb[,2], color.rgb[,3], maxColorValue = 255)
# Text highlighting
color.text <- ifelse(apply(color.rgb, 1, mean) > 127, "black", "white")
# Consolidate
color.df <- data.frame(name = colors(),
red = color.rgb[, "red"],
green = color.rgb[, "green"],
blue = color.rgb[, "blue"],
hex = color.hex,
text = color.text)
# 2.Plot R Colors By Name ----
# configure graphical device
n.col <- 11
n.row <- 60
op <- par()
par(pin = c(11.692, 6.267), mai=c(0.5, 0.5, 0.5, 0.5))
# create plot
plot(c(0, n.col), c(0, n.row),
type = "n",
bty = "n",
ylab = "",
xlab = "",
axes = FALSE)
title("R Colors By Name")
for(i in 1:n.col){
color.count <- (i-1) * n.row
color.mod <- length(colors()) - color.count
y.val <- ifelse(color.mod < n.row, n.row - color.mod + 1, 1)
color.names <- as(color.df[color.count + 1:n.row, "name"], "character")
rect(i - 1, y.val - 0.5, i, n.row:y.val + 0.5, border = "black", col = color.names)
text.color <- as(color.df[color.count + 1:n.row, "text"], "character")
text(i-0.5, n.row:y.val, labels = color.names, cex = 0.5, col = text.color)
}
# 3.Plot R Colors By Hex Code ----
# create plot
plot(c(0, n.col), c(0, n.row),
type = "n",
bty = "n",
ylab = "",
xlab = "",
axes = FALSE)
title("R Colors By Hex Code")
for(i in 1:n.col){
color.count <- (i-1) * n.row
color.mod <- length(colors()) - color.count
y.val <- ifelse(color.mod < n.row, n.row-color.mod + 1, 1)
color.names <- as(color.df[color.count + 1:n.row, "hex"], "character")
rect(i - 1, y.val - 0.5, i, n.row:y.val + 0.5, border = "black", col = color.names)
text.color <- as(color.df[color.count + 1:n.row, "text"], "character")
text(i-0.5, n.row:y.val, labels = color.names, cex = 0.5, col = text.color)
}
# reset graphical device
par(pin = op$pin, mai = op$mai)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.