Skip to main content

license Binder PyPI version

lambda2color: Wavelength-to-RGB Conversion

A lightweight Python library to convert light wavelengths (in nanometers) into corresponding RGB colors using the CIE 1931 XYZ color space.

Features

  • Convert wavelengths (380–780 nm) to RGB values.
  • Based on the CIE 1931 color matching functions.
  • Supports custom illuminants and color spaces.

Installation

pip install lambda2color

Usage

from lambda2color import Lambda2color, xyz_from_xy

# Define a standard white illuminant (D65)
illuminant_D65 = xyz_from_xy(0.3127, 0.3291)

# Initialize color space conversion for sRGB
cs_srgb = Lambda2color(
    red=xyz_from_xy(0.64, 0.33),
    green=xyz_from_xy(0.30, 0.60),
    blue=xyz_from_xy(0.15, 0.06),
    white=illuminant_D65
)

# Convert wavelengths to RGB
wavelengths = cs_srgb.cmf[1:, 0]
N_wavelengths = len(wavelengths)
hues = np.zeros((N_wavelengths, 3))
for i_wavelength in range(N_wavelengths):
    spec = np.zeros((N_wavelengths+1))
    spec[i_wavelength] = 1
    hues[i_wavelength, :] = cs_srgb.spec_to_rgb(spec)

# give for S, M and L cones' peak sensitivities
for spec, color in [(445, 'blue'), (555, 'green'), (600, 'red')]:
  i_wavelength = np.argmin(np.abs(wavelengths - spec))
  print(f' RGB for the {color=} at wavelength {wavelengths[i_wavelength]} nm: {hues[i_wavelength, :]}')

Documentation

Visualizations

CIE Color Matching Functions

CIE colour matching function

Spectral Colors

Rainbow

Advanced Use Cases

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lambda2color-0.8.1.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lambda2color-0.8.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file lambda2color-0.8.1.tar.gz.

File metadata

  • Download URL: lambda2color-0.8.1.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.15

File hashes

Hashes for lambda2color-0.8.1.tar.gz
Algorithm Hash digest
SHA256 2eb92ec39d768703d8e7e5403bd3d12136379797e41e126848e4067150472dd9
MD5 a06dec16c0174ff25da68b35532b20e2
BLAKE2b-256 0fbd2401dff5208adb5cf3d5fff11a857b34ced9c7f64f46c994a24d2459ff74

See more details on using hashes here.

File details

Details for the file lambda2color-0.8.1-py3-none-any.whl.

File metadata

  • Download URL: lambda2color-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.15

File hashes

Hashes for lambda2color-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 33f50fd72b614ca328d9a979cc173add46651b993585e035aee18bf4c03b26fd
MD5 e5eb5236821e2c0d8c37f980b4830212
BLAKE2b-256 70c969690adb44d9c4fee0088a80bed7d91f70a354715dc06107f7a1bfb7fc22

See more details on using hashes here.

Release history Release notifications | RSS feed

0.8.2

2 files

This release

0.8.1 This release

2 files

0.8

2 files

0.7

1 file

0.6

1 file

0.5

1 file

0.4

1 file

0.3

1 file

0.2

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page