The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

Compatibility with other colour packages

library(palettes)

Overview

Because palettes supports casting and coercion for colour vectors, it is generally compatible with other colour packages and functions that accept or return colours as a character vector of "#RRGGBB" or "#RRGGBBAA", colour names from grDevices::colors(), or a positive integer that indexes into grDevices::palette().

This vignette shows how to cast and coerce colour vectors with a select number of colour packages. We use the following colour vector for demonstration.

colour_vector <- pal_colour(
  c("#a00e00", "#d04e00", "#f6c200", "#0086a8", "#132b69")
)
colour_vector
#> <palettes_colour[5]>
#> • #A00E00
#> • #D04E00
#> • #F6C200
#> • #0086A8
#> • #132B69

The same approaches below will also work for single colour palettes extracted as a colour vector using [[ or $. See the “Subsetting” section in vignette("palettes", package = "palettes") for more details.

colorspace

library(colorspace)

To turn colour vectors into sRGB objects, pass them to colorspace::hex2RGB().

colour_matrix <- hex2RGB(colour_vector)
colour_matrix
#>              R          G         B
#> [1,] 0.6274510 0.05490196 0.0000000
#> [2,] 0.8156863 0.30588235 0.0000000
#> [3,] 0.9647059 0.76078431 0.0000000
#> [4,] 0.0000000 0.52549020 0.6588235
#> [5,] 0.0745098 0.16862745 0.4117647

To convert colour matrices into a different colour space use as().

as(colour_matrix, "HLS")
#>              H         L         S
#> [1,]   5.25000 0.3137255 1.0000000
#> [2,]  22.50000 0.4078431 1.0000000
#> [3,]  47.31707 0.4823529 1.0000000
#> [4,] 192.14286 0.3294118 1.0000000
#> [5,] 223.25581 0.2431373 0.6935484

To turn colour matrices from any colour space back into colour vectors use colorspace::hex() and as_colour().

colour_strings <- hex(colour_matrix)
colour_strings
#> [1] "#A00E00" "#D04E00" "#F6C200" "#0086A8" "#132B69"

as_colour(colour_strings)
#> <palettes_colour[5]>
#> • #A00E00
#> • #D04E00
#> • #F6C200
#> • #0086A8
#> • #132B69

farver

library(farver)

To turn colour vectors into the standard form expected by farver, pass them to farver::decode_colour().

colour_matrix <- decode_colour(colour_vector)
colour_matrix
#>        r   g   b
#> [1,] 160  14   0
#> [2,] 208  78   0
#> [3,] 246 194   0
#> [4,]   0 134 168
#> [5,]  19  43 105

To convert colour matrices into a different colour space use as().

convert_colour(colour_matrix, "rgb", "lab")
#>             l          a         b
#> [1,] 33.54208  54.577757  47.36117
#> [2,] 50.52643  48.846955  60.83188
#> [3,] 80.83906   4.979744  82.46354
#> [4,] 51.69585 -18.094069 -26.58568
#> [5,] 19.58020  15.807937 -38.93437

To turn colour matrices back into colour vectors use farver::encode_colour() and as_colour().

colour_strings <- encode_colour(colour_matrix)
colour_strings
#> [1] "#A00E00" "#D04E00" "#F6C200" "#0086A8" "#132B69"

as_colour(colour_strings)
#> <palettes_colour[5]>
#> • #A00E00
#> • #D04E00
#> • #F6C200
#> • #0086A8
#> • #132B69

grDevices

library(grDevices)

To turn colour vectors into the standard form expected by grDevices, pass them to grDevices::col2rgb().

colour_matrix <- col2rgb(colour_vector)
colour_matrix
#>       [,1] [,2] [,3] [,4] [,5]
#> red    160  208  246    0   19
#> green   14   78  194  134   43
#> blue     0    0    0  168  105

To convert colour matrices into a different colour space use grDevices::convertColor() with the transpose of the colour matrix.

convertColor(t(colour_matrix), "sRGB", "Lab")
#>             L          a         b
#> [1,] 3857.402  4409.9316  3748.578
#> [2,] 5202.266  4205.9589  4814.042
#> [3,] 7730.129   541.3668  6602.094
#> [4,] 5255.133 -1327.5554 -2215.970
#> [5,] 2479.118  1571.4634 -3318.586

To turn colour matrices back into colour vectors use grDevices::rgb() and as_colour().

colour_strings <- rgb(
  r = colour_matrix[1, ], g = colour_matrix[2, ], b = colour_matrix[3, ],
  maxColorValue = 255
)
colour_strings
#> [1] "#A00E00" "#D04E00" "#F6C200" "#0086A8" "#132B69"

as_colour(colour_strings)
#> <palettes_colour[5]>
#> • #A00E00
#> • #D04E00
#> • #F6C200
#> • #0086A8
#> • #132B69

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.