Individual entry · report and code

Computer colour matching: K-M single constant and multi-start search

2023 Huashu Cup, problem B · colour matching for opaque plastics

From K/S spectra to CIELAB difference, turning “match this colour” into a constrained search. The repository fixes the missing XYZ normalisation in the original report, uses the measured K/S curves as the forward model, and combines multi-start sampling with coordinate descent for Q2 recipes, Q3 cost and Q4 minimum colorants.

Aug 2023 Python · NumPy · Colour science · Optimisation

The brief

The problem gives K/S values for three colorants (red, yellow, blue) at eight concentrations from 0.05 % to 5 %, sampled every 20 nm from 400 to 700 nm, plus the reflectance of ten target samples. Four questions follow:

  1. fit K/S against concentration at every wavelength;
  2. return ten different recipes per target with ΔE < 1;
  3. add cost control for a 2 kg batch;
  4. use as few colorants as possible while keeping ΔE < 1.

From colour to K/S

Each measured reflectance becomes K/S = (1 - R)² / (2R). A linear fit K/S = a·c + b is then computed per colorant and wavelength. Across the 48 fits the mean R² is 0.9965 and the minimum is 0.9258 (red at 700 nm).

Red at 400 nm: R² = 0.994, but the low-concentration points sit above the line. That bend matters for the forward model.
Red at 400 nm: R² = 0.994, but the low-concentration points sit above the line. That bend matters for the forward model.

The first bug in the original report

Turning reflectance into XYZ requires a normalisation:

X = k · Σ S(λ)x̄(λ)R(λ)Δλ, with k = 100 / Σ S(λ)ȳ(λ)Δλ

The first submission integrated with Simpson and multiplied by 0.1, omitting k. XYZ was roughly ten times too large, so L* exceeded 100 — impossible in CIELAB. This repository applies the normalisation; the ten targets now have L* between 71 and 86.

The ten targets in the a*b* plane; colour encodes L*. Samples 1 and 6 are red, 4, 5, 9 and 10 are blue-green.
The ten targets in the a*b* plane; colour encodes L*. Samples 1 and 6 are red, 4, 5, 9 and 10 are blue-green.

Why the forward model uses the measured curves

Using only the slope of the fit plus the substrate K/S underestimates the low-concentration end: red at 400 nm measures 0.637 at c = 0.05 %, while base + slope × 0.05 gives 0.075. The fitted intercept is not the substrate; the curve bends.

The optimiser therefore interpolates the measured K/S curves directly:

K/S_mix = Σ K/S_i(c_i) − (n − 1)·K/S_base

For one colorant this reproduces the measurement exactly; for mixtures it assumes K/S additivity. It is not perfect, but it follows the data far better than the linear fit.

Three concentrations map to three Lab components, so the ΔE < 1 region is small and irregular. The pipeline is:

  1. log-uniform random starts (1e-5–5 %) plus sparse one- and two-colorant seeds;
  2. batch evaluation of ΔE, keeping feasible starts;
  3. coordinate descent on the best seeds, with the step halved from 0.05;
  4. a diversity filter in concentration space so the ten recipes are genuinely different.

Q2 ranks by ΔE, Q3 by 2 kg cost among ΔE < 1 candidates, and Q4 sweeps one, two and three colorants, keeping the first feasible count.

Best ΔE per target on a log scale. Eight samples reach the 1e-4 level; sample 1 is search-limited and sample 6 is unreachable with this model.
Best ΔE per target on a log scale. Eight samples reach the 1e-4 level; sample 1 is search-limited and sample 6 is unreachable with this model.

Results

  • Q2: 80 of 82 returned recipes meet ΔE < 1; eight of the ten targets return ten distinct recipes.
  • Q3: 79/82 feasible; the cheapest 2 kg recipe costs 0.31 CNY of colorant.
  • Q4: the first five targets need three colorants; two-color recipes are infeasible under this model.

What I took away

  • Fix the units before optimising. A missing normalisation makes every colour difference meaningless, and the result can still look plausible.
  • Model choice beats algorithm choice. The linear fit has a beautiful R² but is wrong at low concentration; the measured curves made feasible recipes appear.
  • Random plus local search beats a grid when the feasible region spans six orders of magnitude.
  • Report the failures. Sample 6 never reaches ΔE < 1; the code and CSV keep feasible=False instead of widening the threshold.

Code, data, result tables and the original report are in the repository. The narrative version is in this post.

Technical notes

01 From K/S to a recipe: notes on a colour-matching project K-M single constant, the CIELAB normalisation, why low concentrations break a linear fit, and how multi-start search returns ten different recipes.

Related skills

  • Python
  • NumPy
  • Colour science
  • Optimisation