Colour Science for Python

repository·develop·Indexed 25 days ago

https://github.com/colour-science/colour

A 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.

Tokens
28.8K
Snippets
78
Records
264
Agent score
82%

What's inside colour-science

  1. Overview of the Colour package

    develop
    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.
  2. Use the colour.continuous module for continuous signals

    develop

    The colour.continuous module 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.
  3. Understand Colour API object categorisation

    develop

    The 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.SD namespace (e.g., SD_GAUSSIAN_METHODS, SDS_ILLUMINANTS).
    • RGB Colourspaces: Accessed via the colour.models namespace with the prefix RGB_COLOURSPACE_ (e.g., colour.models.RGB_COLOURSPACE_BT709).
  4. Explore the Colour sub-packages

    develop

    The colour library is organized into specialized sub-packages. Most of the public API is accessible directly from the root colour namespace, 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.

  5. Understand Spectral Distributions and Continuous Signals

    develop

    In Colour, spectral distributions are represented by the colour.SpectralDistribution class (or its subclasses). These are instances of the colour.continuous.Signal class, 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.MultiSpectralDistributions class, which acts as a container for multiple colour.continuous.Signal instances.

  6. Modify SpectralDistribution values

    develop

    The .wavelengths and .values properties return copies of the underlying discretized data. Modifying these properties directly (e.g., sd.values[0] = 0) will not update the actual SpectralDistribution object.

    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 = vals
  7. Visualize colour conversion paths with colour.graph

    develop
    The colour.graph module provides tools to visualize how colours are converted between different spaces. You can use colour.graph.conversion_path to generate a graph representing the sequence of conversions required to move from one colour space to another.
  8. Convert CIE XYZ to Chromaticity Coordinates

    develop

    To 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)
  9. Install Colour via pip or conda

    develop

    You can install the core Colour package and its primary dependencies using pip or conda.

    Using pip:

    pip install --user colour-science

    Using conda:

    conda install -c conda-forge colour-science

    For detailed installation procedures regarding secondary dependencies, refer to the official Installation Guide.