Package {spectra2rgb}


Type: Package
Title: Convert Spectral Signatures to sRGB and CIELAB Colour Coordinates
Version: 0.1.0
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.
License: GPL (≥ 3)
Encoding: UTF-8
Depends: R (≥ 4.0)
Imports: stats, grDevices, utils
Suggests: testthat (≥ 3.0.0), readxl, knitr, rmarkdown
VignetteBuilder: knitr
Config/roxygen2/version: 8.1.0
NeedsCompilation: no
Packaged: 2026-10-08 13:48:37 UTC; jhohan.elguera
Author: Jhohan Elguera [aut, cre]
Maintainer: Jhohan Elguera <jelguerat@gmail.com>
Repository: CRAN
Date/Publication: 2026-10-20 14:30: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 for XYZ -> sRGB.

Value

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


Convert CIE XYZ to CIELAB

Description

Convenience wrapper: XYZ -> sRGB -> CIELAB. Uses the sRGB path so the result matches what rgb_to_lab produces from the same XYZ.

Usage

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

Arguments

XYZ

A data.frame with columns X, Y, Z, or a numeric matrix with 3 columns.

white

Reference white point.

Value

A data.frame with columns L, a, 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").