| Title: | Convert Spectral Signatures to sRGB and CIELAB Colour Coordinates |
|---|---|
| Description: | Converts hyperspectral reflectance signatures to device- independent sRGB hex codes and CIE L*a*b* coordinates using the CIE 1931 2-degree standard observer and the D65 standard illuminant. Includes bundled reference data so the package is self-contained. Handles missing and non-finite reflectance values via a configurable policy. |
| Authors: | Jhohan Elguera [aut, cre] |
| Maintainer: | Jhohan Elguera <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.1.0 |
| Built: | 2026-10-20 19:49:51 UTC |
| Source: | https://github.com/cran/spectra2rgb |
Integrates a reflectance spectrum (or a matrix of spectra) against the CIE 1931 2-degree standard observer and the D65 standard illuminant to obtain tristimulus values XYZ.
hs_to_xyz(wavelengths, reflectance)hs_to_xyz(wavelengths, reflectance)
wavelengths |
Numeric vector of wavelengths in nanometres. |
reflectance |
Numeric vector (single spectrum) or matrix
(rows = samples, columns = wavelengths). Column order must match
|
The CIE 1931 colour-matching functions (ciexyz31) and the D65
illuminant (illuminantD65) are bundled with the package. Values
are linearly interpolated onto wavelengths. Wavelengths outside
the CMF range are extrapolated flat (rule = 2 in
approx); the user is responsible for ensuring the
input range is appropriate (400-700 nm is standard for visible colour).
Non-finite reflectance values are replaced by 0 and values are clamped
to before integration.
A data.frame with columns X, Y, Z.
wl <- seq(400, 700, by = 10) ref <- rep(0.5, length(wl)) hs_to_xyz(wl, ref)wl <- seq(400, 700, by = 10) ref <- rep(0.5, length(wl)) hs_to_xyz(wl, ref)
Convert sRGB to CIELAB
rgb_to_lab(R, G, B, white = c(X = 95.047, Y = 100, Z = 108.883))rgb_to_lab(R, G, B, white = c(X = 95.047, Y = 100, Z = 108.883))
R, G, B
|
Integer vectors (0-255). |
white |
Reference white point. |
A data.frame with numeric columns L, a, b.
Convert a data frame of spectra to CIELAB
spectra_to_lab( df, id_col = NULL, na_action = c("zero", "drop", "keep"), white = c(X = 95.047, Y = 100, Z = 108.883) )spectra_to_lab( df, id_col = NULL, na_action = c("zero", "drop", "keep"), white = c(X = 95.047, Y = 100, Z = 108.883) )
df |
A data.frame or matrix where each row is a sample and each column is a wavelength band. Column names must be parseable as numeric wavelengths in nanometres. |
id_col |
Optional character vector of sample IDs, or a column name
or index in |
na_action |
One of |
white |
Reference white point for XYZ -> sRGB. |
A data.frame with columns Sample_ID, L, a,
b.
Convert a data frame of spectra to sRGB
spectra_to_rgb( df, id_col = NULL, na_action = c("zero", "drop", "keep"), white = c(X = 95.047, Y = 100, Z = 108.883) )spectra_to_rgb( df, id_col = NULL, na_action = c("zero", "drop", "keep"), white = c(X = 95.047, Y = 100, Z = 108.883) )
df |
A data.frame or matrix where each row is a sample and each column is a wavelength band. Column names must be parseable as numeric wavelengths in nanometres. |
id_col |
Optional character vector of sample IDs, or a column name
or index in |
na_action |
One of |
white |
Reference white point for XYZ -> sRGB. |
A data.frame with columns Sample_ID, R, G,
B, hex.
Convert a data frame of spectra to sRGB and CIELAB
spectra_to_rgb_lab( df, id_col = NULL, na_action = c("zero", "drop", "keep"), white = c(X = 95.047, Y = 100, Z = 108.883) )spectra_to_rgb_lab( df, id_col = NULL, na_action = c("zero", "drop", "keep"), white = c(X = 95.047, Y = 100, Z = 108.883) )
df |
A data.frame or matrix where each row is a sample and each column is a wavelength band. Column names must be parseable as numeric wavelengths in nanometres. |
id_col |
Optional character vector of sample IDs, or a column name
or index in |
na_action |
One of |
white |
Reference white point for XYZ -> sRGB. |
A data.frame with columns Sample_ID, R, G,
B, hex, L, a, b.
Convenience wrapper: XYZ -> sRGB -> CIELAB. Uses the sRGB path so the
result matches what rgb_to_lab produces from the same XYZ.
xyz_to_lab(XYZ, white = c(X = 95.047, Y = 100, Z = 108.883))xyz_to_lab(XYZ, white = c(X = 95.047, Y = 100, Z = 108.883))
XYZ |
A data.frame with columns |
white |
Reference white point. |
A data.frame with columns L, a, b.
Convert CIE XYZ to sRGB
xyz_to_rgb(XYZ, white = c(X = 95.047, Y = 100, Z = 108.883))xyz_to_rgb(XYZ, white = c(X = 95.047, Y = 100, Z = 108.883))
XYZ |
A data.frame with columns |
white |
Reference white point. Defaults to D65
( |
A data.frame with integer columns R, G, B
(0-255) and character column hex (e.g. "#654909").