Package 'colorfast'

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

Help Index


Fast conversion of colors-as-strings to an integer vector of packed RGBA values.

Description

Packed RGBA values are used in R's native raster objects.

Usage

col_to_int(col)

Arguments

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".

Value

Integer vector where the 4 bytes making up each integer represent the RGBA values of the color

Examples

col_to_int(c("hotpink", "#abc", "#abcd", "#aabb99", "#aabb9980"))

Fast conversion of colors-as-strings to a matrix of RGBA integers

Description

This is a faster replacement for grDevices::col2rgb() which uses a hash lookup of R color names (rather than a linear search).

Usage

col_to_rgb(col)

Arguments

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".

Value

An integer matrix with four rows and number of columns the length of the input.

Examples

col_to_rgb(c("hotpink", "#abc", "#abcd", "#aabb99", "#aabb9980"))

Fast conversion of colors-as-packed-RGBA-integers to hexadecimal

Description

Packed RGBA values are used in R's native raster objects.

Usage

int_to_col(icol)

Arguments

icol

Integer vector (where the 4 bytes making up each integer represent the RGBA values of the color)

Value

Character vector

Examples

icols <- col_to_int(c("hotpink", "#abc", "#abcd", "#aabb99", "#aabb9980"))
int_to_col(icols)

Set alpha value of integer colors

Description

Set alpha value of integer colors

Usage

set_alpha(icol, alpha)

Arguments

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'

Value

integer vector of colors with adjusted alpha channel

Examples

icol <- col_to_int('red')
icol
int_to_col(icol)
icol2 <- set_alpha(icol, 0.5)
int_to_col(icol2)