color_tools.conversions
Color space conversion functions.
Handles conversions between: - sRGB (0-255) ↔ XYZ ↔ LAB ↔ LCH - sRGB ↔ HSL (various formats) - Gamma correction (sRGB companding)
All conversions use D65 illuminant and proper color science math.
Example
>>> from color_tools import rgb_to_lab, lab_to_lch, rgb_to_hsl
>>>
>>> # Convert vibrant orange from web color
>>> orange_rgb = (255, 128, 0)
>>>
>>> # To LAB for perceptual analysis
>>> lab = rgb_to_lab(orange_rgb)
>>> print(f"LAB: L={lab[0]:.1f} a={lab[1]:.1f} b={lab[2]:.1f}")
LAB: L=67.8 a=43.4 b=78.0
>>>
>>> # To LCH for hue/chroma work
>>> lch = lab_to_lch(lab)
>>> print(f"LCH: L={lch[0]:.1f} C={lch[1]:.1f} H={lch[2]:.1f}°")
LCH: L=67.8 C=88.6 H=60.9°
>>>
>>> # To HSL for web design
>>> hsl = rgb_to_hsl(orange_rgb)
>>> print(f"HSL: {hsl[0]:.0f}° {hsl[1]:.0f}% {hsl[2]:.0f}%")
HSL: 30° 100% 50%
- color_tools.conversions.hex_to_rgb(hex_code)[source]
Converts a hex color string to an RGB tuple.
- Parameters:
hex_code (
str) – Hex color string in the format “#RGB”, “RGB”, “#RRGGBB” or “RRGGBB”. 3-character codes are expanded (e.g., “#24c” -> “#2244cc”).- Returns:
Tuple of (R, G, B) where each component is 0-255. None if the hex code is invalid.
- Return type:
rgb
- color_tools.conversions.rgb_to_xyz(rgb)[source]
Convert sRGB (0-255) to CIE XYZ using D65 illuminant.
XYZ is a device-independent color space that represents how the human eye responds to light. It’s the bridge between RGB and LAB.
- color_tools.conversions.xyz_to_lab(xyz)[source]
Convert CIE XYZ to L*a*b* using D65 illuminant.
LAB is perceptually uniform - equal distances in LAB space correspond to roughly equal perceived color differences. Perfect for color matching!
L*: Lightness (0=black, 100=white)
a*: Green←→Red axis
b*: Blue←→Yellow axis
- color_tools.conversions.rgb_to_lab(rgb)[source]
Convert sRGB (0-255) to CIE L*a*b*.
This is the main conversion you’ll use for color matching! Goes RGB → XYZ → LAB in one shot.
- color_tools.conversions.lab_to_xyz(lab)[source]
Convert CIE L*a*b* to XYZ using D65 illuminant.
Reverses the LAB → XYZ transformation.
- color_tools.conversions.lab_to_lch(lab)[source]
Convert L*a*b* to L*C*h° (cylindrical LAB).
LCH is more intuitive than LAB for certain operations: - L* (Lightness): Same as LAB, 0-100 - C* (Chroma): Color intensity/saturation, sqrt(a² + b²) - h° (Hue): Hue angle in degrees, 0-360
- color_tools.conversions.lch_to_rgb(lch, clamp=True)[source]
Convert L*C*h° directly to sRGB (0-255).
- color_tools.conversions.rgb_to_hsl(rgb)[source]
Convert RGB (0-255) to HSL (H: 0-360, S: 0-100, L: 0-100).
This is the standard HSL representation: - H: Hue in degrees (0° = red, 120° = green, 240° = blue) - S: Saturation as percentage (0% = gray, 100% = pure color) - L: Lightness as percentage (0% = black, 50% = pure color, 100% = white)
- color_tools.conversions.rgb_to_winhsl(rgb)[source]
Convert RGB (0-255) to Windows HSL (all components 0-240).
Windows HSL is used in Win32 COLORREF and some GDI APIs. Each component is scaled to the 0-240 range instead of the usual H:0-360, S:0-100, L:0-100 representation.