CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-colour-science

Comprehensive Python library providing algorithms and datasets for colour science computations, including chromatic adaptation, colour appearance models, colorimetry, and spectral analysis.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

notation.mddocs/

Colour Notation Systems

Colour notation systems including Munsell Color System, hexadecimal color codes, CSS color names, and conversions between different color representation formats.

Capabilities

Munsell Color System

The Munsell Color System provides a standardized way to describe colors using hue, value (lightness), and chroma (saturation) dimensions.

def munsell_value(Y: ArrayLike, method: str = "ASTM D1535", **kwargs) -> NDArray:
    """
    Calculate Munsell value from relative luminance.
    
    Parameters:
    - Y: relative luminance values (0-100 scale)
    - method: computation method
        - "ASTM D1535" (default): ASTM D1535-08e1 method
        - "Priest 1920": Priest, Gibson, and McNicholas (1920)
        - "Munsell 1933": Munsell, Sloan, and Godlove (1933)
        - "Moon 1943": Moon and Spencer (1943)
        - "Saunderson 1944": Saunderson and Milner (1944)
        - "Ladd 1955": Ladd and Pinney (1955)
        - "McCamy 1987": McCamy (1987)
    
    Returns:
    Munsell value (lightness) on 0-10 scale
    """

def munsell_colour_to_xyY(specification: str) -> NDArray:
    """
    Convert Munsell color specification to CIE xyY chromaticity coordinates.
    
    Parameters:
    - specification: Munsell specification string (e.g., "N 5/", "5R 5/14")
    
    Returns:
    CIE xyY chromaticity coordinates as ndarray shape (3,)
    """

def xyY_to_munsell_colour(xyY: ArrayLike) -> str:
    """
    Convert CIE xyY chromaticity coordinates to Munsell color specification.
    
    Parameters:
    - xyY: CIE xyY chromaticity coordinates
    
    Returns:
    Munsell specification string
    """

# Method collection for Munsell value computation
MUNSELL_VALUE_METHODS: Dict[str, Callable]

Hexadecimal Color Codes

Functions for converting between RGB color values and hexadecimal color codes.

def RGB_to_HEX(RGB: ArrayLike) -> str:
    """
    Convert RGB color values to hexadecimal color code.
    
    Parameters:
    - RGB: RGB color values in 0-1 range as ndarray shape (3,)
    
    Returns:
    Hexadecimal color code string (e.g., "#FF5733")
    """

def HEX_to_RGB(HEX: str) -> NDArray:
    """
    Convert hexadecimal color code to RGB color values.
    
    Parameters:
    - HEX: hexadecimal color code string (e.g., "#FF5733" or "FF5733")
    
    Returns:
    RGB color values in 0-1 range as ndarray shape (3,)
    """

CSS Color Names

CSS Color Module Level 3 color name support for web-compatible color specifications.

def keyword_to_RGB_CSSColor3(keyword: str) -> NDArray:
    """
    Convert CSS Color Module Level 3 color keyword to RGB values.
    
    Parameters:
    - keyword: CSS color keyword (e.g., "red", "blue", "darkslategray")
    
    Returns:
    RGB color values in 0-1 range as ndarray shape (3,)
    """

Color Datasets

Predefined color collections for reference and lookup operations.

# Munsell color datasets
MUNSELL_COLOURS_ALL: Dict[str, NDArray]  # Complete Munsell color collection
MUNSELL_COLOURS_1929: Dict[str, NDArray]  # Munsell 1929 colors
MUNSELL_COLOURS_REAL: Dict[str, NDArray]  # Real Munsell colors subset
MUNSELL_COLOURS: Dict[str, NDArray]  # Standard Munsell colors (alias for REAL)

# CSS color datasets  
CSS_COLOR_3_BASIC: Dict[str, NDArray]  # 16 basic CSS colors
CSS_COLOR_3_EXTENDED: Dict[str, NDArray]  # Extended CSS color keywords
CSS_COLOR_3: Dict[str, NDArray]  # Complete CSS Color 3 specification

Usage Examples

Munsell Color System

import colour
import numpy as np

# Calculate Munsell value from luminance
Y = 18.0  # 18% reflectance
munsell_value = colour.munsell_value(Y, method="ASTM D1535")
print(f"Munsell value: {munsell_value:.2f}")

# Convert Munsell specification to xyY
munsell_spec = "5R 5/14"  # 5 Red, value 5, chroma 14
xyY = colour.munsell_colour_to_xyY(munsell_spec)
print(f"xyY coordinates: {xyY}")

# Convert xyY back to Munsell
recovered_spec = colour.xyY_to_munsell_colour(xyY)
print(f"Recovered Munsell: {recovered_spec}")

Hexadecimal Colors

import colour
import numpy as np

# Convert RGB to hexadecimal
RGB = np.array([1.0, 0.341, 0.2])  # Orange color
hex_code = colour.RGB_to_HEX(RGB)
print(f"Hex code: {hex_code}")  # #FF5733

# Convert hexadecimal to RGB
RGB_recovered = colour.HEX_to_RGB("#FF5733")
print(f"RGB values: {RGB_recovered}")

CSS Color Keywords

import colour

# Convert CSS color names to RGB
red_rgb = colour.keyword_to_RGB_CSSColor3("red")
blue_rgb = colour.keyword_to_RGB_CSSColor3("blue")
custom_rgb = colour.keyword_to_RGB_CSSColor3("darkslategray")

print(f"Red RGB: {red_rgb}")
print(f"Blue RGB: {blue_rgb}")
print(f"Dark Slate Gray RGB: {custom_rgb}")

Working with Color Datasets

import colour

# Access Munsell color data
munsell_colors = colour.MUNSELL_COLOURS
print(f"Available Munsell colors: {len(munsell_colors)}")

# Access specific Munsell color
if "5R 5/14" in munsell_colors:
    xyY = munsell_colors["5R 5/14"] 
    print(f"5R 5/14 xyY: {xyY}")

# Access CSS colors
css_colors = colour.CSS_COLOR_3
print(f"Available CSS colors: {len(css_colors)}")
print(f"CSS red: {css_colors['red']}")

Types

from colour.hints import ArrayLike, NDArray, Dict, Callable
from typing import Dict, Callable

# Type definitions for color datasets
ColorDataset = Dict[str, NDArray]
MethodCollection = Dict[str, Callable]

Imports

# Munsell system
from colour.notation import (
    munsell_value, munsell_colour_to_xyY, xyY_to_munsell_colour,
    MUNSELL_VALUE_METHODS, MUNSELL_COLOURS, MUNSELL_COLOURS_ALL
)

# Hexadecimal colors
from colour.notation import RGB_to_HEX, HEX_to_RGB

# CSS colors
from colour.notation import keyword_to_RGB_CSSColor3, CSS_COLOR_3

# Alternative imports from main package
from colour import (
    munsell_value, munsell_colour_to_xyY, xyY_to_munsell_colour,
    RGB_to_HEX, HEX_to_RGB, keyword_to_RGB_CSSColor3
)

Install with Tessl CLI

npx tessl i tessl/pypi-colour-science

docs

advanced-features.md

chromatic-adaptation.md

colorimetry.md

colour-appearance.md

colour-difference.md

colour-models.md

constants.md

geometry.md

index.md

input-output.md

math-utilities.md

notation.md

plotting.md

quality-assessment.md

temperature.md

tile.json