Convert spectrum or wavelength to color.
$ npm install color-spectrum
import toColor from 'color-spectrum';
import fft from 'fourier-transform';
let magnitudes = fft(myData);
document.body.style.background = toColor(magnitudes);Calculate color based off spectrum or a single wavelength value. Spectrum is a list of float values, like magnitudes or intensities, for example, a direct output from fourier-transform with real numbers. The visible range 380..780nm will be stretched to cover the passed list of intensities.
Possible options:
// Use approximation formulas or matching table interpolation
approximate: false,
// Normalize spectrum
normalize: true,
// Reference whitepoint with x,y,z values, CIE 1931 2° D65 by default
whitepoint: [95.045592705167, 100, 108.9057750759878]Technical methods:
import toColor, { spectrum, wavelength } from 'color-spectrum';
//get xyz values array for a given wavelength from 380..780 range. Use 1/λ to calc frequency.
let [x, y, z] = wavelength(λ, opts?);
//get xyz values for a list of magnitudes
let [x, y, z] = spectrum(list, opts);
//transform to rgb
import xyz from 'color-space/xyz.js';
let [r, g, b] = xyz.rgb(x, y, z);import table from 'color-spectrum/table' — CIE 1931 2° wavelength → XYZ matching table, 380..780nm every 5nm.
import approx from 'color-spectrum/approx' — analytic XYZ approximation (Wyman, Sloan & Shirley 2013).
- Rendering of Spectra
- Wavelength to color
- Color science
- Analytical Approximation of Color Matching Functions
Thanks to all the color scientists, who devoted their lives to color research and delivered their knowledge to us, for we can trust them and use their formulas and their code. Seriously, this package is just a complement to their work in js/npm format, no new scientific knowledge, just science a bit closer to practice.
gl-waveform — color-spectrum used to paint waveform color based of spectral contents.
color-space — color space conversions.
color-interpolate — interpolate color between values.