Colour Science for Python
repository·develop·Indexed 25 days ago
https://github.com/colour-science/colourA comprehensive Python library for colour science providing algorithms and datasets for colorimetry, color appearance models, and color spaces. It supports n-dimensional arrays via NumPy for efficient batch and image processing, and includes tools for spectral distributions, chromatic adaptation (including CMCCAT2000, Fairchild, and CIE 1994), and domain-range scale management.
What's inside colour-science
- Colour is an open-source Python package that provides a comprehensive collection of algorithms and datasets specifically designed for colour science. It is an affiliated project of NumFOCUS and is licensed under the BSD-3-Clause terms.
Use the colour.continuous module for continuous signals
developThe
colour.continuousmodule provides tools for working with continuous signals. It includes classes for representing continuous functions and multi-signal data structures. Key components include:AbstractContinuousFunction: An abstract base class for continuous functions.Signal: A class representing a continuous signal.MultiSignals: A class for handling multiple signals simultaneously.
Understand Colour API object categorisation
developThe Colour API uses common prefixes to bundle related objects, which facilitates easier introspection and auto-completion in environments like IPython or Jupyter.
Key categorisation patterns include:
- Spectral Distribution functions: Prefixed with
sd_(e.g.,colour.sd_blackbody()). - Spectral Distribution attributes/methods: Grouped under the
colour.SDnamespace (e.g.,SD_GAUSSIAN_METHODS,SDS_ILLUMINANTS). - RGB Colourspaces: Accessed via the
colour.modelsnamespace with the prefixRGB_COLOURSPACE_(e.g.,colour.models.RGB_COLOURSPACE_BT709).
- Spectral Distribution functions: Prefixed with
Explore the Colour sub-packages
developThe
colourlibrary is organized into specialized sub-packages. Most of the public API is accessible directly from the rootcolournamespace, but specific domains are grouped into sub-packages such as:adaptation: Chromatic adaptation models.appearance: Colour appearance models.blindness: Colour vision deficiency models.characterisation: Colour correction and camera/display characterisation.colorimetry: Core objects for colour computations (e.g., spectral distributions).difference: Colour difference computations.io: Input/output for reading and writing data.models: Colour models (e.g., Lab, Luv, LCH).notation: Colour notation systems.plotting: Diagrams and figures.temperature: Colour temperature computations.
You can access the public API of the root namespace or specific sub-packages using standard Python imports.
Understand Spectral Distributions and Continuous Signals
developIn Colour, spectral distributions are represented by the
colour.SpectralDistributionclass (or its subclasses). These are instances of thecolour.continuous.Signalclass, which implements an interpolating function. This allows you to evaluate the spectral distribution at any given wavelength within its domain, even if that wavelength was not part of the original discrete data.Multi-spectral distributions are handled by the
colour.MultiSpectralDistributionsclass, which acts as a container for multiplecolour.continuous.Signalinstances.Initialize plotting styles with colour.plotting
developMost plotting functions are available in the
colour.plottingnamespace. You can usecolour_style()to apply the default colour science plotting styles to your environment.from colour.plotting import * colour_style()Modify SpectralDistribution values
developThe
.wavelengthsand.valuesproperties return copies of the underlying discretized data. Modifying these properties directly (e.g.,sd.values[0] = 0) will not update the actualSpectralDistributionobject.To update the values, you must re-assign the modified array back to the property.
import colour data = { 500: 0.0651, 520: 0.0705, 540: 0.0772, 560: 0.0870, 580: 0.1128, 600: 0.1360, } sd = colour.SpectralDistribution(data) # INCORRECT: This does not change the distribution sd.values[0] = 0 # CORRECT: Re-assign the modified array vals = sd.values vals[0] = 0 sd.values = valsVisualize colour conversion paths with colour.graph
developThecolour.graphmodule provides tools to visualize how colours are converted between different spaces. You can usecolour.graph.conversion_pathto generate a graph representing the sequence of conversions required to move from one colour space to another.Access interactive tutorial via Google Colab
developAn interactive version of the Colour tutorial is available via Google Colab for hands-on experimentation without local setup.Locate local examples directory
developA directory containing various examples is included with thecolourinstallation at the pathcolour/examples. You can also view these examples on GitHub.Convert CIE XYZ to Chromaticity Coordinates
developTo compute CIE xy chromaticity coordinates from CIE XYZ values, use
colour.XYZ_to_xy(XYZ). This returns a 2-element array[x, y].# Computing *CIE xy* chromaticity coordinates for the *neutral 5 (.70 D)* patch. xy = colour.XYZ_to_xy(XYZ) print(xy)Install Colour via pip or conda
developYou can install the core Colour package and its primary dependencies using
piporconda.Using pip:
pip install --user colour-scienceUsing conda:
conda install -c conda-forge colour-scienceFor detailed installation procedures regarding secondary dependencies, refer to the official Installation Guide.