Title: | Fast Conversion of R Colors to Color Component Values and Native Packed Integer Format |
---|---|
Description: | Color values in R are often represented as strings of hexadecimal colors or named colors. This package offers fast conversion of these color representations to either an array of red/green/blue/alpha values or to the packed integer format used in native raster objects. Functions for conversion are also exported at the 'C' level for use in other packages. This fast conversion of colors is implemented using an order-preserving minimal perfect hash derived from Majewski et al (1996) "A Family of Perfect Hashing Methods" <doi:10.1093/comjnl/39.6.547>. |
Authors: | Mike Cheng [aut, cre, cph] |
Maintainer: | Mike Cheng <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.1 |
Built: | 2025-01-14 02:44:40 UTC |
Source: | https://github.com/coolbutuseless/colorfast |
Packed RGBA values are used in R's native raster objects.
col_to_int(col)
col_to_int(col)
col |
Character vector of color names. Supports all R color names (e.g. "red", "hotpink") and hex colors of the form: "#RRGGBBAA", "#RRGGBB", "#RGBA" and "#RGB". |
Integer vector where the 4 bytes making up each integer represent the RGBA values of the color
col_to_int(c("hotpink", "#abc", "#abcd", "#aabb99", "#aabb9980"))
col_to_int(c("hotpink", "#abc", "#abcd", "#aabb99", "#aabb9980"))
This is a faster replacement for grDevices::col2rgb()
which uses
a hash lookup of R color names (rather than a linear search).
col_to_rgb(col)
col_to_rgb(col)
col |
Character vector of color names. Supports all R color names (e.g. "red", "hotpink") and hex colors of the form: "#RRGGBBAA", "#RRGGBB", "#RGBA" and "#RGB". |
An integer matrix with four rows and number of columns the length of the input.
col_to_rgb(c("hotpink", "#abc", "#abcd", "#aabb99", "#aabb9980"))
col_to_rgb(c("hotpink", "#abc", "#abcd", "#aabb99", "#aabb9980"))
Packed RGBA values are used in R's native raster objects.
int_to_col(icol)
int_to_col(icol)
icol |
Integer vector (where the 4 bytes making up each integer represent the RGBA values of the color) |
Character vector
icols <- col_to_int(c("hotpink", "#abc", "#abcd", "#aabb99", "#aabb9980")) int_to_col(icols)
icols <- col_to_int(c("hotpink", "#abc", "#abcd", "#aabb99", "#aabb9980")) int_to_col(icols)
Set alpha value of integer colors
set_alpha(icol, alpha)
set_alpha(icol, alpha)
icol |
Integer vector (where the 4 bytes making up each integer represent the RGBA values of the color) |
alpha |
numeric alpha value in range [0, 1]. Length of 1, or same length as 'col' |
integer vector of colors with adjusted alpha channel
icol <- col_to_int('red') icol int_to_col(icol) icol2 <- set_alpha(icol, 0.5) int_to_col(icol2)
icol <- col_to_int('red') icol int_to_col(icol) icol2 <- set_alpha(icol, 0.5) int_to_col(icol2)