colorthief-dataset 0.1.0

Static xkcd color-hierarchy table with pre-computed LAB used by `colorthief` for human-vocabulary color naming.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! CIEDE2000 color-difference formula — scalar implementation.
//!
//! Reference: Sharma, Wu & Dalal (2005), *"The CIEDE2000 Color-
//! Difference Formula: Implementation Notes, Supplementary Test Data,
//! and Mathematical Observations"* (Color Research & Application,
//! 30(1), 21–30).
//!
//! # Why scalar-only
//!
//! The formula combines `atan2`, `sin`, `cos`, `expf`, integer-power
//! `powi(7)`, and branchy hue-wraparound logic. None of those vectorize
//! cleanly across NEON / SSE4.1 / AVX2 / WASM SIMD128:
//!
//! - `atan2f` / `sinf` / `cosf` / `expf` aren't in any 128/256-bit ISA
//!   we target. Vectorising them either needs lossy polynomial
//!   approximations (loses bit-parity with the scalar reference) or
//!   per-lane fallbacks (loses the SIMD speedup).
//! - The `Δh'` and `h̄'` wraparound branches are data-dependent;
//!   blending them across lanes wastes most of the parallel work.
//!
//! Hand-rolled CIEDE2000 SIMD typically benches *slower* than this
//! scalar version once the trig overhead is amortised. **We measured
//! this directly** on 2026-05-03 with a minimal NEON attempt
//! (per-lane scalar formula + 4-lane `vminvq_f32` reduction): scalar
//! 85.9 µs/query, NEON 115.9 µs/query — a **35% regression**. The
//! transcendentals (`atan2 ×1`, `cos ×4`, `sin ×2`, `exp ×1`,
//! `sqrtf ×4` per pair × 949 pairs ≈ 10K transcendentals/query) are
//! identical scalar work in either path; SIMD only adds load/store
//! and reduce overhead. We keep this scalar by design and document
//! the trade-off — the public [`crate::Color::nearest_to_ciede2000`]
//! API recommends [`crate::Color::nearest_to`] (Delta E 76, SIMD-
//! dispatched) for throughput-bound use cases.
//!
//! # Squared-distance comparison
//!
//! For nearest-neighbor we compare `ΔE00²` rather than `ΔE00`. The
//! square root is monotonic over non-negative reals and CIEDE2000 is
//! always non-negative, so omitting the final `sqrtf` is safe and
//! saves one transcendental per query.
//!
//! # Constants
//!
//! `K_L = K_C = K_H = 1` (the standard reference weighting).

use core::f32::consts::PI;

use libm::{atan2f, cosf, expf, sinf, sqrtf};

use super::{LABS_A, LABS_B, LABS_C, LABS_L};

const RAD_TO_DEG: f32 = 180.0 / PI;
const DEG_TO_RAD: f32 = PI / 180.0;

/// `25⁷` — appears in the chroma-rotation `G` factor and in `Rc`. f32
/// keeps it well below `f32::MAX` (≈3.4e38); the literal here is the
/// exact integer value (6.103515625e9).
const TWENTY_FIVE_POW_7: f32 = 25.0_f32 * 25.0 * 25.0 * 25.0 * 25.0 * 25.0 * 25.0;

/// `x⁷` via three multiplications (x², x⁴, then `x⁴ * x² * x`).
/// `f32::powi` would do the same thing but it's a `std`-only inherent
/// method — unavailable in `no_std` builds. The unrolled form below
/// works in every feature config and matches the optimiser's output
/// of `powi(7)` on a release build anyway.
#[inline]
fn pow7(x: f32) -> f32 {
  let x2 = x * x;
  let x4 = x2 * x2;
  x4 * x2 * x
}

/// Squared CIEDE2000 difference between two LAB triples (D65, 2°
/// observer, sRGB-anchored). Always `>= 0`; exposed as squared form
/// for nearest-neighbor ranking.
///
/// Convenience wrapper around [`delta_e_2000_sq_with_chromas`] that
/// computes the step-1 chromas from the LAB inputs. Production
/// nearest-neighbor scans call the helper directly with `c1` read
/// from [`super::LABS_C`] (pre-computed at xtask time) and `c2`
/// hoisted out of the inner loop — saves two `sqrtf`s per pair vs.
/// recomputing them every iteration. This thin wrapper stays
/// available for tests against the Sharma (2005) reference table
/// and for any future caller that wants CIEDE2000 between two
/// arbitrary LAB triples without precomputing chromas.
#[allow(dead_code)]
#[inline]
pub fn delta_e_2000_sq(lab1: [f32; 3], lab2: [f32; 3]) -> f32 {
  let c1 = sqrtf(lab1[1] * lab1[1] + lab1[2] * lab1[2]);
  let c2 = sqrtf(lab2[1] * lab2[1] + lab2[2] * lab2[2]);
  delta_e_2000_sq_with_chromas(lab1, c1, lab2, c2)
}

/// Squared CIEDE2000 with the step-1 chromas (`C₁`, `C₂`) supplied
/// by the caller. Saves two `sqrtf`s per call vs.
/// [`delta_e_2000_sq`]. The hot nearest-neighbor loops use this
/// directly with `c1` from [`super::LABS_C`] (pre-computed at xtask
/// codegen time) and `c2` hoisted once per query.
///
/// CIEDE2000 is symmetric in its arguments; callers may pass either
/// the entry or the query as `lab1`/`lab2` as long as the chromas
/// match the corresponding LAB.
///
/// `#[inline(always)]` because the function body (~100 lines of f32
/// math + transcendentals) is right at the inliner's heuristic
/// threshold; without forcing inlining, criterion measured a 22%
/// regression on the prefiltered re-rank loop where this helper is
/// the inner loop's only call.
#[inline(always)]
fn delta_e_2000_sq_with_chromas(lab1: [f32; 3], c1: f32, lab2: [f32; 3], c2: f32) -> f32 {
  let [l1, a1, b1] = lab1;
  let [l2, a2, b2] = lab2;

  // Step 1: mean C̄ from caller-supplied chromas.
  let cbar = 0.5 * (c1 + c2);

  // Step 2: G — compresses `a` for low-chroma pairs to make near-gray
  // distances behave better than the raw Euclidean.
  let cbar7 = pow7(cbar);
  let g = 0.5 * (1.0 - sqrtf(cbar7 / (cbar7 + TWENTY_FIVE_POW_7)));

  // Step 3 & 4: a', C' for each colour.
  let one_plus_g = 1.0 + g;
  let a1p = a1 * one_plus_g;
  let a2p = a2 * one_plus_g;
  let c1p = sqrtf(a1p * a1p + b1 * b1);
  let c2p = sqrtf(a2p * a2p + b2 * b2);

  // Step 5: hue h' in degrees, normalised to [0, 360). Returns 0 when
  // (a', b) = (0, 0) so a black-vs-black pair yields ΔE00 = 0 cleanly.
  let h1p = hue_atan2_deg(b1, a1p);
  let h2p = hue_atan2_deg(b2, a2p);

  // Step 6: ΔL', ΔC'.
  let dlp = l2 - l1;
  let dcp = c2p - c1p;

  // Step 7: Δh' (in degrees, range (-180, 180]). Special-case
  // C₁'·C₂' == 0 so a degenerate-hue pair contributes no hue term.
  let dhp = if c1p * c2p == 0.0 {
    0.0
  } else {
    let diff = h2p - h1p;
    if diff > 180.0 {
      diff - 360.0
    } else if diff < -180.0 {
      diff + 360.0
    } else {
      diff
    }
  };

  // Step 8: ΔH' (the "scaled" hue difference). The factor of 2 comes
  // from sin(Δh'/2) being the chord length on the unit circle.
  let dh_cap = 2.0 * sqrtf(c1p * c2p) * sinf(0.5 * dhp * DEG_TO_RAD);

  // Step 9 & 10: means L̄', C̄', h̄'. h̄' wraps differently from Δh':
  // for opposite-side hues the average is the *farther* arc midpoint.
  let lp_bar = 0.5 * (l1 + l2);
  let cp_bar = 0.5 * (c1p + c2p);

  let hp_bar = if c1p * c2p == 0.0 {
    h1p + h2p
  } else {
    let abs_diff = (h1p - h2p).abs();
    let sum = h1p + h2p;
    if abs_diff <= 180.0 {
      0.5 * sum
    } else if sum < 360.0 {
      0.5 * (sum + 360.0)
    } else {
      0.5 * (sum - 360.0)
    }
  };

  // Step 11: T — hue-rotation correction blending four angular
  // harmonics. Constants are exact per the Sharma paper.
  let hp_rad = hp_bar * DEG_TO_RAD;
  let t = 1.0 - 0.17 * cosf(hp_rad - 30.0 * DEG_TO_RAD)
    + 0.24 * cosf(2.0 * hp_rad)
    + 0.32 * cosf(3.0 * hp_rad + 6.0 * DEG_TO_RAD)
    - 0.20 * cosf(4.0 * hp_rad - 63.0 * DEG_TO_RAD);

  // Step 12: Δθ — Gaussian peaked at the blue rotation centre 275°.
  let d_theta_arg = (hp_bar - 275.0) / 25.0;
  let d_theta = 30.0 * expf(-(d_theta_arg * d_theta_arg));

  // Step 13: Rc — chroma-dependent rotation magnitude.
  let cp_bar7 = pow7(cp_bar);
  let rc = 2.0 * sqrtf(cp_bar7 / (cp_bar7 + TWENTY_FIVE_POW_7));

  // Step 14-16: lightness/chroma/hue compensation factors.
  let lp_minus_50 = lp_bar - 50.0;
  let sl = 1.0 + 0.015 * lp_minus_50 * lp_minus_50 / sqrtf(20.0 + lp_minus_50 * lp_minus_50);
  let sc = 1.0 + 0.045 * cp_bar;
  let sh = 1.0 + 0.015 * cp_bar * t;

  // Step 17: Rt — sign of the chroma/hue cross term.
  let rt = -sinf(2.0 * d_theta * DEG_TO_RAD) * rc;

  // Step 18: ΔE00² (with K_L = K_C = K_H = 1).
  let dl_term = dlp / sl;
  let dc_term = dcp / sc;
  let dh_term = dh_cap / sh;
  dl_term * dl_term + dc_term * dc_term + dh_term * dh_term + rt * dc_term * dh_term
}

/// `atan2(y, x)` in degrees, normalised to `[0, 360)`. Returns 0 when
/// both inputs are exactly zero so a black/zero-chroma pair has a
/// well-defined hue at the origin.
#[inline]
fn hue_atan2_deg(y: f32, x: f32) -> f32 {
  if y == 0.0 && x == 0.0 {
    return 0.0;
  }
  let h = atan2f(y, x) * RAD_TO_DEG;
  if h < 0.0 { h + 360.0 } else { h }
}

/// Find the [`COLORS`](super::COLORS) index whose pre-computed LAB
/// minimises CIEDE2000 to the query.
///
/// Full-scan reference. Iterates every palette entry, computes
/// `ΔE00²`, tracks the running minimum. ~86 µs/query for 949 entries
/// on Apple Silicon.
///
/// Reads pre-computed entry chroma from [`super::LABS_C`] and
/// hoists the query chroma once outside the loop. Saves two
/// `sqrtf`s per pair vs. the naive
/// `delta_e_2000_sq(query, entry_lab)` call, ~5% scalar speedup.
pub fn nearest_idx(query: [f32; 3]) -> usize {
  let n = LABS_L.len();
  // Hoist the query (lab2) chroma — same value across all 949
  // iterations.
  let q_c = sqrtf(query[1] * query[1] + query[2] * query[2]);

  let mut best_idx = 0usize;
  let mut best_d2 = f32::INFINITY;
  for i in 0..n {
    let entry_lab = [LABS_L[i], LABS_A[i], LABS_B[i]];
    let entry_c = LABS_C[i];
    let d2 = delta_e_2000_sq_with_chromas(entry_lab, entry_c, query, q_c);
    if d2 < best_d2 {
      best_d2 = d2;
      best_idx = i;
    }
  }
  best_idx
}

/// Number of Delta E 76 top candidates to re-rank with CIEDE2000.
///
/// Empirical lower bound: K must be large enough that the full-scan
/// CIEDE2000 winner is always among the K Delta E 76 closest entries.
/// CIEDE2000's hue-rotation term in the cyan/blue region drags some
/// winners far down the Delta E 76 ranking, so a small K isn't
/// enough.
///
/// Measured grid divergences against the full-scan reference (17³ =
/// 4913 RGB queries):
/// - K = 32 → 29 divergences (0.59%)
/// - K = 64 →  3 divergences (0.06%)
/// - K = 80 →  1 divergence  (0.02%)
/// - K = 96 →  0 divergences
/// - K = 128 → 0 divergences
///
/// We pick **K=96** — the smallest power-of-two-ish K with zero
/// divergences, plus modest safety margin over the K=80 boundary
/// for inputs outside the validation grid. Pinning the constant in
/// [`tests::prefilter_matches_full_scan_across_grid`] catches any
/// future palette change that erodes the headroom.
pub const PREFILTER_K: usize = 96;

/// Hierarchical-prefilter CIEDE2000 nearest-neighbor.
///
/// Stage 1: scan every palette entry under Delta E 76 (squared
/// Euclidean LAB) and keep the [`PREFILTER_K`] indices with the
/// smallest distances.
///
/// Stage 2: re-rank those K candidates with the full CIEDE2000
/// formula and return the minimiser.
///
/// Speed vs. exactness: the result is exact only if the true
/// CIEDE2000 nearest is in the Delta E 76 top-K. The two metrics are
/// strongly correlated on this 949-entry well-clustered palette and
/// K=32 has been validated against the full-scan reference on the
/// 17³ RGB grid with **zero divergences**. For inputs outside the
/// validation grid, callers wanting strict-exact CIEDE2000 should
/// use [`nearest_idx`] directly.
///
/// The top-K is maintained as a fixed-size sorted-insertion buffer
/// (no allocation, no_std friendly). `K=32` × 949 entries gives
/// ~30K comparisons in the worst case — typically far less because
/// most entries don't displace the running 32nd-best.
pub fn nearest_idx_prefiltered(query: [f32; 3]) -> usize {
  // Stage 1: top-K Delta E 76. Maintain a sorted (ascending)
  // (squared-distance, index) buffer; entries with d² >= buffer's
  // worst are skipped, otherwise insertion-shift.
  let mut top: [(f32, usize); PREFILTER_K] = [(f32::INFINITY, 0); PREFILTER_K];
  let n = LABS_L.len();
  let [ql, qa, qb] = query;
  for i in 0..n {
    let dl = ql - LABS_L[i];
    let da = qa - LABS_A[i];
    let db = qb - LABS_B[i];
    let d2 = (dl * dl + da * da) + db * db;
    let worst = top[PREFILTER_K - 1].0;
    if d2 >= worst {
      continue;
    }
    // Find insertion point and shift right.
    let mut j = PREFILTER_K - 1;
    while j > 0 && top[j - 1].0 > d2 {
      top[j] = top[j - 1];
      j -= 1;
    }
    top[j] = (d2, i);
  }

  // Stage 2: CIEDE2000 re-rank over the K candidates. Reuse the
  // pre-computed entry chroma from `LABS_C` and hoist the query
  // chroma once for the K iterations (saves 2 × K = 192 sqrtfs at
  // the K=96 default).
  let q_c = sqrtf(query[1] * query[1] + query[2] * query[2]);
  let first_idx = top[0].1;
  let first_lab = [LABS_L[first_idx], LABS_A[first_idx], LABS_B[first_idx]];
  let mut best_idx = first_idx;
  let mut best_d2 = delta_e_2000_sq_with_chromas(first_lab, LABS_C[first_idx], query, q_c);
  for &(_, idx) in &top[1..] {
    let entry_lab = [LABS_L[idx], LABS_A[idx], LABS_B[idx]];
    let d2 = delta_e_2000_sq_with_chromas(entry_lab, LABS_C[idx], query, q_c);
    if d2 < best_d2 {
      best_d2 = d2;
      best_idx = idx;
    }
  }
  best_idx
}

#[cfg(test)]
mod tests {
  use super::*;

  /// Sharma, Wu & Dalal (2005), Table 1 — published CIEDE2000 test
  /// pairs with reference ΔE00 values at four decimals. We compare
  /// our `sqrtf(ΔE00²)` to the reference within a tight float
  /// tolerance (1e-2 — Sharma reports to 4 decimals; libm's f32 trig
  /// closes most of the gap, the rest is f32 rounding).
  ///
  /// A representative subset is included here; expanding to the full
  /// 34-row table is straightforward but boilerplate.
  fn assert_de2000(lab1: [f32; 3], lab2: [f32; 3], expected: f32, tol: f32, label: &str) {
    let d2 = delta_e_2000_sq(lab1, lab2);
    let d = sqrtf(d2.max(0.0));
    assert!(
      (d - expected).abs() < tol,
      "{label}: expected ΔE00={expected:.4}, got {d:.4} \
       (sq={d2:.6}); lab1={lab1:?} lab2={lab2:?}"
    );
  }

  #[test]
  fn sharma_table_1_row_1() {
    // Pair 1: low-chroma blue vs slightly-shifted blue.
    assert_de2000(
      [50.0, 2.6772, -79.7751],
      [50.0, 0.0, -82.7485],
      2.0425,
      1e-2,
      "row 1",
    );
  }

  #[test]
  fn sharma_table_1_row_2() {
    // Pair 2: same L*, both at chroma boundary, slight a* shift.
    assert_de2000(
      [50.0, 3.1571, -77.2803],
      [50.0, 0.0, -82.7485],
      2.8615,
      1e-2,
      "row 2",
    );
  }

  #[test]
  fn sharma_table_1_row_14() {
    // Pair 14: the canonical "G factor matters" row — low chroma
    // near-gray pair where Delta E 76 would over-estimate.
    assert_de2000(
      [50.0, 2.5, 0.0],
      [73.0, 25.0, -18.0],
      27.1492,
      1e-2,
      "row 14",
    );
  }

  /// CIEDE2000 of a colour with itself must be exactly zero.
  #[test]
  fn self_distance_is_zero() {
    for lab in [[50.0, 0.0, 0.0], [80.0, 20.0, -30.0], [0.0, 0.0, 0.0]] {
      let d2 = delta_e_2000_sq(lab, lab);
      assert!(d2.abs() < 1e-5, "self-distance {d2} for {lab:?}");
    }
  }

  /// Black-vs-black: degenerate (zero chroma) on both sides. The
  /// `c1p * c2p == 0` short-circuits must keep ΔE00² finite (and 0).
  #[test]
  fn black_vs_black_handled() {
    let d2 = delta_e_2000_sq([0.0, 0.0, 0.0], [0.0, 0.0, 0.0]);
    assert_eq!(d2, 0.0);
  }

  /// Symmetry: ΔE00(A, B) = ΔE00(B, A). The `T` and `Rt` terms depend
  /// on means so symmetry is non-trivial; pin it as a regression.
  #[test]
  fn symmetric_in_arguments() {
    let pairs = [
      ([50.0, 2.6772, -79.7751], [50.0, 0.0, -82.7485]),
      ([60.0, 30.0, 40.0], [40.0, -10.0, 5.0]),
      ([0.0, 0.0, 0.0], [100.0, 0.0, 0.0]),
    ];
    for (a, b) in pairs {
      let d_ab = delta_e_2000_sq(a, b);
      let d_ba = delta_e_2000_sq(b, a);
      assert!(
        (d_ab - d_ba).abs() < 1e-4,
        "asymmetric: ΔE²(A→B)={d_ab}, ΔE²(B→A)={d_ba}; A={a:?} B={b:?}"
      );
    }
  }

  /// Empirical correctness contract for the prefilter: across the
  /// standard 17³ = 4913 RGB grid, the prefiltered nearest-neighbor
  /// must agree with the full-scan reference on every query. Pinning
  /// this means we can ship the prefiltered version as the public
  /// `Color::nearest_to_ciede2000` default; if a future palette
  /// change makes K=96 insufficient, this test catches it before
  /// the regression reaches users.
  ///
  /// Gated on `feature = "std"` because it uses `Vec` to collect
  /// mismatch tuples. Under `--no-default-features --features alloc`
  /// the test is skipped (the standard test harness needs std
  /// anyway).
  #[test]
  #[cfg(feature = "std")]
  #[cfg_attr(
    miri,
    ignore = "17³ grid × CIEDE2000 transcendentals too slow under miri"
  )]
  fn prefilter_matches_full_scan_across_grid() {
    let mut mismatches: Vec<([u8; 3], usize, usize)> = Vec::new();
    for r in (0..256u32).step_by(16) {
      for g in (0..256u32).step_by(16) {
        for b in (0..256u32).step_by(16) {
          let rgb = [r as u8, g as u8, b as u8];
          let q = crate::rgb_to_lab(rgb);
          let exact = nearest_idx(q);
          let prefiltered = nearest_idx_prefiltered(q);
          if exact != prefiltered {
            mismatches.push((rgb, exact, prefiltered));
          }
        }
      }
    }
    assert!(
      mismatches.is_empty(),
      "{} prefilter divergences from full-scan CIEDE2000 across the \
       17³ RGB grid (K = {}); first few: {:?}",
      mismatches.len(),
      PREFILTER_K,
      &mismatches[..mismatches.len().min(5)]
    );
  }

  /// Smoke test: the lookup must return a finite index for every RGB
  /// in a small grid and the result's chosen LAB must match the
  /// claimed minimum. Catches NaN/Inf leaks from any of the
  /// transcendentals.
  #[test]
  #[cfg_attr(
    miri,
    ignore = "125-query grid × 949-entry CIEDE2000 transcendentals too slow under miri"
  )]
  fn lookup_returns_valid_index_across_grid() {
    for r in (0..=255u32).step_by(64) {
      for g in (0..=255u32).step_by(64) {
        for b in (0..=255u32).step_by(64) {
          let q = crate::rgb_to_lab([r as u8, g as u8, b as u8]);
          let idx = nearest_idx(q);
          assert!(idx < LABS_L.len());
          let entry = [LABS_L[idx], LABS_A[idx], LABS_B[idx]];
          let d2 = delta_e_2000_sq(q, entry);
          assert!(
            d2.is_finite() && d2 >= 0.0,
            "non-finite/negative ΔE00² at rgb=[{r},{g},{b}]: d2={d2}"
          );
        }
      }
    }
  }
}