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_hex(rgb)[source]

Converts an RGB tuple to a hex color string.

Parameters:

rgb (Tuple[int, int, int]) – Tuple of (R, G, B) where each component is 0-255.

Return type:

str

Returns:

Hex color string in the format “#RRGGBB”.

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.

Parameters:

rgb (Tuple[int, int, int])

Return type:

Tuple[float, float, float]

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

Parameters:

xyz (Tuple[float, float, float])

Return type:

Tuple[float, float, float]

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.

Parameters:

rgb (Tuple[int, int, int])

Return type:

Tuple[float, float, float]

color_tools.conversions.lab_to_xyz(lab)[source]

Convert CIE L*a*b* to XYZ using D65 illuminant.

Reverses the LAB → XYZ transformation.

Parameters:

lab (Tuple[float, float, float])

Return type:

Tuple[float, float, float]

color_tools.conversions.xyz_to_rgb(xyz, clamp=True)[source]

Convert CIE XYZ to sRGB (0-255).

Parameters:
  • xyz (Tuple[float, float, float]) – XYZ color tuple

  • clamp (bool) – If True, clamp out-of-gamut values to 0-255. If False, may return values outside valid range (useful for gamut checking).

Return type:

Tuple[int, int, int]

Returns:

RGB tuple (0-255)

color_tools.conversions.lab_to_rgb(lab, clamp=True)[source]

Convert CIE L*a*b* to sRGB (0-255).

Parameters:
  • lab (Tuple[float, float, float]) – L*a*b* color tuple

  • clamp (bool) – If True, clamp out-of-gamut colors to valid RGB range

Return type:

Tuple[int, int, int]

Returns:

RGB tuple (0-255)

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

Return type:

Tuple[float, float, float]

Returns:

(L*, C*, h°) tuple

Parameters:

lab (Tuple[float, float, float])

color_tools.conversions.lch_to_lab(lch)[source]

Convert L*C*h° to L*a*b*.

Parameters:

lch (Tuple[float, float, float]) – (L*, C*, h°) tuple

Return type:

Tuple[float, float, float]

Returns:

(L*, a*, b*) tuple

color_tools.conversions.lch_to_rgb(lch, clamp=True)[source]

Convert L*C*h° directly to sRGB (0-255).

Parameters:
Return type:

Tuple[int, int, int]

color_tools.conversions.rgb_to_lch(rgb)[source]

Convert sRGB (0-255) directly to L*C*h°.

Parameters:

rgb (Tuple[int, int, int])

Return type:

Tuple[float, float, float]

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)

Parameters:

rgb (Tuple[int, int, int])

Return type:

Tuple[float, float, float]

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.

Parameters:

rgb (Tuple[int, int, int])

Return type:

Tuple[int, int, int]

color_tools.conversions.hsl_to_rgb(hsl)[source]

Convert HSL (H: 0-360, S: 0-100, L: 0-100) to RGB (0-255).

This is the inverse of rgb_to_hsl(), converting from the standard HSL representation back to 8-bit RGB values.

Parameters:

hsl (Tuple[float, float, float]) – HSL tuple (Hue 0-360°, Saturation 0-100%, Lightness 0-100%)

Return type:

Tuple[int, int, int]

Returns:

RGB tuple (0-255 for each component)