{"id":7099,"library":"colorzero","title":"Colorzero","description":"Colorzero is a Python library designed for simple and 'pythonic' color manipulation. It features a comprehensive `Color` class capable of converting between various color representations and calculating color differences. The library utilizes immutable `Color` objects, which are descendents of `namedtuple`, necessitating a specific approach to color manipulation. The current version is 2.0, last released in March 2021.","status":"active","version":"2.0","language":"en","source_language":"en","source_url":"https://github.com/waveform80/colorzero","tags":["color","colour","color manipulation","rgb","hls","hsv","cie lab","css colors"],"install":[{"cmd":"pip install colorzero","lang":"bash","label":"Install stable release"}],"dependencies":[],"imports":[{"symbol":"Color","correct":"from colorzero import Color"},{"note":"These classes are used for manipulating immutable Color objects.","symbol":"Red, Green, Blue, Hue, Lightness, Saturation","correct":"from colorzero import Red, Green, Blue"}],"quickstart":{"code":"from colorzero import Color, Green, Lightness\n\n# Create a color from various inputs\nred = Color('red')\nblue = Color(0, 0, 1) # RGB floats (0.0-1.0)\nmagenta_html = Color('#ff00ff')\n\n# Color objects are immutable; operations return new objects\nc = Color('yellow')\nprint(f\"Initial color: {c}\")\n\n# Add green component\nc_more_green = c + Green(0.1)\nprint(f\"After adding green: {c_more_green}\")\n\n# Reduce lightness by multiplying\nc_darker = c_more_green * Lightness(0.5)\nprint(f\"After making darker: {c_darker}\")\n\n# Convert to different representations\nprint(f\"HTML: {c_darker.html}\")\nprint(f\"RGB bytes: {c_darker.rgb_bytes}\")\nprint(f\"HLS: {c_darker.hls}\")","lang":"python","description":"Demonstrates creating `Color` objects using various constructors and manipulating them by producing new instances through arithmetic operations with manipulation classes."},"warnings":[{"fix":"Instead of direct assignment, use arithmetic operators with specific manipulation classes (e.g., `my_color = my_color + Red(0.1)`).","message":"Color objects are immutable (tuple descendents). You cannot directly modify their attributes (e.g., `my_color.red = 0.5`). Operations on a color return a *new* Color instance.","severity":"gotcha","affected_versions":"2.0 and earlier"},{"fix":"To change color components, use helper classes like `Red`, `Green`, `Blue`, `Lightness`, etc., in conjunction with arithmetic operators, which return a new `Color` object. For example: `c = c + Green(0.1)` or `c = c * Lightness(0.5)`.","message":"Direct manipulation of color components via attribute assignment will raise an `AttributeError`. For example, `c.red = 0.5` will fail.","severity":"gotcha","affected_versions":"2.0 and earlier"},{"fix":"Be aware of potential minor discrepancies when converting colors between different representations, especially when high precision is critical.","message":"Some conversions between color spaces may result in a certain amount of precision loss.","severity":"gotcha","affected_versions":"2.0 and earlier"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"To change a color, create a new `Color` instance by using arithmetic operators with manipulation classes (e.g., `from colorzero import Red; my_color = my_color + Red(0.1)`).","cause":"Attempting to modify a color component directly on an existing `Color` object (e.g., `my_color.red = 0.5`). `Color` instances are immutable.","error":"AttributeError: can't set attribute"},{"fix":"Refer to the `Color` class documentation for accepted constructor arguments and valid ranges for color components (e.g., RGB floats between 0.0 and 1.0, or byte values between 0 and 255).","cause":"The `Color` constructor was called with parameters that do not conform to any of its supported variants (e.g., invalid string format for a named color or hex code, or out-of-range numerical values for components).","error":"ValueError: ..."}]}