Color Transformations & Distance
Blend modes
Multiply
Darkens and mutes a color (result = base × factor):
base_color = HSLColor.from_abs_hsla(200, 80, 60)
# 80% of original saturation, 50% of original lightness
darker = base_color.multiply_components(0.8, 0.5)
Screen
Lightens and brightens a color (result = 1 − (1 − base) × (1 − factor)):
Lightness inversion
Inverts lightness while preserving hue and saturation:
dark_color = HSLColor.from_abs_hsla(0, 80, 30)
light_color = dark_color.invert_lightness() # lightness becomes 70%
Contrasting color selection
Pick whichever of two colors contrasts better with the current color. Useful for choosing readable text colors on colored backgrounds:
background = HSLColor.from_abs_hsla(200, 70, 50)
dark_text = HSLColor.from_abs_hsla(0, 0, 10)
light_text = HSLColor.from_abs_hsla(0, 0, 95)
text_color = background.pick_contrasting_color(dark_text, light_text, method='auto')
Available methods:
| Method | Description |
|---|---|
'distance' |
Uses CIE76 Delta E in LAB color space |
'lightness' |
Simple lightness comparison |
'auto' |
Intelligent selection (recommended) |
Perceptual distance
Colors can be compared using the CIE76 Delta E metric in the perceptually uniform CIELAB color space, which accounts for human perception more accurately than RGB Euclidean distance:
color1 = HSLColor.from_hex('#ff0000')
color2 = HSLColor.from_hex('#00ff00')
distance_sq = color1.squared_distance(color2) # faster; use for comparisons
distance = color1.distance(color2) # Euclidean distance in LAB space
Finding the closest color
The comparison uses LAB color space distance for perceptual accuracy.