Advanced color manipulation library for gradients and conversions
Project description
Chromatica
Project Scope
Chromatica is designed for developers, designers, and data visualization specialists who need:
- Precise color conversions between multiple color spaces with support for different numeric formats (int, float, percentage)
- Complex gradient generation including 1D, 2D, radial, and angular-radial patterns with advanced interpolation controls
- Hue-aware interpolation for smooth color transitions in HSV/HSL spaces with clockwise/counterclockwise direction control
- Array-based operations for efficient batch processing of colors using NumPy
- Type safety with dedicated color classes for each color space and format
- Flexible format handling supporting integer (0-255), float (0.0-1.0), and percentage representations
- Animation support for creating smooth color transitions and dynamic gradient effects
Key Features
Color Spaces & Formats
- Supported color spaces: RGB, RGBA, HSV, HSVA, HSL, HSLA, CMYK, grayscale, and palette modes
- Multiple format types: Integer (0-255), Float (0.0-1.0), and percentage representations
- Alpha channel support: Full alpha transparency handling across all compatible color spaces
- CSS Color 4 compatibility: Optional CSS-compliant algorithms for web-standard conversions
Gradient Generation
- 1D gradients: Linear color interpolation with customizable steps and easing functions
- 2D gradients: Bilinear interpolation from four corner colors
- Radial gradients: Circular gradients radiating from a center point with configurable radius
- Angular-radial gradients: Advanced polar coordinate gradients with separate angular and radial interpolation
- Simple angular-radial: Two-color gradients with angular and radial variation
- Full parametrical: Multi-ring gradients with per-ring color control and custom easing functions
- Hue interpolation: Intelligent hue path selection (shortest, clockwise, or counterclockwise) for smooth color transitions
- Masking & bounds: Configurable angular and radial masks for partial gradient coverage
- Outside fill: Custom colors for regions outside the gradient boundaries
Color Manipulation
- Typed color classes: Dedicated classes for each color space with automatic value clamping
- Arithmetic operations: Add, subtract, multiply, and divide colors with automatic range handling
- Format conversion: Seamless conversion between integer, float, and percentage formats
- Space conversion: Convert between any supported color spaces with high precision
- Array operations: Batch processing of color arrays with NumPy efficiency
- Channel access: Direct manipulation of individual color channels (hue, saturation, brightness, etc.)
Utility Functions
- Value clamping: Keep values within valid ranges
- Cyclic wrapping: Handle hue wraparound for seamless color wheel navigation
- Bounce wrapping: Reflect values that exceed boundaries
- Easing functions: Apply custom interpolation curves to gradients
- Coordinate transforms: Advanced spatial transformations for complex gradient patterns
Use Cases
Chromatica is ideal for:
- Data Visualization: Create color scales and gradients for heatmaps, charts, and scientific visualizations
- Image Processing: Manipulate image colors, apply gradients, and perform color space conversions
- UI/UX Design: Generate color palettes, themes, and dynamic color schemes
- Animation & Motion Graphics: Create smooth color transitions and animated gradients
- Game Development: Generate procedural textures, environmental effects, and dynamic color themes
- Web Development: Produce CSS-compatible color values and gradient backgrounds
- Art & Creative Coding: Experiment with color theory and create generative art
- Scientific Computing: Perform precise color space transformations for research applications
Installation
Chromatica targets Python 3.8+. To install from source:
pip install .
For development (with testing tools):
pip install -e .[dev]
Quick start
Color conversion
Use the conversion wrapper to move between color spaces. You can choose integer (0–255), float (0–1), or percentage formats and opt into the CSS algorithm when needed.
from chromatica import convert
# Integer RGB to HSV (analytical algorithm)
rgb = (255, 128, 64)
h, s, v = convert(rgb, from_space="rgb", to_space="hsv", input_type="int", output_type="int")
# HSV back to RGB using CSS Color 4 math
converted_rgb = convert((h, s, v), from_space="hsv", to_space="rgb", use_css_algo=True)
Working with color objects
Color classes clamp inputs to valid ranges and expose helpers for channel access and conversion.
from chromatica import ColorRGB, ColorHSV
sunset = ColorRGB((255, 128, 64))
print(sunset.unit_values) # normalized tuple
hsv = ColorHSV((30, 50, 100))
print(hsv.to_rgb())
Building gradients
Create smooth gradients in the color space of your choice. Hue-based spaces support clockwise/counterclockwise interpolation.
from chromatica import Gradient1D, Gradient2D
from chromatica.conversions.format_type import FormatType
# 1D HSV gradient, forcing clockwise hue rotation
strip = Gradient1D.from_colors((0, 100, 100), (300, 100, 100), steps=32, color_space="hsv", format_type=FormatType.INT, direction="cw")
# 2D RGB gradient from four corner colors
canvas = Gradient2D.from_colors((255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), width=256, height=256)
array = canvas.value # NumPy array of shape (H, W, channels)
Utility helpers
Clamp, wrap, and bounce numeric values when preparing inputs or post-processing channels.
from chromatica.functions import clamp, bounce, cyclic_wrap_float
clamped = clamp(300, 0, 255)
looped_hue = cyclic_wrap_float(390.0, 0.0, 360.0)
runtime_safe = bounce(-10, 0, 100)
Running tests
Chromatica includes pytest-based coverage. From the repository root:
pytest
This exercises conversion math, utility functions, and gradient helpers.
Examples
Runnable examples live in the examples/ folder:
examples/basic_usage.pydemonstrates typed colors, conversions, and 1D/2D gradients.examples/array_operations.pyshows array-backed gradients, hue wrapping, and simple tiling.
Run either script directly with Python, e.g.:
python examples/basic_usage.py
Advanced Features
Angular-Radial Gradients
Create sophisticated polar coordinate gradients with independent control over angular and radial interpolation:
from chromatica.gradients.simple_angular_radial import SimpleAngularRadialGradient
from chromatica.format_type import FormatType
# Create a gradient that varies both angularly and radially
gradient = SimpleAngularRadialGradient.generate(
width=500,
height=500,
radius=200,
inner_ring_colors=(
(0, 255, 255), # Cyan at start angle
(120, 255, 255) # Green at end angle
),
outer_ring_colors=(
(240, 255, 255), # Blue at start angle
(300, 255, 255) # Magenta at end angle
),
color_space='hsv',
format_type=FormatType.INT,
deg_start=0.0,
deg_end=360.0,
radius_start=0.3,
radius_end=1.0,
hue_direction_theta='cw', # Clockwise hue interpolation
outside_fill=(0, 0, 0)
)
Hue Direction Control
When working with HSV/HSL color spaces, control how hues interpolate:
from chromatica import Gradient1D
from chromatica.format_type import FormatType
# Shortest path (default) - goes through the closest colors
gradient_short = Gradient1D.from_colors(
(350, 100, 100), (10, 100, 100),
steps=20, color_space='hsv', direction=None
)
# Clockwise - forces interpolation through increasing hue values
gradient_cw = Gradient1D.from_colors(
(350, 100, 100), (10, 100, 100),
steps=20, color_space='hsv', direction='cw'
)
# Counter-clockwise - forces interpolation through decreasing hue values
gradient_ccw = Gradient1D.from_colors(
(350, 100, 100), (10, 100, 100),
steps=20, color_space='hsv', direction='ccw'
)
Custom Easing Functions
Apply easing functions to control interpolation curves:
import numpy as np
from chromatica import Gradient1D
# Ease-in-out cubic
def ease_in_out_cubic(t):
return np.where(t < 0.5, 4 * t**3, 1 - (-2 * t + 2)**3 / 2)
gradient = Gradient1D.from_colors(
(255, 0, 0), (0, 0, 255),
steps=50,
color_space='rgb',
unit_transform=ease_in_out_cubic
)
Array-Based Color Operations
Efficiently process multiple colors using NumPy arrays:
from chromatica import ColorRGBArr
import numpy as np
# Create an array of colors
colors = ColorRGBArr(np.array([
[255, 0, 0],
[0, 255, 0],
[0, 0, 255]
]))
# Convert all colors to HSV
hsv_colors = colors.convert('hsv')
# Perform arithmetic operations
brightened = colors * 1.2 # Increase brightness
mixed = colors + ColorRGB((50, 50, 50)) # Add to each color
Contributing
Contributions are welcome! Please feel free to submit pull requests, report bugs, or suggest new features through the GitHub issues page.
License
Chromatica is released under the MIT License. See the LICENSE file for details.
Acknowledgments
This library builds upon established color science principles and aims to provide a robust, performant, and user-friendly interface for color manipulation in Python.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file chromatica-1.0.3.tar.gz.
File metadata
- Download URL: chromatica-1.0.3.tar.gz
- Upload date:
- Size: 60.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
546185838e0f651e05257f43641cc89e580228cc4302674e81094f79ba55775e
|
|
| MD5 |
b0aa5c391a12fc83b9562aafb7449e84
|
|
| BLAKE2b-256 |
34ee679afc13ac32e58b8ce22610013c6e0f15ead21d4cab71f2501ece520e28
|
File details
Details for the file chromatica-1.0.3-py3-none-any.whl.
File metadata
- Download URL: chromatica-1.0.3-py3-none-any.whl
- Upload date:
- Size: 68.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61f150a51e14efc2dbe7bd7d0cec40f7752400dcc5f487d02ce9ec7cfab00aa7
|
|
| MD5 |
bc39e84f152fadb59593bded5f892fb5
|
|
| BLAKE2b-256 |
5bcfd0a37db3ed9aebd1786ac2818c9e7769affcbb792b725158566c0b0bf5c2
|