Getting Started

This tutorial demonstrates the main features of qualpal for generating and working with color palettes.

Installation

Qualpal is on PyPi and can be installed via pip:

pip install qualpal

If you want visualization support (requires matplotlib), install with:

pip install qualpal[viz]

Generating Palettes

The main API entry point is the Qualpal class, which generates color palettes through the generate method.

from qualpal import Qualpal

qp = Qualpal()

palette = qp.generate(6)

Here, we have generated a palette with 6 distinct colors. The palette class supports a rich HTML representation in Jupyter notebooks, so printing it will show the colors visually:

palette
#1c3600
#ff9400
#01ff45
#02c1ff
#020058
#ff70ee

Customizing the Color Space

By default, qualpal generates colors across the full HSL color space, but you can restrict the hue, saturation, and lightness ranges to create specific styles of palettes.

qp_pastel = Qualpal(
    colorspace={
        'h': (0, 360),   # Full hue range
        's': (0.3, 0.6), # Low saturation
        'l': (0.7, 0.9)  # High lightness
    }
)

qp_pastel.generate(5)
#e0b885
#85d5e1
#8fe085
#8585e0
#f5d6ee

Or create a palette with only warm colors:

qp_warm = Qualpal(
    colorspace={
        'h': (-20, 60),
        's': (0.5, 1.0),
        'l': (0.4, 0.7)
    }
)

qp_warm.generate(5)
#ffff01
#a72534
#db8aa5
#ff6500
#998833

As you can see, negative hue values wrap around, so -20 corresponds to 340 degrees.

Alternative Inputs

Qualpal supports two other modes of input: a list of candidate colors to choose from or one of the built-in color palettes that are available. To use candidate colors, provide a list of hex color strings:

candidates = ["#e6194b", "#3cb44b", "#ffe119", "#4363d8",
              "#f58231", "#911eb4", "#46f0f0", "#f032e6",
              "#bcf60c", "#fabebe", "#008080", "#e6beff",
              "#9a6324", "#fffac8", "#800000", "#aaffc3",
              "#808000", "#ffd8b1", "#000075", "#808080"]

qp_candidates = Qualpal(colors=candidates)
qp_candidates.generate(3)
#000075
#e6beff
#bcf60c

You can also use built-in palettes by name:

qp_builtin = Qualpal(palette="Tableau:10")
qp_builtin.generate(5)
#4e79a7
#edc948
#59a14f
#ff9da7
#9c755f

For a list of available built-in palettes, you can call list_palettes(), which returns a dictionary of palette names and their descriptions.

from qualpal import list_palettes, Palette

available_palettes = list_palettes()
available_palettes["Rembrandt"] # Example built-in palette
['AnatomyLesson', 'Staalmeesters']
from qualpal import get_palette

get_palette("Rembrandt:Staalmeesters")
#a13826
#701b06
#4c3114
#7a491e
#d7e1d6
#060a0d
#d39c7b

As you can see from above, built-in palettes are specified using the format Domain:Name, so that Rembrandt:Staalmeesters refers to the “Staalmeesters” palette from the “Rembrandt” collection.

Working with Palettes

Palettes are represented by the Palette class, which supports indexing, iteration, and length retrieval.

from qualpal import Palette

# Create palette from hex colors
pal = Palette(["#ff0000", "#00ff00", "#0000ff", "#ffff00"])

# Access colors by index
first_color = pal[0]
print(f"First color: {first_color.hex()}")

# Iterate over colors
print("\nAll colors:")
for i, color in enumerate(pal):
    print(f"  {i}: {color.hex()}")

# Get length
print(f"\nPalette size: {len(pal)}")
First color: #ff0000

All colors:
  0: #ff0000
  1: #00ff00
  2: #0000ff
  3: #ffff00

Palette size: 4

Indexing a single color returns a Color object, which supports various color operations.

Palette Analysis

Qualpal also provides methods to analyze palettes, such as measuring the perceptual distances between colors.

# Minimum pairwise distance
min_dist = pal.min_distance()
print(f"Minimum distance: {min_dist:.2f}")

# Distance to each nearest neighbor
min_dists = pal.min_distances()
print(f"Distances: {[f'{d:.2f}' for d in min_dists]}")

# Full distance matrix
matrix = pal.distance_matrix()
print(f"\nDistance matrix shape: {len(matrix)}x{len(matrix[0])}")
print(f"Distance from color 0 to color 1: {matrix[0][1]:.2f}")

matrix
Minimum distance: 23.40
Distances: ['52.88', '23.40', '52.88', '23.40']

Distance matrix shape: 4x4
Distance from color 0 to color 1: 86.61
[[0.0, 86.60823745353736, 52.88136803127365, 64.30086090099863],
 [86.60823745353736, 0.0, 83.18587765548577, 23.404244339728187],
 [52.88136803127365, 83.18587765548577, 0.0, 103.42697889277265],
 [64.30086090099863, 23.404244339728187, 103.42697889277265, 0.0]]

Exporting Palettes

Qualpal supports exporting palettes in various formats. For example, CSS variables:

pal.to_css(prefix="brand")
['--brand-1: #ff0000;',
 '--brand-2: #00ff00;',
 '--brand-3: #0000ff;',
 '--brand-4: #ffff00;']

There is also support for JSON export:

import json
json_str = pal.to_json()
print(f"JSON: {json_str}")

# Can be used in configs
config = {
    "theme": {
        "colors": json.loads(json_str)
    }
}
print(f"\nConfig: {config}")
JSON: ["#ff0000", "#00ff00", "#0000ff", "#ffff00"]

Config: {'theme': {'colors': ['#ff0000', '#00ff00', '#0000ff', '#ffff00']}}

CVD-Aware Palette Generation

You can generate palettes that are optimized for viewers with color vision deficiencies (CVD). To do this, provide a cvd dictionary specifying the type and severity of CVD to account for.

Here, we generate a palette that is safe for deuteranomaly (the most common form of CVD):

qp_cvd = Qualpal(
    cvd={'deutan': 0.7}  # 70% severity deuteranomaly
)

qp_cvd.generate(5)
#ffff1d
#2e0018
#3b7800
#5b59ff
#ffe2ff

Account for Background Color

When generating palettes, you can also specify a background color to ensure good contrast. This is especially useful for generating palettes for dark backgrounds. To do so, provide a background hex color string when creating the Qualpal instance.

Here, we create a palette optimized for dark backgrounds:

qp_dark = Qualpal(background="#1a1a1a")
qp_dark.generate(4)
#ff43ff
#ff8502
#64ff00
#01c6ff

Compare this to a palette optimized for light backgrounds:

qp_light = Qualpal(background="#ffffff")
qp_light.generate(4)
#745cff
#0d0000
#f40001
#018500

White Point Configuration

The white point defines the reference illuminant used for color space conversions. Different white points are used in different contexts - D65 is standard for web/sRGB, while D50 is common in printing and graphics. Qualpal supports several CIE standard illuminants.

# Default: D65 (standard web/sRGB)
qp_web = Qualpal()
web_palette = qp_web.generate(5)
print("D65 (web/sRGB):")
web_palette
D65 (web/sRGB):
#004500
#ff6019
#c8ff00
#03c6ff
#24003a
# D50: Common in printing industry
qp_print = Qualpal(white_point='d50')
print_palette = qp_print.generate(5)
print("D50 (printing):")
print_palette
D50 (printing):
#2d4400
#160050
#ff3f19
#f5ff00
#03c9ff

Available white points:

  • d65: Daylight 6500K (default, sRGB/web standard)

  • d50: Daylight 5000K (printing/graphics industry)

  • d55: Daylight 5500K

  • a: Incandescent tungsten 2856K

  • e: Equal energy (theoretical reference)

The choice of white point affects color appearance and should match your target medium. For web applications, use D65 (default). For print work, D50 is typically appropriate.

Complete Example: Accessible Data Visualization Palette

Let’s create a complete palette suitable for accessible data visualization:

# Requirements:
# - 8 distinct colors
# - Work on white background
# - Safe for deuteranomaly (most common CVD)
# - Avoid very light or very dark colors

qp_accessible = Qualpal(
    colorspace={
        'h': (0, 360),
        's': (0.4, 0.9),
        'l': (0.3, 0.7)
    },
    background="#ffffff",
    cvd={'deutan': 0.5}
)

accessible_palette = qp_accessible.generate(8)

print("Accessible visualization palette:")
for i, color in enumerate(accessible_palette, 1):
    rgb = color.rgb255()
    print(f"  {i}. {color.hex()}  RGB{rgb}")

print(f"\nMinimum distance: {accessible_palette.min_distance():.2f}")
print("(Higher is better - minimum recommended: 30)")

accessible_palette
Accessible visualization palette:
  1. #1513f3  RGB(21, 19, 243)
  2. #2e6b59  RGB(46, 107, 89)
  3. #0cb1d5  RGB(12, 177, 213)
  4. #e8480c  RGB(232, 72, 12)
  5. #7f1a32  RGB(127, 26, 50)
  6. #f65c9c  RGB(246, 92, 156)
  7. #0cd87f  RGB(12, 216, 127)
  8. #dcc60c  RGB(220, 198, 12)

Minimum distance: 31.22
(Higher is better - minimum recommended: 30)
#1513f3
#2e6b59
#0cb1d5
#e8480c
#7f1a32
#f65c9c
#0cd87f
#dcc60c

Visualization

If you have matplotlib installed, you can visualize palettes using the show method, which returns a figure with color swatches.

# Display color swatches
fig = pal.show()
_images/fd3e073ea1d851e4167026d7ae1b9b9390249e4c06bfad41e1cf5ad15816ca9d.png

If you like, you can add labels of the hex codes to the swatches:

fig = pal.show(labels=True)
_images/7f25209c88fe9d26a98063053226aef9d2c505ab631202195d07d92715757588.png

You can also provide custom labels:

# With custom labels
fig = pal.show(labels=["Primary", "Success", "Info", "Warning"])
_images/76671b5f51886cada8fc9234ad5f89eadb6f0f8a31eb7a1b1ebab05a706abdb7.png

The figures that are returned are matplotlib Figure objects and can be further customized or saved:

fig.savefig("palette.png", bbox_inches="tight")

For more details, see the API documentation.