Colors¶
Qualpal features a full-fledged color class for working with colors, measuring perceptual distances, and simulating color vision deficiencies.
Basic Color Operations¶
Let’s start by working with individual colors:
from qualpal import Color, Palette
# Create a color from hex
red = Color("#ff0000")
print(f"Hex: {red.hex()}")
print(f"RGB: {red.rgb()}")
print(f"RGB (0-255): {red.rgb255()}")
print(f"HSL: {red.hsl()}")
red
Hex: #ff0000
RGB: (1.0, 0.0, 0.0)
RGB (0-255): (255, 0, 0)
HSL: (0.0, 1.0, 0.5)
#ff0000As you can see, the Color class supports multiple color formats and
provides easy conversion between them. It also has a rich HTML
representation for Jupyter notebooks.
Creating Colors from Different Formats¶
Colors can be created from various formats:
# From RGB (0-1 range)
green = Color.from_rgb(0.0, 1.0, 0.0)
# From HSL
blue_hsl = Color.from_hsl(240, 1.0, 0.5)
Palette([red, green, blue_hsl])
And as you can see above, these colors can be combined into a
Palette for easy visualization.
Color Distance¶
A key feature of Qualpal is measuring perceptual color differences, which is used in the palette generation algorithm. You can compute the distance between two colors like this:
color1 = Color("#ff0000")
color2 = Color("#ff6600")
color1.distance(color2)
13.659111166211344
Color Vision Deficiency (CVD) Simulation¶
We also expose methods to simulate how colors appear to individuals with different types of color vision deficiencies:
original = Color("#ff0000")
# Simulate different types of CVD
protan = original.simulate_cvd("protan", severity=1.0)
deutan = original.simulate_cvd("deutan", severity=1.0)
tritan = original.simulate_cvd("tritan", severity=1.0)
Palette([original, protan, deutan, tritan])
For more details, see the API documentation.