Package {spectra2rgb}


Type: Package
Title: Convert Spectral Signatures to sRGB and CIELAB Colour Coordinates
Version: 0.1.1
Description: Converts hyperspectral reflectance signatures to sRGB hex codes and CIE L*a*b* coordinates using the CIE 1931 2-degree standard observer and the D65 standard illuminant. CIELAB coordinates are calculated directly from CIE XYZ values; sRGB output is generated independently. Includes bundled reference data so the package is self-contained. Handles missing and non-finite reflectance values via a configurable policy.
License: GPL (≥ 3)
Encoding: UTF-8
Depends: R (≥ 4.0)
Imports: stats, grDevices, utils
Suggests: testthat (≥ 3.0.0), knitr, rmarkdown
VignetteBuilder: knitr
Config/roxygen2/version: 8.1.0
NeedsCompilation: no
Packaged: 2026-11-16 09:17:46 UTC; jhohan.elguera
Author: Jhohan Elguera [aut, cre]
Maintainer: Jhohan Elguera <jelguerat@gmail.com>
Repository: CRAN
Date/Publication: 2026-11-16 09:50:02 UTC

Convert hyperspectral reflectance to CIE XYZ

Description

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.

Usage

hs_to_xyz(wavelengths, reflectance)

Arguments

wavelengths

Numeric vector of wavelengths in nanometres.

reflectance

Numeric vector (single spectrum) or matrix (rows = samples, columns = wavelengths). Column order must match wavelengths.

Details

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 [0, 1] before integration.

Value

A data.frame with columns X, Y, Z.

Examples

wl  <- seq(400, 700, by = 10)
ref <- rep(0.5, length(wl))
hs_to_xyz(wl, ref)


Convert sRGB to CIELAB

Description

Convert sRGB to CIELAB

Usage

rgb_to_lab(R, G, B, white = c(X = 95.047, Y = 100, Z = 108.883))

Arguments

R, G, B

Integer vectors (0-255).

white

Reference white point.

Value

A data.frame with numeric columns L, a, b.


Convert a data frame of spectra to CIELAB

Description

Convert a data frame of spectra to CIELAB

Usage

spectra_to_lab(
  df,
  id_col = NULL,
  na_action = c("zero", "drop", "keep"),
  white = c(X = 95.047, Y = 100, Z = 108.883)
)

Arguments

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 df. If NULL, the row names are used, or seq_len(nrow(df)) if row names are absent.

na_action

One of "zero" (default; replace non-finite with 0), "drop" (remove all-NA rows with a warning), or "keep" (preserve NA in the output).

white

Reference white point for XYZ -> sRGB.

Value

A data.frame with columns Sample_ID, L, a, b.


Convert a data frame of spectra to sRGB

Description

Convert a data frame of spectra to sRGB

Usage

spectra_to_rgb(
  df,
  id_col = NULL,
  na_action = c("zero", "drop", "keep"),
  white = c(X = 95.047, Y = 100, Z = 108.883)
)

Arguments

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 df. If NULL, the row names are used, or seq_len(nrow(df)) if row names are absent.

na_action

One of "zero" (default; replace non-finite with 0), "drop" (remove all-NA rows with a warning), or "keep" (preserve NA in the output).

white

Reference white point for XYZ -> sRGB.

Value

A data.frame with columns Sample_ID, R, G, B, hex.


Convert a data frame of spectra to sRGB and CIELAB

Description

Convert a data frame of spectra to sRGB and CIELAB

Usage

spectra_to_rgb_lab(
  df,
  id_col = NULL,
  na_action = c("zero", "drop", "keep"),
  white = c(X = 95.047, Y = 100, Z = 108.883)
)

Arguments

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 df. If NULL, the row names are used, or seq_len(nrow(df)) if row names are absent.

na_action

One of "zero" (default; replace non-finite with 0), "drop" (remove all-NA rows with a warning), or "keep" (preserve NA in the output).

white

Reference white point used for both the XYZ-to-sRGB and direct XYZ-to-CIELAB conversions. XYZ values and the white point use the same tristimulus scale.

Details

RGB and CIELAB coordinates are calculated independently from the same unquantized CIE XYZ tristimulus values. The CIELAB calculation does not pass through 8-bit sRGB values.

Value

A data.frame with columns Sample_ID, R, G, B, hex, L, a, b.


Convert CIE XYZ directly to CIELAB

Description

Converts CIE XYZ tristimulus values directly to CIELAB coordinates using the specified reference white. This calculation does not pass through sRGB and therefore avoids sRGB gamut clipping, gamma encoding, and 8-bit quantization.

Usage

xyz_to_lab(XYZ, white = c(X = 95.047, Y = 100, Z = 108.883))

Arguments

XYZ

A data frame with columns X, Y, and Z, or a numeric matrix with three columns. XYZ values and white must use the same scale.

white

Reference white point, expressed on the same scale as XYZ. Must contain three positive, finite values named X, Y, and Z. Defaults to D65 (c(X = 95.047, Y = 100, Z = 108.883)).

Details

The standard CIELAB piecewise function is applied to the normalized tristimulus values X/X_n, Y/Y_n, and Z/Z_n, where X_n, Y_n, and Z_n are the tristimulus values of the reference white.

Missing XYZ values propagate to the output coordinates that depend on them. Specifically, missing X affects a^*, missing Y affects all three coordinates, and missing Z affects b^*.

Value

A data frame with numeric columns L, a, and b.


Convert CIE XYZ to sRGB

Description

Convert CIE XYZ to sRGB

Usage

xyz_to_rgb(XYZ, white = c(X = 95.047, Y = 100, Z = 108.883))

Arguments

XYZ

A data.frame with columns X, Y, Z (as returned by hs_to_xyz), or a numeric matrix with 3 columns.

white

Reference white point. Defaults to D65 (c(X = 95.047, Y = 100, Z = 108.883)).

Value

A data.frame with integer columns R, G, B (0-255) and character column hex (e.g. "#654909").