Qualpal
Repositories

qualpal::Qualpal

C++ · cpp · 4.1.0

class qualpal::Qualpal

Builder for qualitative color palette generation.

The Qualpal class allows step-by-step configuration of palette generation options. Users can set the input source (RGB, hex, palette name, or HSL colorspace), color vision deficiency simulation parameters, background color, color difference metric, and memory limit. The palette is generated by calling generate().

Methods

Qualpal

Qualpal() = default

Default constructor. Initializes with no input source.

extend

std::vector<colors::RGB> extend(const std::vector<colors::RGB> & palette, std::size_t n)

Extend an existing palette by adding n new colors.

Parameters

palette

Existing palette (RGB colors) to keep fixed.

n

Total size of the new palette to generate, including existing colors. In other words, the new palette will have n colors.

Returns

Vector of palette + n new RGB colors.

generate

std::vector<colors::RGB> generate(std::size_t n)

Generate a qualitative color palette with the configured options.

Parameters

n

Number of colors to generate.

Returns

Vector of n selected RGB colors, each channel in [0, 1].

Throws

std::runtime_error

if no input source is configured.

std::invalid_argument

for invalid configuration.

setBackground

Qualpal & setBackground(const colors::RGB & bg_color)

Set the background color for palette generation.

Parameters

bg_color

RGB background color.

Returns

Reference to this object for chaining.

setColorspaceSize

Qualpal & setColorspaceSize(std::size_t n_points)

Set the number of points in the colorspace grid for HSL and LCHab input.

Parameters

n_points

Number of points to sample in the colorspace grid.

Returns

Reference to this object for chaining.

Throws

std::invalid_argument

if n_points <= 0.

setCvd

Qualpal & setCvd(const std::map<std::string, double> & cvd_params)

Set color vision deficiency simulation parameters.

Parameters

cvd_params

Map of CVD type to severity, e.g., {{"protan", 0.5}, {"deutan", 0.2}}. Valid keys: "protan", "deutan", "tritan". Severity must be in [0, 1].

Returns

Reference to this object for chaining.

Throws

std::invalid_argument

if an invalid key or out-of-range severity is provided.

setInputColorspace

Qualpal & setInputColorspace(const std::array<double, 2> & h_lim, const std::array<double, 2> & s_or_c_lim, const std::array<double, 2> & l_lim, ColorspaceType space = ColorspaceType::HSL)

Set input colors by sampling a colorspace (HSL or LCHab).

Parameters

h_lim

Hue range in degrees [-360, 360].

s_or_c_lim

Saturation or Chroma (depending on ColorspaceType) range [0, 1] or >= 0.

l_lim

Lightness range [0, 1] or [0, 100].

space

Colorspace type

Returns

Reference to this object for chaining.

Throws

std::invalid_argument

for invalid ranges.

setInputColorspaceRegions

Qualpal & setInputColorspaceRegions(const std::vector<ColorspaceRegion> & regions, ColorspaceType space = ColorspaceType::HSL)

Set input colors by sampling multiple regions in a colorspace.

Parameters

regions

Vector of colorspace regions to sample from. Regions may overlap, which will increase sampling density in overlapping areas.

space

Colorspace type (HSL or LCHab)

Returns

Reference to this object for chaining.

Throws

std::invalid_argument

if regions is empty or any region has invalid ranges.

// Sample from warm and cool colors only
qualpal::Qualpal qp;
qp.setInputColorspaceRegions({
  {{0, 60}, {0.5, 1.0}, {0.3, 0.7}},    // Reds/oranges
  {{180, 240}, {0.5, 1.0}, {0.3, 0.7}}  // Cyans/blues
}, qualpal::ColorspaceType::HSL);
auto palette = qp.generate(8);

setInputHex

Qualpal & setInputHex(const std::vector<std::string> & colors)

Set input colors from a vector of hex color strings.

Parameters

colors

Vector of hex color strings (e.g., "#ff0000").

Returns

Reference to this object for chaining.

Throws

std::invalid_argument

if any hex string is invalid.

setInputPalette

Qualpal & setInputPalette(const std::string & palette_name)

Set input colors from a named palette.

Parameters

palette_name

Palette name in the format "Package:Palette".

Returns

Reference to this object for chaining.

Throws

std::invalid_argument

if the palette name is invalid.

setInputRGB

Qualpal & setInputRGB(const std::vector<colors::RGB> & colors)

Set input colors from a vector of RGB values.

Parameters

colors

Vector of RGB colors to use as input. Each channel should be in [0, 1].

Returns

Reference to this object for chaining.

setMemoryLimit

Qualpal & setMemoryLimit(double gb)

Set the maximum memory limit (in GB) for color difference matrix.

Parameters

gb

Memory limit in gigabytes.

Returns

Reference to this object for chaining.

Throws

std::invalid_argument

if gb <= 0.

setMetric

Qualpal & setMetric(metrics::MetricType metric)

Set the color difference metric to use.

Parameters

metric

Metric type (e.g., DIN99d).

Returns

Reference to this object for chaining.

setRefinementStarts

Qualpal & setRefinementStarts(int n_starts)

Configure the post-discrete continuous refinement step.

Experimental. When n_starts > 0, generate() runs continuous local refinement after the discrete farthest-points selection, shifting each non-fixed palette point in CIE L*a*b* to maximize its minimum CIEDE2000 distance to the others. With n_starts > 1, the refinement is run from additional random seeds (in-region, in-gamut) and the best palette is kept — this escapes basins where the discrete warm start was suboptimal.

Refinement only takes effect when the input source is a colorspace region (HSL or LCHab); for fixed input sets (RGB/hex/named palettes) the output must come from the input set, so refinement is skipped.

Parameters

n_starts

0 disables refinement; 1 single-start (warm start from the discrete selection); ≥2 multi-start (warm start + random extras). Default is 5.

Returns

Reference to this object for chaining.

Throws

std::invalid_argument

if n_starts is negative.

setWhitePoint

setWhitePoint

Qualpal & setWhitePoint(WhitePoint wp)

Set the reference white point for color conversions.

Parameters

wp

White point enum value (D65, D50, D55, A, or E).

Returns

Reference to this object for chaining.

setWhitePoint

Qualpal & setWhitePoint(const std::array<double, 3> & white_point)

Set the reference white point using custom XYZ values.

Parameters

white_point

Custom XYZ tristimulus values for the white point.

Returns

Reference to this object for chaining.