├── .Rbuildignore ├── R ├── analogous.R ├── opposite.R ├── col2HSV.R ├── setColors.R ├── anagrams.R ├── pals.R ├── square.R ├── tetradic.R ├── splitComp.R ├── adjacent.R ├── triadic.R ├── complementary.R ├── pizza.R ├── wheel.R └── sequential.R ├── NAMESPACE ├── man ├── col2HSV.Rd ├── setColors.Rd ├── pals.Rd ├── anagrams.Rd ├── pizza.Rd ├── square.Rd ├── tetradic.Rd ├── wheel.Rd ├── splitComp.Rd ├── triadic.Rd ├── adjacent.Rd ├── complementary.Rd └── sequential.Rd ├── DESCRIPTION └── README.md /.Rbuildignore: -------------------------------------------------------------------------------- 1 | README.md 2 | -------------------------------------------------------------------------------- /R/analogous.R: -------------------------------------------------------------------------------- 1 | #' @export 2 | analogous <- 3 | function(color, plot=TRUE, bg="white", labcol=NULL, cex=0.8, title=TRUE) 4 | { 5 | adjacent(color, plot=plot, bg=bg, labcol=labcol, cex=cex, title=title) 6 | } 7 | -------------------------------------------------------------------------------- /R/opposite.R: -------------------------------------------------------------------------------- 1 | #' @export 2 | opposite <- 3 | function(color, plot=TRUE, bg="white", labcol=NULL, cex=0.8, title=TRUE) 4 | { 5 | complementary(color, plot=plot, bg=bg, labcol=labcol, cex=cex, title=title) 6 | } 7 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | export(adjacent) 2 | export(anagrams) 3 | export(analogous) 4 | export(col2HSV) 5 | export(complementary) 6 | export(opposite) 7 | export(pals) 8 | export(pizza) 9 | export(sequential) 10 | export(setColors) 11 | export(splitComp) 12 | export(square) 13 | export(tetradic) 14 | export(triadic) 15 | export(wheel) 16 | -------------------------------------------------------------------------------- /man/col2HSV.Rd: -------------------------------------------------------------------------------- 1 | \name{col2HSV} 2 | \alias{col2HSV} 3 | \title{col2HSV: converts a color to HSV in hexadecimal notation} 4 | \usage{ 5 | col2HSV(color) 6 | } 7 | \arguments{ 8 | \item{color}{an R color name or a color in hexadecimal 9 | notation} 10 | } 11 | \value{ 12 | A character vector with the color(s) name(s) in 13 | hexadecimal notation 14 | } 15 | \description{ 16 | col2HSV converts an R color (or a set of colors) into an 17 | HSV color model, and then returns the color names in 18 | hexadeciaml notation 19 | } 20 | \examples{ 21 | # convert 'tomato' 22 | col2HSV("tomato") 23 | } 24 | \author{ 25 | Gaston Sanchez 26 | } 27 | \seealso{ 28 | \code{\link{wheel}} 29 | } 30 | 31 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: colortools 2 | Type: Package 3 | Title: Tools for colors in a Hue-Saturation-Value (HSV) color model 4 | Version: 0.1.6 5 | Date: 2015-08-29 6 | Author: Gaston Sanchez 7 | Maintainer: Gaston Sanchez 8 | Description: R package with handy functions to help users select and play with 9 | color schemes in an HSV color model 10 | License: GPL-3 11 | URL: http://www.gastonsanchez.com 12 | Depends: 13 | R (>= 3.0.1) 14 | Collate: 15 | 'adjacent.R' 16 | 'anagrams.R' 17 | 'analogous.R' 18 | 'col2HSV.R' 19 | 'complementary.R' 20 | 'opposite.R' 21 | 'pals.R' 22 | 'pizza.R' 23 | 'sequential.R' 24 | 'setColors.R' 25 | 'splitComp.R' 26 | 'square.R' 27 | 'tetradic.R' 28 | 'triadic.R' 29 | 'wheel.R' 30 | -------------------------------------------------------------------------------- /man/setColors.Rd: -------------------------------------------------------------------------------- 1 | \name{setColors} 2 | \alias{setColors} 3 | \title{Set Colors for a color wheel} 4 | \usage{ 5 | setColors(color, num) 6 | } 7 | \arguments{ 8 | \item{color}{an R color name or a color in hexadecimal 9 | notation} 10 | 11 | \item{num}{integer value indicating how many colors to be 12 | added to the wheel} 13 | } 14 | \value{ 15 | A character vector with the given color and the set of 16 | colors to create a wheel color 17 | } 18 | \description{ 19 | This function set a given number of colors to create a 20 | color wheel 21 | } 22 | \examples{ 23 | # create a color wheel based on 'tomato' 24 | setColors("tomato", 12) 25 | 26 | # set 7 colors for '#3D6DCC' 27 | setColors("#3D6DCC", 7) 28 | } 29 | \author{ 30 | Gaston Sanchez 31 | } 32 | \seealso{ 33 | \code{\link{col2HSV}} 34 | } 35 | 36 | -------------------------------------------------------------------------------- /man/pals.Rd: -------------------------------------------------------------------------------- 1 | \name{pals} 2 | \alias{pals} 3 | \title{Palette colors included in colortools} 4 | \usage{ 5 | pals(name = NULL, bg = "white") 6 | } 7 | \arguments{ 8 | \item{name}{optional argument to specify the name of the 9 | palette} 10 | 11 | \item{bg}{background color of the plot. Used only when 12 | \code{name=NULL} (by default)} 13 | } 14 | \description{ 15 | This function can be used to display the palettes 16 | included in the package, or to show the color names of 17 | the specified palette 18 | } 19 | \details{ 20 | When no name is given, a plot window will show the 21 | different palettes. If a name is given, then the function 22 | will return the names of the colors for that palette (no 23 | plot) 24 | } 25 | \examples{ 26 | # default use 27 | pals() 28 | 29 | # color names of palette 'terra' 30 | pals("terra") 31 | 32 | # pizza wheel for palette 'cheer' 33 | pizza(pals("cheer"), init.angle = 90) 34 | 35 | # color wheel for first color in palette 'fish' 36 | wheel(pals("fish")[1]) 37 | } 38 | \author{ 39 | Gaston Sanchez 40 | } 41 | \seealso{ 42 | \code{\link{wheel}} 43 | } 44 | 45 | -------------------------------------------------------------------------------- /R/col2HSV.R: -------------------------------------------------------------------------------- 1 | #'@title col2HSV: converts a color to HSV in hexadecimal notation 2 | #' 3 | #'@description 4 | #'col2HSV converts an R color (or a set of colors) into an HSV color model, and 5 | #'then returns the color names in hexadeciaml notation 6 | #' 7 | #'@param color an R color name or a color in hexadecimal notation 8 | #'@return A character vector with the color(s) name(s) in hexadecimal notation 9 | #'@author Gaston Sanchez 10 | #'@seealso \code{\link{wheel}} 11 | #'@export 12 | #'@examples 13 | #' 14 | #' # convert 'tomato' 15 | #' col2HSV("tomato") 16 | #' 17 | col2HSV <- 18 | function(color) 19 | { 20 | # convert to RGB 21 | rgb_col = col2rgb(color) 22 | # convert to HSV 23 | hsv_col = rgb2hsv(rgb_col) 24 | if (length(color) == 1) 25 | { 26 | # get degree 27 | hue = hsv_col[1] 28 | sat = hsv_col[2] 29 | val = hsv_col[3] 30 | # get colors with hsv 31 | hex_col = hsv(hue, sat, val) 32 | } 33 | if (length(color) > 1) 34 | { 35 | hex_col = rep("", length(color)) 36 | for (j in 1:length(color)) 37 | { 38 | hex_col[j] = hsv(hsv_col[1,j], hsv_col[2,j], hsv_col[3,j]) 39 | } 40 | } 41 | hex_col 42 | } 43 | -------------------------------------------------------------------------------- /man/anagrams.Rd: -------------------------------------------------------------------------------- 1 | \name{anagrams} 2 | \alias{anagrams} 3 | \title{anagram colors} 4 | \usage{ 5 | anagrams(color) 6 | } 7 | \arguments{ 8 | \item{color}{an R color name or a color in hexadecimal 9 | notation} 10 | } 11 | \value{ 12 | A character vector with the anagram colors of a given 13 | color in hexadecimal notation 14 | } 15 | \description{ 16 | This function returns the anagrams of a given color. 17 | Anagrams are made by rearranging the hexadecimal letter 18 | pairs to form another color. 19 | } 20 | \details{ 21 | An anagram is a word that is made by rearranging the 22 | letters of another word. In this case, we have color 23 | names instead of words, and hexadecimal letter-pairs 24 | instead of single letters. For example, the color 25 | "#CD1091" is composed by three pairs "CD", "10", and 26 | "91". By rearranging these three pairs, we could obtain 27 | an anagram like "#CD9110" or "#10CD91" 28 | } 29 | \examples{ 30 | # anagrams of 'tomato' 31 | ana_tom <- anagrams("tomato") 32 | 33 | # plot tomato anagrams in a color 'pizza' wheel 34 | pizza(ana_tom, bg = "gray20") 35 | } 36 | \author{ 37 | Gaston Sanchez 38 | } 39 | \seealso{ 40 | \code{\link{pizza}} 41 | } 42 | 43 | -------------------------------------------------------------------------------- /R/setColors.R: -------------------------------------------------------------------------------- 1 | #'@title Set Colors for a color wheel 2 | #' 3 | #'@description 4 | #'This function set a given number of colors to create a color wheel 5 | #' 6 | #' 7 | #'@param color an R color name or a color in hexadecimal notation 8 | #'@param num integer value indicating how many colors to be added to the wheel 9 | #'@return A character vector with the given color and the set of colors to 10 | #'create a wheel color 11 | #'@author Gaston Sanchez 12 | #'@seealso \code{\link{col2HSV}} 13 | #'@export 14 | #'@examples 15 | #' 16 | #' # create a color wheel based on 'tomato' 17 | #' setColors("tomato", 12) 18 | #' 19 | #' # set 7 colors for '#3D6DCC' 20 | #' setColors("#3D6DCC", 7) 21 | #' 22 | setColors <- 23 | function(color, num) 24 | { 25 | # convert to RGB 26 | rgb_col = col2rgb(color) 27 | # convert to HSV 28 | hsv_col = rgb2hsv(rgb_col)[,1] 29 | # get degree 30 | hue = hsv_col[1] 31 | sat = hsv_col[2] 32 | val = hsv_col[3] 33 | cols = seq(hue, hue + 1, by=1/num) 34 | cols = cols[1:num] 35 | cols[cols > 1] <- cols[cols > 1] - 1 36 | # get colors with hsv 37 | colors = hsv(cols, sat, val) 38 | # transparency 39 | if (substr(color, 1, 1) == "#" && nchar(color) == 9) 40 | { 41 | alpha = substr(color, 8, 9) 42 | colors = paste(colors, alpha, sep="") 43 | } 44 | colors 45 | } 46 | -------------------------------------------------------------------------------- /man/pizza.Rd: -------------------------------------------------------------------------------- 1 | \name{pizza} 2 | \alias{pizza} 3 | \title{Pizza color wheel} 4 | \usage{ 5 | pizza(colors, bg = "gray95", border = NA, 6 | init.angle = 105, cex = 0.8, lty = 1, labcol = NULL, 7 | ...) 8 | } 9 | \arguments{ 10 | \item{colors}{a vector with R color names of colors in 11 | hexadecimal notation} 12 | 13 | \item{bg}{background color of the plot. Default 14 | \code{"gray95"}} 15 | 16 | \item{border}{color of the border separating the pizza 17 | slices} 18 | 19 | \item{init.angle}{integer value indicating the start 20 | angle (in degrees) for the slices} 21 | 22 | \item{cex}{numeric value indicating the character 23 | expansion of the labels} 24 | 25 | \item{lty}{argument passed to \code{\link{polygon}} which 26 | draws each slice} 27 | 28 | \item{labcol}{color for the labels (i.e. names of the 29 | colors)} 30 | 31 | \item{\dots}{graphical parameters (\code{\link{par}}) can 32 | be given as argument to \code{pizza}} 33 | } 34 | \description{ 35 | This function displays a color wheel with specified 36 | colors 37 | } 38 | \details{ 39 | This function is based on the \code{\link{pie}} function 40 | } 41 | \examples{ 42 | # pizza color wheel for rainbow colors 43 | pizza(rainbow(7)) 44 | 45 | # pizza color wheel for tomato (18 colors) 46 | pizza(setColors("tomato", 18), bg = "gray20", cex = 0.7) 47 | } 48 | \author{ 49 | Gaston Sanchez 50 | } 51 | \seealso{ 52 | \code{\link{wheel}} 53 | } 54 | 55 | -------------------------------------------------------------------------------- /man/square.Rd: -------------------------------------------------------------------------------- 1 | \name{square} 2 | \alias{square} 3 | \title{Square color scheme} 4 | \usage{ 5 | square(color, plot = TRUE, bg = "white", labcol = NULL, 6 | cex = 0.8, title = TRUE) 7 | } 8 | \arguments{ 9 | \item{color}{an R color name or a color in hexadecimal 10 | notation} 11 | 12 | \item{plot}{logical value indicating whether to plot a 13 | color wheel with the generated scheme} 14 | 15 | \item{bg}{background color of the plot. Used only when 16 | \code{plot=TRUE}} 17 | 18 | \item{labcol}{color for the labels (i.e. names of the 19 | colors). Used only when \code{plot=TRUE}} 20 | 21 | \item{cex}{numeric value indicating the character 22 | expansion of the labels} 23 | 24 | \item{title}{logical value indicating whether to display 25 | a title in the plot. Used only when \code{plot=TRUE}} 26 | } 27 | \value{ 28 | A character vector with the given color and the square 29 | scheme colors in hexadecimal notation 30 | } 31 | \description{ 32 | The square color scheme is similar to the tetradic 33 | scheme, but with all four colors spaced around the color 34 | circle. 35 | } 36 | \details{ 37 | The square colors are obtained following a color wheel 38 | with 12 colors, each one spaced at 30 degrees from each 39 | other. Square color schemes tend to work best if you let 40 | one color be dominant 41 | } 42 | \examples{ 43 | # square color scheme for 'tomato' 44 | square("tomato") 45 | } 46 | \author{ 47 | Gaston Sanchez 48 | } 49 | \seealso{ 50 | \code{\link{complementary}}, \code{\link{adjacent}}, 51 | \code{\link{triadic}}, \code{\link{tetradic}} 52 | } 53 | 54 | -------------------------------------------------------------------------------- /man/tetradic.Rd: -------------------------------------------------------------------------------- 1 | \name{tetradic} 2 | \alias{tetradic} 3 | \title{Tetradic Color Scheme} 4 | \usage{ 5 | tetradic(color, plot = TRUE, bg = "white", labcol = NULL, 6 | cex = 0.8, title = TRUE) 7 | } 8 | \arguments{ 9 | \item{color}{an R color name or a color in hexadecimal 10 | notation} 11 | 12 | \item{plot}{logical value indicating whether to plot a 13 | color wheel with the generated scheme} 14 | 15 | \item{bg}{background color of the plot. Used only when 16 | \code{plot=TRUE}} 17 | 18 | \item{labcol}{color for the labels (i.e. names of the 19 | colors). Used only when \code{plot=TRUE}} 20 | 21 | \item{cex}{numeric value indicating the character 22 | expansion of the labels} 23 | 24 | \item{title}{logical value indicating whether to display 25 | a title in the plot. Used only when \code{plot=TRUE}} 26 | } 27 | \value{ 28 | A character vector with the given color and the tetradic 29 | colors in hexadecimal notation 30 | } 31 | \description{ 32 | Tetradic color schemes uses four colors arranged into two 33 | complementary pairs. 34 | } 35 | \details{ 36 | The tetradic colors are obtained following a color wheel 37 | with 12 colors, each one spaced at 30 degrees from each 38 | oter. 39 | } 40 | \examples{ 41 | # tetradic colors for 'tomato' 42 | tetradic("tomato") 43 | 44 | # tetradic colors for 'tomato' with bg='gray20' 45 | tetradic("tomato", bg = "gray20") 46 | } 47 | \author{ 48 | Gaston Sanchez 49 | } 50 | \seealso{ 51 | \code{\link{complementary}}, \code{\link{splitComp}}, 52 | \code{\link{adjacent}}, \code{\link{triadic}}, 53 | \code{\link{square}} 54 | } 55 | 56 | -------------------------------------------------------------------------------- /man/wheel.Rd: -------------------------------------------------------------------------------- 1 | \name{wheel} 2 | \alias{wheel} 3 | \title{Color Wheel} 4 | \usage{ 5 | wheel(color, num = 12, bg = "gray95", border = NULL, 6 | init.angle = 105, cex = 1, lty = NULL, main = NULL, 7 | verbose = TRUE, ...) 8 | } 9 | \arguments{ 10 | \item{color}{an R color name or a color in hexadecimal 11 | notation} 12 | 13 | \item{num}{integer value indicating how many colors to be 14 | generated for the color wheel} 15 | 16 | \item{bg}{background color of the plor} 17 | 18 | \item{border}{color of the border separating the slices} 19 | 20 | \item{init.angle}{integer value indicating the start 21 | angle (in degrees) for the slices} 22 | 23 | \item{cex}{numeric value indicating the character 24 | expansion of the labels} 25 | 26 | \item{lty}{argument passed to \code{\link{polygon}} which 27 | draws the slices} 28 | 29 | \item{main}{an overall title for the plot} 30 | 31 | \item{verbose}{logical value indicating whether to return 32 | the color names} 33 | 34 | \item{\dots}{graphical parameters (\code{\link{par}}) can 35 | be given as argument to \code{wheel}} 36 | } 37 | \value{ 38 | A character vector with the color names of the generated 39 | wheel in hexadecimal notation 40 | } 41 | \description{ 42 | This function generates a color wheel for a given color 43 | } 44 | \details{ 45 | This function is based on the \code{\link{pie}} function 46 | } 47 | \examples{ 48 | # wheel color with 18 slices for 'tomato' 49 | wheel("tomato", num = 18, bg = "gray20", cex = 0.7) 50 | } 51 | \author{ 52 | Gaston Sanchez 53 | } 54 | \seealso{ 55 | \code{\link{pizza}} 56 | } 57 | 58 | -------------------------------------------------------------------------------- /man/splitComp.Rd: -------------------------------------------------------------------------------- 1 | \name{splitComp} 2 | \alias{splitComp} 3 | \title{Split Complementary Color Scheme} 4 | \usage{ 5 | splitComp(color, plot = TRUE, bg = "white", 6 | labcol = NULL, cex = 0.8, title = TRUE) 7 | } 8 | \arguments{ 9 | \item{color}{an R color name of a color in hexadecimal 10 | notation} 11 | 12 | \item{plot}{logical value indicating whether to plot a 13 | color wheel with the generated scheme} 14 | 15 | \item{bg}{background color of the plot. Used only when 16 | \code{plot=TRUE}} 17 | 18 | \item{labcol}{color for the labels (i.e. names of the 19 | colors). Used only when \code{plot=TRUE}} 20 | 21 | \item{cex}{numeric value indicating the character 22 | expansion of the labels} 23 | 24 | \item{title}{logical value indicating whether to display 25 | a title in the plot. Unsed only when \code{plot=TRUE}} 26 | } 27 | \value{ 28 | A character vector with the given color and the 29 | split-complementary colors in hexadecimal notation 30 | } 31 | \description{ 32 | The split-complementary color scheme is a variation of 33 | the complementary color scheme. It uses the two colors 34 | adjacent to its complement. 35 | } 36 | \details{ 37 | This color scheme has the same strong visual contrast as 38 | the complementary scheme, but it is supposed to have less 39 | tension. 40 | } 41 | \examples{ 42 | # split-complementary colors of 'tomato' (no plot) 43 | splitComp("tomato", plot = FALSE) 44 | 45 | # split-complementary colors of 'tomato' (with dark gray background) 46 | splitComp("tomato", bg = "gray40") 47 | } 48 | \author{ 49 | Gaston Sanchez 50 | } 51 | \seealso{ 52 | \code{\link{complementary}} 53 | } 54 | 55 | -------------------------------------------------------------------------------- /man/triadic.Rd: -------------------------------------------------------------------------------- 1 | \name{triadic} 2 | \alias{triadic} 3 | \title{Triadic Color Scheme} 4 | \usage{ 5 | triadic(color, plot = TRUE, bg = "white", labcol = NULL, 6 | cex = 0.8, title = TRUE) 7 | } 8 | \arguments{ 9 | \item{color}{an R color name or a color in hexadecimal 10 | notation} 11 | 12 | \item{plot}{logical value indicating whether to plot a 13 | color wheel with the generated scheme} 14 | 15 | \item{bg}{background color of the plot. Used only when 16 | \code{plot=TRUE}} 17 | 18 | \item{labcol}{color for the labels (i.e. names of the 19 | colors). Used only when \code{plot=TRUE}} 20 | 21 | \item{cex}{numeric value indicating the character 22 | expansion of the labels} 23 | 24 | \item{title}{logical value indicating whether to display 25 | a title in the plot. Used only when \code{plot=TRUE}} 26 | } 27 | \value{ 28 | A character vector with the given color and the triadic 29 | colors in hexadecimal notation 30 | } 31 | \description{ 32 | Triadic color schemes use colors that are evenly spaced 33 | around the color wheel. 34 | } 35 | \details{ 36 | The triadic colors are obtained following a color wheel 37 | with 12 colors, each one spaced at 30 degrees from each 38 | other. Triadic color schemes tend to be quite vibrant. To 39 | use a triadic harmony successfully, the colors should be 40 | carefully balanced letting one color dominate and use the 41 | others for accent. 42 | } 43 | \examples{ 44 | # triadic colors of 'tomato' 45 | triadic("tomato") 46 | 47 | # triadic colors of 'tomato' with background color 'gray20' 48 | triadic("tomato", bg = "gray20") 49 | } 50 | \author{ 51 | Gaston Sanchez 52 | } 53 | \seealso{ 54 | \code{\link{complementary}}, \code{\link{splitComp}}, 55 | \code{\link{adjacent}}, \code{\link{tetradic}}, 56 | \code{\link{square}} 57 | } 58 | 59 | -------------------------------------------------------------------------------- /man/adjacent.Rd: -------------------------------------------------------------------------------- 1 | \name{adjacent} 2 | \alias{adjacent} 3 | \alias{analogous} 4 | \title{Adjacent or analogous colors} 5 | \usage{ 6 | adjacent(color, plot = TRUE, bg = "white", labcol = NULL, 7 | cex = 0.8, title = TRUE) 8 | } 9 | \arguments{ 10 | \item{color}{an R color name or a color in hexadecimal 11 | notation} 12 | 13 | \item{plot}{logical value indicating whether to plot a 14 | color wheel with the generated scheme} 15 | 16 | \item{bg}{background color of the plot. Used only when 17 | \code{plot=TRUE}} 18 | 19 | \item{labcol}{color for the labels (i.e. names of the 20 | colors). Used only when \code{plot=TRUE}} 21 | 22 | \item{cex}{numeric value indicating the character 23 | expansion of the labels} 24 | 25 | \item{title}{logical value indicating whether to display 26 | a title in the plot. Used only when \code{plot=TRUE}} 27 | } 28 | \value{ 29 | A character vector with the given color and the analogous 30 | colors in hexadecimal notation 31 | } 32 | \description{ 33 | Adjacent color schemes use colors that are next to each 34 | other on the color wheel. These colors usually match well 35 | and create comfortable designs. 36 | } 37 | \details{ 38 | The analogous colors are obtained following a color wheel 39 | with 12 colors, each one spaced at 30 degrees from each 40 | other. 41 | } 42 | \examples{ 43 | # analogous colors of 'red' 44 | adjacent("red", plot = FALSE) 45 | 46 | # analogous colors of 'tomato' with default color wheel 47 | analogous("tomato") 48 | 49 | # analogous colors of '#606FEF' with darker background 50 | adjacent("#606FEF", bg = "gray20") 51 | } 52 | \author{ 53 | Gaston Sanchez 54 | } 55 | \seealso{ 56 | \code{\link{complementary}}, \code{\link{splitComp}}, 57 | \code{\link{triadic}}, \code{\link{tetradic}}, 58 | \code{\link{square}} 59 | } 60 | 61 | -------------------------------------------------------------------------------- /R/anagrams.R: -------------------------------------------------------------------------------- 1 | #'@title anagram colors 2 | #' 3 | #'@description 4 | #'This function returns the anagrams of a given color. Anagrams are made by 5 | #'rearranging the hexadecimal letter pairs to form another color. 6 | #' 7 | #'@details 8 | #'An anagram is a word that is made by rearranging the letters of another word. 9 | #'In this case, we have color names instead of words, and hexadecimal 10 | #'letter-pairs instead of single letters. For example, the color "#CD1091" is 11 | #'composed by three pairs "CD", "10", and "91". By rearranging these three 12 | #'pairs, we could obtain an anagram like "#CD9110" or "#10CD91" 13 | #' 14 | #'@param color an R color name or a color in hexadecimal notation 15 | #'@return A character vector with the anagram colors of a given color in 16 | #'hexadecimal notation 17 | #'@author Gaston Sanchez 18 | #'@seealso \code{\link{pizza}} 19 | #'@export 20 | #'@examples 21 | #' 22 | #' # anagrams of 'tomato' 23 | #' ana_tom <- anagrams("tomato") 24 | #' 25 | #' # plot tomato anagrams in a color 'pizza' wheel 26 | #' pizza(ana_tom, bg = "gray20") 27 | #' 28 | anagrams <- 29 | function(color) 30 | { 31 | # convert to HSV 32 | col_hsv = rgb2hsv(col2rgb((color)))[,1] 33 | # get hue, saturation, and value 34 | hue = col_hsv[1] 35 | sat = col_hsv[2] 36 | val = col_hsv[3] 37 | # transparency 38 | alpha = 1 39 | if (substr(color, 1, 1) == "#" && nchar(color) == 9) 40 | alpha = substr(color, 8, 9) 41 | # make sure it is in HEX code 42 | hex = hsv(hue, sat, val) 43 | # break down hex in pairs 44 | par1 = substr(hex, 2, 3) 45 | par2 = substr(hex, 4, 5) 46 | par3 = substr(hex, 6, 7) 47 | # put pars in a vector 48 | hp = c(par1, par2, par3) 49 | # create vector to store results 50 | anacols = rep("", 27) 51 | # generate anagrams 52 | aux = 1 53 | for (i in 1:3) 54 | { 55 | for (j in 1:3) 56 | { 57 | for (k in 1:3) 58 | { 59 | anacols[aux] = paste("#", hp[i], hp[j], hp[k], sep="") 60 | aux = aux + 1 61 | } 62 | } 63 | } 64 | if (alpha != 1) 65 | anacols = paste(anacols, alpha, sep="") 66 | # result 67 | anacols 68 | } 69 | -------------------------------------------------------------------------------- /man/complementary.Rd: -------------------------------------------------------------------------------- 1 | \name{complementary} 2 | \alias{complementary} 3 | \alias{opposite} 4 | \title{Complementary or opposite color} 5 | \usage{ 6 | complementary(color, plot = TRUE, bg = "white", 7 | labcol = NULL, cex = 0.8, title = TRUE) 8 | } 9 | \arguments{ 10 | \item{color}{an R color name or color in hexadecimal 11 | notation} 12 | 13 | \item{plot}{logical value indicating whether to plot a 14 | color wheel with the generated scheme} 15 | 16 | \item{bg}{background color of the plot. Used only when 17 | \code{plot=TRUE}} 18 | 19 | \item{labcol}{color for the labels (i.e. names of the 20 | colors). Used only when \code{plot=TRUE}} 21 | 22 | \item{cex}{numeric value indicating the character 23 | expansion of the labels} 24 | 25 | \item{title}{logical value indicating whether to display 26 | a title in the plot. Used ony when \code{plot=TRUE}} 27 | } 28 | \value{ 29 | A character vector with the given color and the 30 | complementary color in hexadecimal notation 31 | } 32 | \description{ 33 | Complementary or opposite color scheme is formed by 34 | colors that are opposite each other on the color wheel 35 | (example: red and green). The high contrast of 36 | complementary colors creates a vibrant look that must be 37 | managed well so it is not jarring. 38 | } 39 | \details{ 40 | The complementary color is obtained following a color 41 | wheel with 12 colors, each one spaced at 30 degrees from 42 | each other. Complementary color schemes are tricky to use 43 | in large doses, but work well when you wnat something to 44 | stand out. In addition, omplementary colors are really 45 | bad for text. 46 | } 47 | \examples{ 48 | # complementary color of 'tomato' with no plot 49 | opposite("tomato", plot = FALSE) 50 | 51 | # complementary color of 'tomato' with color wheel 52 | opposite("tomato", bg = "gray30") 53 | } 54 | \author{ 55 | Gaston Sanchez 56 | } 57 | \seealso{ 58 | \code{\link{adjacent}}, \code{\link{splitComp}}, 59 | \code{\link{triadic}}, \code{\link{tetradic}}, 60 | \code{\link{square}} 61 | } 62 | 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # `"colortools"` 2 | 3 | `colortools` is an R package designed to help users generate color schemes and color palettes. It provides some handy functions that will allow you to select and play with colors in an [HSV](http://en.wikipedia.org/wiki/HSL_and_HSV) color representation. 4 | 5 | 6 | ## Donation 7 | 8 | As a Data Science and Statistics educator, I love to share the work I do. 9 | Each month I spend dozens of hours curating learning materials and computational 10 | tools like this R package. If you find any value and usefulness in `colortools`, 11 | please consider making a 12 | one-time donation---via paypal---in any amount 13 | (e.g. the amount you would spend inviting me a coffee or any other drink). 14 | Your support really matters. 15 | 16 | 17 | 18 | 19 | ## Installation 20 | 21 | Stable version on [CRAN](http://cran.r-project.org/web/packages/colortools/index.html) 22 | ```ruby 23 | # stable version 24 | install.packages("colortools") 25 | ``` 26 | 27 | Development version on [github](https://github.com/gastonstat/colortools) 28 | ```ruby 29 | # development version 30 | library(devtools) 31 | install_github('colortools', username='gastonstat') 32 | ``` 33 | 34 | ## Some Examples 35 | 36 | ```ruby 37 | # load package 38 | library(colortools) 39 | 40 | # define some colors 41 | some_colors = setColors("#3D6DCC", 15) 42 | 43 | # pizza plot 44 | pizza(some_colors) 45 | 46 | # analagous scheme for color "#3D6DCC" 47 | analogous("#3D6DCC") 48 | 49 | # complementary scheme for color "#3D6DCC" 50 | complementary("#3D6DCC") 51 | 52 | # split complementary scheme for color "#3D6DCC" 53 | splitComp("#3D6DCC") 54 | 55 | # triadic scheme for color "#3D6DCC" 56 | triadic("#3D6DCC") 57 | 58 | # tetradic scheme for color "#3D6DCC" 59 | tetradic("#3D6DCC") 60 | 61 | # square scheme for color "#3D6DCC" 62 | square("#3D6DCC") 63 | 64 | # sequential colors for "#3D6DCC" 65 | sequential("#3D6DCC") 66 | ``` 67 | 68 | ## Author 69 | 70 | [www.gastonsanchez.com](https://www.gastonsanchez.com) 71 | 72 | Gaston Sanchez (`gaston.stat at gmail.com`) 73 | -------------------------------------------------------------------------------- /R/pals.R: -------------------------------------------------------------------------------- 1 | #'@title Palette colors included in colortools 2 | #' 3 | #'@description 4 | #'This function can be used to display the palettes included in the package, or 5 | #'to show the color names of the specified palette 6 | #' 7 | #'@details 8 | #'When no name is given, a plot window will show the different palettes. 9 | #'If a name is given, then the function will return the names of the colors for 10 | #'that palette (no plot) 11 | #' 12 | #'@param name optional argument to specify the name of the palette 13 | #'@param bg background color of the plot. Used only when \code{name=NULL} (by 14 | #'default) 15 | #'@author Gaston Sanchez 16 | #'@seealso \code{\link{wheel}} 17 | #'@export 18 | #'@examples 19 | #' 20 | #' # default use 21 | #' pals() 22 | #' 23 | #' # color names of palette 'terra' 24 | #' pals("terra") 25 | #' 26 | #' # pizza wheel for palette 'cheer' 27 | #' pizza(pals("cheer"), init.angle = 90) 28 | #' 29 | #' # color wheel for first color in palette 'fish' 30 | #' wheel(pals("fish")[1]) 31 | #' 32 | pals <- 33 | function(name=NULL, bg="white") 34 | { 35 | pal_cols = list( 36 | c("#69D2E7", "#A7DBD8", "#E0E4CC", "#F38630", "#FA6900"), 37 | c("#556270", "#4ECDC4", "#C7F464", "#FF6B6B", "#C44D58"), 38 | c("#0B486B", "#3B8686", "#79BD9A", "#A8DBA8", "#C44D58"), 39 | c("#4B4452", "#487D76", "#92B55F", "#E8DA5E", "#FF4746"), 40 | c("#00A0B0", "#6A4A3C", "#CC333F", "#EB6841", "#EDC951"), 41 | c("#554236", "#F77825", "#D3CE3D", "#F1EFA5", "#60B99A"), 42 | c("#E8DDCB", "#CDB380", "#036564", "#033649", "#031634"), 43 | c("#594F4F", "#547980", "#45ADA8", "#9DE0AD", "#E5FCC2"), 44 | c("#1B676B", "#519548", "#88C425", "#BEF202", "#EAFDE6"), 45 | c("#B2D9F7", "#487AA1", "#3D3C3B", "#7C8071", "#DDE3CA")) 46 | pal_names = c("fish", "cheer", "drift", "spec", "ocean", 47 | "mystery", "terra", "pancake", "dream", "deserve") 48 | 49 | if (is.null(name)) 50 | { 51 | n = 5 52 | par(mar = c(2, 5, 2, 2), bg=bg) 53 | plot(0, 0, type="n", xlim = c(0, 1), ylim = c(0, 1), 54 | axes = FALSE, xlab = "", ylab = "") 55 | k = 0 56 | h = 1/15 57 | for (i in 1:10) 58 | { 59 | rect(0:(n-1)/n, h-1/15, 1:n/n, h, col=pal_cols[[i]], border=bg, lwd=3) 60 | h = h + 1/10 61 | } 62 | cp = "black" 63 | if (mean(col2rgb(bg)) <= 127) 64 | cp = "white" 65 | mtext(pal_names, side=2, at = seq(0.04, .93, length=10), 66 | col=cp, las=2) 67 | mtext("pals palette names", side=3, at=0.5, line=0, col=cp) 68 | } else { 69 | if (name %in% pal_names) { 70 | pal_cols[[which(pal_names == name)]] 71 | } else { 72 | stop("Wrong color name") 73 | } 74 | } 75 | } 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /R/square.R: -------------------------------------------------------------------------------- 1 | #' @title Square color scheme 2 | #' 3 | #' @description 4 | #' The square color scheme is similar to the tetradic scheme, but with all four 5 | #' colors spaced around the color circle. 6 | #' 7 | #' @details 8 | #' The square colors are obtained following a color wheel with 12 colors, each 9 | #' one spaced at 30 degrees from each other. Square color schemes tend to work 10 | #' best if you let one color be dominant 11 | #' 12 | #' @param color an R color name or a color in hexadecimal notation 13 | #' @param plot logical value indicating whether to plot a color wheel with the 14 | #' generated scheme 15 | #' @param bg background color of the plot. Used only when \code{plot=TRUE} 16 | #' @param labcol color for the labels (i.e. names of the colors). Used only when 17 | #' \code{plot=TRUE} 18 | #' @param cex numeric value indicating the character expansion of the labels 19 | #' @param title logical value indicating whether to display a title in the plot. 20 | #' Used only when \code{plot=TRUE} 21 | #' @return A character vector with the given color and the square scheme colors 22 | #' in hexadecimal notation 23 | #' @author Gaston Sanchez 24 | #' @seealso \code{\link{complementary}}, \code{\link{adjacent}}, 25 | #' \code{\link{triadic}}, \code{\link{tetradic}} 26 | #' @export 27 | #' @examples 28 | #' # square color scheme for 'tomato' 29 | #' square("tomato") 30 | #' 31 | square <- 32 | function(color, plot=TRUE, bg="white", labcol=NULL, cex = 0.8, title=TRUE) 33 | { 34 | tmp_cols = setColors(color, 12) 35 | sqr_colors <- tmp_cols[c(1,4,7,10)] 36 | 37 | # plot 38 | if (plot) 39 | { 40 | # labels color 41 | if (is.null(labcol)) 42 | { 43 | lab_col = rep("", 12) 44 | if (mean(col2rgb(bg)) > 127) 45 | { 46 | lab_col[c(1,4,7,10)] <- "black" 47 | lab_col[c(2,3,5,6,8,9,11,12)] <- col2HSV(bg) 48 | } else { 49 | lab_col[c(1,4,7,10)] <- "white" 50 | lab_col[c(2,3,5,6,8,9,11,12)] <- col2HSV(bg) 51 | } 52 | } else { 53 | lab_col = rep(labcol, 12) 54 | if (mean(col2rgb(bg)) > 127) 55 | { 56 | lab_col[c(1,4,7,10)] <- labcol 57 | lab_col[c(2,3,5,6,8,9,11,12)] <- col2HSV(bg) 58 | } else { 59 | lab_col[c(1,4,7,10)] <- labcol 60 | lab_col[c(2,3,5,6,8,9,11,12)] <- col2HSV(bg) 61 | } 62 | } 63 | # hide non-adjacent colors 64 | tmp_cols[c(2,3,5,6,8,9,11,12)] <- paste(substr(tmp_cols[c(2,3,5,6,8,9,11,12)],1,7), "0D", sep="") 65 | pizza(tmp_cols, labcol=lab_col, bg=bg, cex = cex) 66 | # title 67 | if (title) 68 | title(paste("Square color scheme of: ", tmp_cols[1]), 69 | col.main=lab_col[1], cex.main=0.8) 70 | } 71 | # result 72 | sqr_colors 73 | } 74 | -------------------------------------------------------------------------------- /R/tetradic.R: -------------------------------------------------------------------------------- 1 | #' @title Tetradic Color Scheme 2 | #' 3 | #' @description 4 | #' Tetradic color schemes uses four colors arranged into two complementary 5 | #' pairs. 6 | #' 7 | #' @details 8 | #' The tetradic colors are obtained following a color wheel with 12 colors, each 9 | #' one spaced at 30 degrees from each oter. 10 | #' 11 | #' @param color an R color name or a color in hexadecimal notation 12 | #' @param plot logical value indicating whether to plot a color wheel with the 13 | #' generated scheme 14 | #' @param bg background color of the plot. Used only when \code{plot=TRUE} 15 | #' @param labcol color for the labels (i.e. names of the colors). Used only when 16 | #' \code{plot=TRUE} 17 | #' @param cex numeric value indicating the character expansion of the labels 18 | #' @param title logical value indicating whether to display a title in the plot. 19 | #' Used only when \code{plot=TRUE} 20 | #' @return A character vector with the given color and the tetradic colors in 21 | #' hexadecimal notation 22 | #' @author Gaston Sanchez 23 | #' @seealso \code{\link{complementary}}, \code{\link{splitComp}}, 24 | #' \code{\link{adjacent}}, \code{\link{triadic}}, \code{\link{square}} 25 | #' @export 26 | #' @examples 27 | #' # tetradic colors for 'tomato' 28 | #' tetradic("tomato") 29 | #' 30 | #' # tetradic colors for 'tomato' with bg='gray20' 31 | #' tetradic("tomato", bg = "gray20") 32 | #' 33 | tetradic <- 34 | function(color, plot=TRUE, bg="white", labcol=NULL, cex=0.8, title=TRUE) 35 | { 36 | tmp_cols = setColors(color, 12) 37 | tetrad_colors <- tmp_cols[c(1,3,7,9)] 38 | 39 | # plot 40 | if (plot) 41 | { 42 | # labels color 43 | if (is.null(labcol)) 44 | { 45 | lab_col = rep("", 12) 46 | if (mean(col2rgb(bg)) > 127) 47 | { 48 | lab_col[c(1,3,7,9)] <- "black" 49 | lab_col[c(2,4,5,6,8,10,11,12)] <- col2HSV(bg) 50 | } else { 51 | lab_col[c(1,3,7,9)] <- "white" 52 | lab_col[c(2,4,5,6,8,10,11,12)] <- col2HSV(bg) 53 | } 54 | } else { 55 | lab_col = rep(labcol, 12) 56 | if (mean(col2rgb(bg)) > 127) 57 | { 58 | lab_col[c(1,3,7,9)] <- labcol 59 | lab_col[c(2,4,5,6,8,10,11,12)] <- col2HSV(bg) 60 | } else { 61 | lab_col[c(1,3,7,9)] <- labcol 62 | lab_col[c(2,4,5,6,8,10,11,12)] <- col2HSV(bg) 63 | } 64 | } 65 | # hide non-adjacent colors 66 | tmp_cols[c(2,4,5,6,8,10,11,12)] <- paste(substr(tmp_cols[c(2,4,5,6,8,10,11,12)],1,7), "0D", sep="") 67 | pizza(tmp_cols, labcol=lab_col, bg=bg, cex=cex) 68 | # title 69 | if (title) 70 | title(paste("Tetradic colors of: ", tmp_cols[1]), 71 | col.main=lab_col[1], cex.main=0.8) 72 | } 73 | # result 74 | tetrad_colors 75 | } 76 | -------------------------------------------------------------------------------- /R/splitComp.R: -------------------------------------------------------------------------------- 1 | #' @title Split Complementary Color Scheme 2 | #' 3 | #' @description 4 | #' The split-complementary color scheme is a variation of the complementary 5 | #' color scheme. It uses the two colors adjacent to its complement. 6 | #' 7 | #' @details 8 | #' This color scheme has the same strong visual contrast as the complementary 9 | #' scheme, but it is supposed to have less tension. 10 | #' 11 | #' @param color an R color name of a color in hexadecimal notation 12 | #' @param plot logical value indicating whether to plot a color wheel with the 13 | #' generated scheme 14 | #' @param bg background color of the plot. Used only when \code{plot=TRUE} 15 | #' @param labcol color for the labels (i.e. names of the colors). Used only when 16 | #' \code{plot=TRUE} 17 | #' @param cex numeric value indicating the character expansion of the labels 18 | #' @param title logical value indicating whether to display a title in the plot. 19 | #' Unsed only when \code{plot=TRUE} 20 | #' @return A character vector with the given color and the split-complementary 21 | #' colors in hexadecimal notation 22 | #' @author Gaston Sanchez 23 | #' @seealso \code{\link{complementary}} 24 | #' @export 25 | #' @examples 26 | #' # split-complementary colors of 'tomato' (no plot) 27 | #' splitComp("tomato", plot = FALSE) 28 | #' 29 | #' # split-complementary colors of 'tomato' (with dark gray background) 30 | #' splitComp("tomato", bg = "gray40") 31 | #' 32 | splitComp <- 33 | function(color, plot=TRUE, bg="white", labcol=NULL, cex=0.8, title=TRUE) 34 | { 35 | tmp_cols = setColors(color, 12) 36 | split_colors <- tmp_cols[c(1,6,8)] 37 | 38 | # plot 39 | if (plot) 40 | { 41 | # labels color 42 | if (is.null(labcol)) 43 | { 44 | lab_col = rep("", 12) 45 | if (mean(col2rgb(bg)) > 127) 46 | { 47 | lab_col[c(1, 6, 8)] <- "black" 48 | lab_col[c(2:5,7,9:12)] <- col2HSV(bg) 49 | } else { 50 | lab_col[c(1, 6, 8)] <- "white" 51 | lab_col[c(2:5,7,9:12)] <- col2HSV(bg) 52 | } 53 | } else { 54 | lab_col = rep(labcol, 12) 55 | if (mean(col2rgb(bg)) > 127) 56 | { 57 | lab_col[c(1, 6, 8)] <- labcol 58 | lab_col[c(2:5,7,9:12)] <- col2HSV(bg) 59 | } else { 60 | lab_col[c(1, 6, 8)] <- labcol 61 | lab_col[c(2:5,7,9:12)] <- col2HSV(bg) 62 | } 63 | } 64 | # hide non-adjacent colors 65 | tmp_cols[c(2:5,7,9:12)] <- paste(substr(tmp_cols[c(2:5,7,9:12)],1,7), "0D", sep="") 66 | pizza(tmp_cols, labcol=lab_col, bg=bg, cex=cex) 67 | # title 68 | if (title) 69 | title(paste("Split complementary colors of: ", tmp_cols[1]), 70 | col.main=lab_col[1], cex.main=0.8) 71 | } 72 | # result 73 | split_colors 74 | } 75 | -------------------------------------------------------------------------------- /R/adjacent.R: -------------------------------------------------------------------------------- 1 | #' @title Adjacent or analogous colors 2 | #' 3 | #' @description 4 | #' Adjacent color schemes use colors that are next to each other on the color 5 | #' wheel. These colors usually match well and create comfortable designs. 6 | #' 7 | #' @details 8 | #' The analogous colors are obtained following a color wheel with 12 colors, 9 | #' each one spaced at 30 degrees from each other. 10 | #' 11 | #' @aliases adjacent analogous 12 | #' @param color an R color name or a color in hexadecimal notation 13 | #' @param plot logical value indicating whether to plot a color wheel with the 14 | #' generated scheme 15 | #' @param bg background color of the plot. Used only when \code{plot=TRUE} 16 | #' @param labcol color for the labels (i.e. names of the colors). Used only when 17 | #' \code{plot=TRUE} 18 | #' @param cex numeric value indicating the character expansion of the labels 19 | #' @param title logical value indicating whether to display a title in the plot. 20 | #' Used only when \code{plot=TRUE} 21 | #' @return A character vector with the given color and the analogous colors in 22 | #' hexadecimal notation 23 | #' @author Gaston Sanchez 24 | #' @seealso \code{\link{complementary}}, \code{\link{splitComp}}, 25 | #' \code{\link{triadic}}, \code{\link{tetradic}}, \code{\link{square}} 26 | #' @export 27 | #' @examples 28 | #' # analogous colors of 'red' 29 | #' adjacent("red", plot = FALSE) 30 | #' 31 | #' # analogous colors of 'tomato' with default color wheel 32 | #' analogous("tomato") 33 | #' 34 | #' # analogous colors of '#606FEF' with darker background 35 | #' adjacent("#606FEF", bg = "gray20") 36 | #' 37 | adjacent <- 38 | function(color, plot=TRUE, bg="white", labcol=NULL, cex=0.8, title=TRUE) 39 | { 40 | tmp_cols = setColors(color, 12) 41 | adja_colors <- tmp_cols[c(1,2,12)] 42 | 43 | # plot 44 | if (plot) 45 | { 46 | # labels color 47 | if (is.null(labcol)) 48 | { 49 | lab_col = rep("", 12) 50 | if (mean(col2rgb(bg)) > 127) 51 | { 52 | lab_col[c(1, 2, 12)] <- "black" 53 | lab_col[c(3:11)] <- col2HSV(bg) 54 | } else { 55 | lab_col[c(1, 2, 12)] <- "white" 56 | lab_col[c(3:11)] <- col2HSV(bg) 57 | } 58 | } else { 59 | lab_col = rep(labcol, 12) 60 | if (mean(col2rgb(bg)) > 127) 61 | { 62 | lab_col[c(1, 2, 12)] <- labcol 63 | lab_col[c(3:11)] <- col2HSV(bg) 64 | } else { 65 | lab_col[c(1, 2, 12)] <- labcol 66 | lab_col[c(3:11)] <- col2HSV(bg) 67 | } 68 | } 69 | # hide non-adjacent colors 70 | tmp_cols[c(3:11)] <- paste(substr(tmp_cols[c(3:11)],1,7), "0D", sep="") 71 | pizza(tmp_cols, labcol=lab_col, bg=bg, cex=cex) 72 | # title 73 | if (title) 74 | title(paste("Adjacent (analogous) colors of: ", tmp_cols[1]), 75 | col.main=lab_col[1], cex.main=0.8) 76 | } 77 | # result 78 | adja_colors 79 | } 80 | -------------------------------------------------------------------------------- /R/triadic.R: -------------------------------------------------------------------------------- 1 | #' @title Triadic Color Scheme 2 | #' 3 | #' @description 4 | #' Triadic color schemes use colors that are evenly spaced around the color 5 | #' wheel. 6 | #' 7 | #' @details 8 | #' The triadic colors are obtained following a color wheel with 12 colors, each 9 | #' one spaced at 30 degrees from each other. 10 | #' Triadic color schemes tend to be quite vibrant. To use a triadic harmony 11 | #' successfully, the colors should be carefully balanced letting one color 12 | #' dominate and use the others for accent. 13 | #' 14 | #' @param color an R color name or a color in hexadecimal notation 15 | #' @param plot logical value indicating whether to plot a color wheel with the 16 | #' generated scheme 17 | #' @param bg background color of the plot. Used only when \code{plot=TRUE} 18 | #' @param labcol color for the labels (i.e. names of the colors). Used only when 19 | #' \code{plot=TRUE} 20 | #' @param cex numeric value indicating the character expansion of the labels 21 | #' @param title logical value indicating whether to display a title in the plot. 22 | #' Used only when \code{plot=TRUE} 23 | #' @return A character vector with the given color and the triadic colors in 24 | #' hexadecimal notation 25 | #' @author Gaston Sanchez 26 | #' @seealso \code{\link{complementary}}, \code{\link{splitComp}}, 27 | #' \code{\link{adjacent}}, \code{\link{tetradic}}, \code{\link{square}} 28 | #' @export 29 | #' @examples 30 | #' # triadic colors of 'tomato' 31 | #' triadic("tomato") 32 | #' 33 | #' # triadic colors of 'tomato' with background color 'gray20' 34 | #' triadic("tomato", bg = "gray20") 35 | #' 36 | triadic <- 37 | function(color, plot=TRUE, bg="white", labcol=NULL, cex=0.8, title=TRUE) 38 | { 39 | tmp_cols = setColors(color, 12) 40 | triad_colors <- tmp_cols[c(1,5,9)] 41 | 42 | # plot 43 | if (plot) 44 | { 45 | # labels color 46 | if (is.null(labcol)) 47 | { 48 | lab_col = rep("", 12) 49 | if (mean(col2rgb(bg)) > 127) 50 | { 51 | lab_col[c(1, 5, 9)] <- "black" 52 | lab_col[c(2,3,4,6,7,8,10,11,12)] <- col2HSV(bg) 53 | } else { 54 | lab_col[c(1, 5, 9)] <- "white" 55 | lab_col[c(2,3,4,6,7,8,10,11,12)] <- col2HSV(bg) 56 | } 57 | } else { 58 | lab_col = rep(labcol, 12) 59 | if (mean(col2rgb(bg)) > 127) 60 | { 61 | lab_col[c(1, 5, 9)] <- labcol 62 | lab_col[c(2,3,4,6,7,8,10,11,12)] <- col2HSV(bg) 63 | } else { 64 | lab_col[c(1, 5, 9)] <- labcol 65 | lab_col[c(2,3,4,6,7,8,10,11,12)] <- col2HSV(bg) 66 | } 67 | } 68 | # hide non-adjacent colors 69 | tmp_cols[c(2,3,4,6,7,8,10,11,12)] <- paste( 70 | substr(tmp_cols[c(2,3,4,6,7,8,10,11,12)],1,7), "0D", sep="") 71 | pizza(tmp_cols, labcol=lab_col, bg=bg, cex=cex) 72 | # title 73 | if (title) 74 | title(paste("Triadic color scheme of: ", tmp_cols[1]), 75 | col.main=lab_col[1], cex.main=0.8) 76 | } 77 | # result 78 | triad_colors 79 | } 80 | -------------------------------------------------------------------------------- /R/complementary.R: -------------------------------------------------------------------------------- 1 | #' @title Complementary or opposite color 2 | #' 3 | #' @description 4 | #' Complementary or opposite color scheme is formed by colors that are opposite 5 | #' each other on the color wheel (example: red and green). The high contrast of 6 | #' complementary colors creates a vibrant look that must be managed well so it 7 | #' is not jarring. 8 | #' 9 | #' @details 10 | #' The complementary color is obtained following a color wheel with 12 colors, 11 | #' each one spaced at 30 degrees from each other. 12 | #' Complementary color schemes are tricky to use in large doses, but work well 13 | #' when you wnat something to stand out. In addition, omplementary colors are 14 | #' really bad for text. 15 | #' 16 | #' @aliases complementary opposite 17 | #' @param color an R color name or color in hexadecimal notation 18 | #' @param plot logical value indicating whether to plot a color wheel with the 19 | #' generated scheme 20 | #' @param bg background color of the plot. Used only when \code{plot=TRUE} 21 | #' @param labcol color for the labels (i.e. names of the colors). Used only when 22 | #' \code{plot=TRUE} 23 | #' @param cex numeric value indicating the character expansion of the labels 24 | #' @param title logical value indicating whether to display a title in the plot. 25 | #' Used ony when \code{plot=TRUE} 26 | #' @return A character vector with the given color and the complementary color 27 | #' in hexadecimal notation 28 | #' @author Gaston Sanchez 29 | #' @seealso \code{\link{adjacent}}, \code{\link{splitComp}}, 30 | #' \code{\link{triadic}}, \code{\link{tetradic}}, \code{\link{square}} 31 | #' @export 32 | #' @examples 33 | #' # complementary color of 'tomato' with no plot 34 | #' opposite("tomato", plot = FALSE) 35 | #' 36 | #' # complementary color of 'tomato' with color wheel 37 | #' opposite("tomato", bg = "gray30") 38 | #' 39 | complementary <- 40 | function(color, plot=TRUE, bg="white", labcol=NULL, cex=0.8, title=TRUE) 41 | { 42 | tmp_cols = setColors(color, 12) 43 | comp_colors <- tmp_cols[c(1, 7)] 44 | 45 | # plot 46 | if (plot) 47 | { 48 | # labels color 49 | if (is.null(labcol)) 50 | { 51 | lab_col = rep("", 12) 52 | if (mean(col2rgb(bg)) > 127) 53 | { 54 | lab_col[c(1, 7)] <- "black" 55 | lab_col[c(2:6,8:12)] <- col2HSV(bg) 56 | } else { 57 | lab_col[c(1, 7)] <- "white" 58 | lab_col[c(2:6,8:12)] <- col2HSV(bg) 59 | } 60 | } else { 61 | lab_col = rep(labcol, 12) 62 | if (mean(col2rgb(bg)) > 127) 63 | { 64 | lab_col[c(1, 7)] <- labcol 65 | lab_col[c(2:6,8:12)] <- col2HSV(bg) 66 | } else { 67 | lab_col[c(1, 7)] <- labcol 68 | lab_col[c(2:6,8:12)] <- col2HSV(bg) 69 | } 70 | } 71 | # hide non-adjacent colors 72 | tmp_cols[c(2:6,8:12)] <- paste(substr(tmp_cols[c(2:6,8:12)],1,7), "0D", sep="") 73 | pizza(tmp_cols, labcol=lab_col, bg=bg, cex=cex) 74 | # title 75 | if (title) 76 | title(paste("Complementary (opposite) color of: ", tmp_cols[1]), 77 | col.main=lab_col[1], cex.main=0.8) 78 | } 79 | # result 80 | comp_colors 81 | } 82 | -------------------------------------------------------------------------------- /R/pizza.R: -------------------------------------------------------------------------------- 1 | #' @title Pizza color wheel 2 | #' 3 | #' @description 4 | #' This function displays a color wheel with specified colors 5 | #' 6 | #' @details 7 | #' This function is based on the \code{\link{pie}} function 8 | #' 9 | #' @param colors a vector with R color names of colors in hexadecimal notation 10 | #' @param bg background color of the plot. Default \code{"gray95"} 11 | #' @param border color of the border separating the pizza slices 12 | #' @param init.angle integer value indicating the start angle (in degrees) for 13 | #' the slices 14 | #' @param cex numeric value indicating the character expansion of the labels 15 | #' @param lty argument passed to \code{\link{polygon}} which draws each slice 16 | #' @param labcol color for the labels (i.e. names of the colors) 17 | #' @param \dots graphical parameters (\code{\link{par}}) can be given as 18 | #' argument to \code{pizza} 19 | #' @author Gaston Sanchez 20 | #' @seealso \code{\link{wheel}} 21 | #' @export 22 | #' @examples 23 | #' 24 | #' # pizza color wheel for rainbow colors 25 | #' pizza(rainbow(7)) 26 | #' 27 | #' # pizza color wheel for tomato (18 colors) 28 | #' pizza(setColors("tomato", 18), bg = "gray20", cex = 0.7) 29 | #' 30 | pizza <- 31 | function(colors, bg = "gray95", border = NA, 32 | init.angle = 105, cex = 0.8, lty = 1, labcol = NULL, ...) 33 | { 34 | n <- length(colors) 35 | x <- rep(1, n) 36 | x <- c(0, cumsum(x)/sum(x)) 37 | dx <- diff(x) 38 | nx <- length(dx) 39 | # set colors 40 | labels = colors 41 | # 42 | if (is.null(labcol)) 43 | { 44 | if (mean(col2rgb(bg)) > 127) 45 | labcol = rep("black", n) 46 | if (mean(col2rgb(bg)) <= 127) 47 | labcol = rep("white", n) 48 | } 49 | # prepare plot window 50 | par(bg = bg) 51 | plot.new() 52 | pin <- par("pin") 53 | xlim <- ylim <- c(-1, 1) 54 | if (pin[1L] > pin[2L]) 55 | xlim <- (pin[1L]/pin[2L]) * xlim 56 | else ylim <- (pin[2L]/pin[1L]) * ylim 57 | dev.hold() 58 | on.exit(dev.flush()) 59 | plot.window(xlim, ylim, "", asp = 1) 60 | # get ready to plot 61 | border <- rep(border, length.out = nx) 62 | if (is.null(border[1])) 63 | border <- rep(bg, length.out = nx) 64 | lty <- rep(lty, length.out = nx) 65 | angle <- rep(45, length.out = nx) 66 | radius = seq(1, 0, by=-1/n)[1:n] 67 | twopi <- -2 * pi 68 | t2xy <- function(t, rad) { 69 | t2p <- twopi * t + init.angle * pi/180 70 | list(x = rad * cos(t2p), y = rad * sin(t2p)) 71 | } 72 | # plot colored segments 73 | for (i in 1L:nx) 74 | { 75 | n <- max(2, floor(200 * dx[i])) 76 | P <- t2xy(seq.int(x[i], x[i + 1], length.out = n), rad=radius[1]) 77 | polygon(c(P$x, 0), c(P$y, 0), angle = angle[i], 78 | border = border[i], col = colors[i], lty = lty[i]) 79 | P <- t2xy(mean(x[i + 0:1]), rad=radius[1]) 80 | lab <- labels[i] 81 | if (!is.na(lab) && nzchar(lab)) { 82 | adjs = 0.5 83 | if (P$x > 1e-08) adjs <- 0 84 | if (P$x < -1e-08) adjs <- 1 85 | lines(c(1, 1.05) * P$x, c(1, 1.05) * P$y, col=labcol[i]) 86 | text(1.1 * P$x, 1.1 * P$y, labels[i], xpd = TRUE, 87 | adj = adjs, cex=cex, col=labcol[i], ...) 88 | } 89 | } 90 | invisible(NULL) 91 | } 92 | -------------------------------------------------------------------------------- /man/sequential.Rd: -------------------------------------------------------------------------------- 1 | \name{sequential} 2 | \alias{sequential} 3 | \title{sequential HSV colors} 4 | \usage{ 5 | sequential(color, percentage = 5, what = "saturation", 6 | s = NULL, v = NULL, alpha = NULL, fun = "linear", 7 | plot = TRUE, verbose = TRUE) 8 | } 9 | \arguments{ 10 | \item{color}{an R color name or a color in hexadeciaml 11 | notation} 12 | 13 | \item{percentage}{numeric value indicating the increment 14 | steps of the sequence in percentage} 15 | 16 | \item{what}{character string indicating what parameter to 17 | taki into account to generate the sequence. Possible 18 | values are \code{"saturation"}, \code{"value"}, and 19 | \code{alpha}} 20 | 21 | \item{s}{optional decimal value (between 0 and 1) to fix 22 | the color saturation} 23 | 24 | \item{v}{optional decimal value (between 0 and 1) to fix 25 | the color value} 26 | 27 | \item{alpha}{optional decimal value (between 0 and 1) to 28 | fix the color alpha transparency} 29 | 30 | \item{fun}{character string indicating the applied 31 | transformation to the generated sequence. Possible values 32 | are \code{"linear"}, \code{"sqrt"}, and \code{"log"}} 33 | 34 | \item{plot}{logical value indicating whether to plot the 35 | sequence} 36 | 37 | \item{verbose}{logical value indicating whether to return 38 | the color names of the sequence} 39 | } 40 | \description{ 41 | This functions allows to get a sequence of colors in an 42 | HSV model with optional pre-especified numbers for 43 | saturation, value, and alpha. It is a very flexible 44 | function to play with different combinations of 45 | saturation, value, and alpha. 46 | } 47 | \details{ 48 | The idea bechind this function is to explore a sequence 49 | of colors given some fixed numbers of saturation, valur 50 | or alpha for an HSV color model. The argument \code{what} 51 | will be taken to generate the sequence in the given 52 | \code{percentage} increment steps. In addition, we can 53 | specify a number for \code{s, v, alpha}. For example, if 54 | \code{what="value"}, we can fix the saturation in 55 | \code{s=0.8}, obtaining a sequence of colors with 56 | different values but with the same level of saturation. 57 | 58 | The argument \code{fun} allows to apply a transformation 59 | to the generated sequence. By default 60 | \code{fun="linear"}, no transformation is applied. If 61 | \code{fun="sqrt"}, the square root of the generated 62 | sequence will be taken. If \code{fun="log"}, the 63 | logarithmic of the generated sequence will be taken. 64 | } 65 | \examples{ 66 | # sequence for 'orange' 67 | sequential("orange") 68 | 69 | # sequence for 'orange' with fun='sqrt' transformation 70 | sequential("orange", fun = "sqrt") 71 | 72 | # sequence for 'orange' with fun='log' transformation 73 | sequential("orange", fun = "log") 74 | 75 | # sequential sequence for value with fix saturation s=0.7 and fun='log' 76 | sequential("orange", what = "value", s = 0.7, fun = "log") 77 | 78 | # sequential sequence for saturation, with fix value s=0.8, alpha=0.5, percentage 10, and fun='log' 79 | sequential("orange", 10, what = "value", s = 0.7, alpha = 0.5, fun = "log") 80 | } 81 | \author{ 82 | Gaston Sanchez 83 | } 84 | \seealso{ 85 | \code{\link{pizza}} 86 | } 87 | 88 | -------------------------------------------------------------------------------- /R/wheel.R: -------------------------------------------------------------------------------- 1 | #' @title Color Wheel 2 | #' 3 | #' @description 4 | #' This function generates a color wheel for a given color 5 | #' 6 | #' @details 7 | #' This function is based on the \code{\link{pie}} function 8 | #' 9 | #' @param color an R color name or a color in hexadecimal notation 10 | #' @param num integer value indicating how many colors to be generated for the 11 | #' color wheel 12 | #' @param bg background color of the plor 13 | #' @param border color of the border separating the slices 14 | #' @param init.angle integer value indicating the start angle (in degrees) for 15 | #' the slices 16 | #' @param cex numeric value indicating the character expansion of the labels 17 | #' @param lty argument passed to \code{\link{polygon}} which draws the slices 18 | #' @param main an overall title for the plot 19 | #' @param verbose logical value indicating whether to return the color names 20 | #' @param \dots graphical parameters (\code{\link{par}}) can be given as 21 | #' argument to \code{wheel} 22 | #' @return A character vector with the color names of the generated wheel in 23 | #' hexadecimal notation 24 | #' @author Gaston Sanchez 25 | #' @seealso \code{\link{pizza}} 26 | #' @export 27 | #' @examples 28 | #' # wheel color with 18 slices for 'tomato' 29 | #' wheel("tomato", num = 18, bg = "gray20", cex = 0.7) 30 | #' 31 | wheel <- 32 | function(color, num=12, bg="gray95", border=NULL, 33 | init.angle=105, cex=1, lty=NULL, main=NULL, verbose=TRUE, ...) 34 | { 35 | if (!is.numeric(num) || any(is.na(num) | num < 0)) 36 | stop("\n'num' must be positive") 37 | x <- rep(1, num) 38 | x <- c(0, cumsum(x)/sum(x)) 39 | dx <- diff(x) 40 | nx <- length(dx) 41 | # set colors 42 | col = setColors(color, num) 43 | labels = col 44 | # labels color 45 | labcol = ifelse( mean(col2rgb(bg)) > 127, "black", "white") 46 | # prepare plot window 47 | par(bg = bg) 48 | plot.new() 49 | pin <- par("pin") 50 | xlim <- ylim <- c(-1, 1) 51 | if (pin[1L] > pin[2L]) 52 | xlim <- (pin[1L]/pin[2L]) * xlim 53 | else ylim <- (pin[2L]/pin[1L]) * ylim 54 | dev.hold() 55 | on.exit(dev.flush()) 56 | plot.window(xlim, ylim, "", asp = 1) 57 | # get ready to plot 58 | if (is.null(border[1])) { 59 | border <- rep(bg, length.out = nx) 60 | } else { 61 | border <- rep(border, length.out = nx) 62 | } 63 | if (!is.null(lty)) 64 | lty <- rep(NULL, length.out = nx) 65 | angle <- rep(45, length.out = nx) 66 | radius = seq(1, 0, by=-1/num)[1:num] 67 | twopi <- -2 * pi 68 | t2xy <- function(t, rad) { 69 | t2p <- twopi * t + init.angle * pi/180 70 | list(x = rad * cos(t2p), y = rad * sin(t2p)) 71 | } 72 | # plot colored segments 73 | for (i in 1L:nx) 74 | { 75 | n <- max(2, floor(200 * dx[i])) 76 | P <- t2xy(seq.int(x[i], x[i + 1], length.out = n), rad=radius[1]) 77 | polygon(c(P$x, 0), c(P$y, 0), angle = angle[i], 78 | border = border[i], col = col[i], lty = lty[i]) 79 | P <- t2xy(mean(x[i + 0:1]), rad=radius[1]) 80 | lab <- labels[i] 81 | if (!is.na(lab) && nzchar(lab)) { 82 | adjs = 0.5 83 | if (P$x > 1e-08) adjs <- 0 84 | if (P$x < -1e-08) adjs <- 1 85 | lines(c(1, 1.05) * P$x, c(1, 1.05) * P$y) 86 | text(1.1 * P$x, 1.1 * P$y, labels[i], xpd = TRUE, 87 | adj = adjs, cex=cex, col=labcol, ...) 88 | } 89 | } 90 | # add title 91 | title(main = main, ...) 92 | # return color names 93 | if (verbose) 94 | col 95 | } 96 | -------------------------------------------------------------------------------- /R/sequential.R: -------------------------------------------------------------------------------- 1 | #'@title sequential HSV colors 2 | #' 3 | #'@description 4 | #'This functions allows to get a sequence of colors in an HSV model with 5 | #'optional pre-especified numbers for saturation, value, and alpha. It is a 6 | #'very flexible function to play with different combinations of saturation, 7 | #'value, and alpha. 8 | #' 9 | #'@details 10 | #'The idea bechind this function is to explore a sequence of colors given some 11 | #'fixed numbers of saturation, valur or alpha for an HSV color model. The 12 | #'argument \code{what} will be taken to generate the sequence in the given 13 | #'\code{percentage} increment steps. In addition, we can specify a number for 14 | #'\code{s, v, alpha}. For example, if \code{what="value"}, we can fix the 15 | #'saturation in \code{s=0.8}, obtaining a sequence of colors with different 16 | #'values but with the same level of saturation. 17 | #' 18 | #'The argument \code{fun} allows to apply a transformation to the generated 19 | #'sequence. By default \code{fun="linear"}, no transformation is applied. If 20 | #'\code{fun="sqrt"}, the square root of the generated sequence will be taken. 21 | #'If \code{fun="log"}, the logarithmic of the generated sequence will be taken. 22 | #' 23 | #'@param color an R color name or a color in hexadeciaml notation 24 | #'@param percentage numeric value indicating the increment steps of the 25 | #'sequence in percentage 26 | #'@param what character string indicating what parameter to taki into account 27 | #'to generate the sequence. Possible values are \code{"saturation"}, 28 | #'\code{"value"}, and \code{alpha} 29 | #'@param s optional decimal value (between 0 and 1) to fix the color saturation 30 | #'@param v optional decimal value (between 0 and 1) to fix the color value 31 | #'@param alpha optional decimal value (between 0 and 1) to fix the color alpha 32 | #'transparency 33 | #'@param fun character string indicating the applied transformation to the 34 | #'generated sequence. Possible values are \code{"linear"}, \code{"sqrt"}, and 35 | #'\code{"log"} 36 | #'@param plot logical value indicating whether to plot the sequence 37 | #'@param verbose logical value indicating whether to return the color names of 38 | #'the sequence 39 | #'@author Gaston Sanchez 40 | #'@seealso \code{\link{pizza}} 41 | #'@export 42 | #'@examples 43 | #' 44 | #' # sequence for 'orange' 45 | #' sequential("orange") 46 | #' 47 | #' # sequence for 'orange' with fun='sqrt' transformation 48 | #' sequential("orange", fun = "sqrt") 49 | #' 50 | #' # sequence for 'orange' with fun='log' transformation 51 | #' sequential("orange", fun = "log") 52 | #' 53 | #' # sequential sequence for value with fix saturation s=0.7 and fun='log' 54 | #' sequential("orange", what = "value", s = 0.7, fun = "log") 55 | #' 56 | #' # sequential sequence for saturation, with fix value s=0.8, alpha=0.5, percentage 10, and fun='log' 57 | #' sequential("orange", 10, what = "value", s = 0.7, alpha = 0.5, fun = "log") 58 | #' 59 | sequential <- 60 | function(color, percentage=5, what="saturation", 61 | s=NULL, v=NULL, alpha=NULL, fun="linear", plot=TRUE, verbose=TRUE) 62 | { 63 | # convert to HSV 64 | col_hsv = rgb2hsv(col2rgb(color))[,1] 65 | # transparency 66 | if (is.null(alpha)) 67 | alpha = 1 68 | if (substr(color, 1, 1) == "#" && nchar(color) == 9) 69 | alpha = substr(color, 8, 9) 70 | # get hue, saturation, and value 71 | hue = col_hsv[1] 72 | if (is.null(s)) s = col_hsv[2] 73 | if (is.null(v)) v = col_hsv[3] 74 | # sequence function 75 | getseq = switch(fun, 76 | linear = seq(0, 1, by=percentage/100), 77 | sqrt = sqrt(seq(0, 1, by=percentage/100)), 78 | log = log1p(seq(0, 1, by=percentage/100)), 79 | log10 = log10(seq(0, 1, by=percentage/100)) 80 | ) 81 | # what type of sequence? 82 | if (what == "saturation") { 83 | sat = getseq 84 | fixed = paste("v=", round(v,2), " and alpha=", alpha, sep="") 85 | if (is.numeric(alpha)) 86 | seq_col = hsv(hue, s=sat, v=v, alpha=alpha) 87 | if (is.character(alpha)) { 88 | seq_col = hsv(hue, s=sat, v=v) 89 | seq_col = paste(seq_col, alpha, sep="") 90 | } 91 | } 92 | if (what == "value") { 93 | val = getseq 94 | fixed = paste("s=", round(s,2), " and alpha=", alpha, sep="") 95 | if (is.numeric(alpha)) 96 | seq_col = hsv(hue, s=s, v=val, alpha=alpha) 97 | if (is.character(alpha)) { 98 | seq_col = hsv(hue, s=s, v=val) 99 | seq_col = paste(seq_col, alpha, sep="") 100 | } 101 | } 102 | if (what == "alpha") { 103 | alpha = getseq 104 | fixed = paste("s=", round(s,2), " and v=", round(v,2), sep="") 105 | seq_col = hsv(hue, s=s, v=v, alpha=alpha) 106 | } 107 | # if plot TRUE 108 | if (plot) 109 | { 110 | n = length(seq(0, 1, by=percentage/100)) 111 | fx = unlist(fixed) 112 | #dev.new() 113 | plot(0, 0, type="n", xlim=c(0,1), ylim=c(0,1), axes=FALSE, xlab="", ylab="") 114 | rect(0:(n-1)/n, 0, 1:n/n, 1, col=seq_col, border="lightgray") 115 | mtext(seq_col, side=1, at=0.5:(n)/n, cex=0.8, las=2) 116 | title(paste("Sequential colors based on ", what, "\n with fixed ", fx, sep=""), 117 | cex.main=0.9) 118 | } 119 | # result 120 | if (verbose) 121 | seq_col 122 | } 123 | 124 | --------------------------------------------------------------------------------