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.
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()
.
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.581655 47.36185
#> [2,] 50.52643 48.851834 60.83304
#> [3,] 80.83906 4.985885 82.46536
#> [4,] 51.69585 -18.090090 -26.58260
#> [5,] 19.58020 15.810397 -38.93221
To turn colour matrices back into colour vectors use
farver::encode_colour()
and as_colour()
.
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