spectra2rgb converts hyperspectral reflectance
signatures to sRGB hex codes and CIELAB coordinates. It bundles the CIE
1931 standard observer and D65 illuminant, so no external colour-science
package is required.
set.seed(1)
wl <- seq(400, 700, by = 10)
mat <- matrix(runif(5 * length(wl), 0.1, 0.9), nrow = 5)
colnames(mat) <- as.character(wl)
df <- as.data.frame(mat)
spectra_to_rgb_lab(df, id_col = paste0("leaf_", 1:5))
#> Sample_ID R G B hex L a b
#> 1 leaf_1 208 180 193 #D0B4C1 75.94563 5.473204 -7.532564
#> 2 leaf_2 189 195 167 #BDC3A7 77.70915 -13.652753 9.003911
#> 3 leaf_3 176 177 186 #B0B1BA 72.32000 -4.948629 -9.435452
#> 4 leaf_4 233 175 176 #E9AFB0 76.85865 14.004340 3.584069
#> 5 leaf_5 213 199 179 #D5C7B3 80.92846 -5.719798 7.240532df2 <- df
df2[2, ] <- NA
# Silent zero-fill (default):
spectra_to_rgb(df2, na_action = "zero")
#> Sample_ID R G B hex
#> 1 1 208 180 193 #D0B4C1
#> 2 2 0 0 0 #000000
#> 3 3 176 177 186 #B0B1BA
#> 4 4 233 175 176 #E9AFB0
#> 5 5 213 199 179 #D5C7B3
# Drop with warning:
suppressWarnings(spectra_to_rgb(df2, na_action = "drop"))
#> Sample_ID R G B hex
#> 1 1 208 180 193 #D0B4C1
#> 2 3 176 177 186 #B0B1BA
#> 3 4 233 175 176 #E9AFB0
#> 4 5 213 199 179 #D5C7B3
# Preserve NA:
spectra_to_rgb(df2, na_action = "keep")
#> Sample_ID R G B hex
#> 1 1 208 180 193 #D0B4C1
#> 2 2 NA NA NA <NA>
#> 3 3 176 177 186 #B0B1BA
#> 4 4 233 175 176 #E9AFB0
#> 5 5 213 199 179 #D5C7B3