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.tar.gz (5.7 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-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lambda2color-0.8.tar.gz
  • Upload date:
  • Size: 5.7 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.tar.gz
Algorithm Hash digest
SHA256 34c5a0091af9e9be68dc22ee5d8ad1f5e152b73e5f7d70b7e8e33a4d2ae6b22f
MD5 1feb0a5f1a2f9b0b6cac309351334858
BLAKE2b-256 67f050b1b2814ed1c855cb9776a630b887f74bd79ea5a4739b7c458c405a7365

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lambda2color-0.8-py3-none-any.whl
  • Upload date:
  • Size: 5.8 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-py3-none-any.whl
Algorithm Hash digest
SHA256 eb00d310a67292c1957d5340af704919702597694a986a5340974b5b07c79ea8
MD5 acf41175c129a08fbddb7ba6c112fd60
BLAKE2b-256 896df04917df7c1c73c0c16613d22ec13f30b47bb0dd445c7670a3c02210be41

See more details on using hashes here.

Release history Release notifications | RSS feed

0.8.2

2 files

0.8.1

2 files

This release

0.8 This release

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