colorthief 0.1.0

Dominant-color extraction (MMCQ) and human-vocabulary naming for packed-RGB video keyframes — CIEDE2000 (default, scalar), CIE94 (opt-in, SIMD-dispatched), or Delta E 76 (opt-in, SIMD-dispatched) nearest-neighbor against the xkcd hierarchy.
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
//! End-to-end integration tests for [`colorthief::extract`].
//!
//! Synthetic-frame coverage that crosses both pipeline stages:
//!   pixels → MMCQ dominants → CIEDE76 nearest xkcd entry.
//! The `mmcq::tests` unit tests cover MMCQ in isolation; the
//! `colorthief_dataset::tests` cover NN lookup. These tests pin the
//! composition.
//!
//! Asserting on family / hue rather than exact xkcd names keeps the
//! tests robust across xkcd-dataset regenerations.

use colorthief::{
  Algorithm, Dominant, Mmcq, Rgb48Frame, RgbFrame, RgbFrameError, extract, extract_rgb48,
};

fn solid_color_frame(rgb: [u8; 3], width: u32, height: u32) -> Vec<u8> {
  let mut buf = Vec::with_capacity((width * height) as usize * 3);
  for _ in 0..width * height {
    buf.extend_from_slice(&rgb);
  }
  buf
}

#[test]
fn extract_on_solid_red_returns_a_red_named_color() {
  let buf = solid_color_frame([255, 0, 0], 8, 8);
  let frame = RgbFrame::try_new(&buf, 8, 8, 24).expect("frame");
  let dominants = extract(frame, 5);
  assert!(!dominants.is_empty(), "expected at least one dominant");
  let top = dominants[0];
  assert!(
    top.color().family().as_str().contains("red") || top.color().name().contains("red"),
    "top dominant on solid red was rgb={:?} name={:?} family={:?}",
    top.rgb(),
    top.color().name(),
    top.color().family().as_str(),
  );
  assert!(top.population() > 0, "population must be non-zero");
}

#[test]
fn extract_on_solid_blue_returns_a_blue_named_color() {
  let buf = solid_color_frame([0, 0, 255], 8, 8);
  let frame = RgbFrame::try_new(&buf, 8, 8, 24).expect("frame");
  let dominants = extract(frame, 5);
  assert!(!dominants.is_empty());
  let top = dominants[0];
  assert!(
    top.color().family().as_str().contains("blue") || top.color().name().contains("blue"),
    "top dominant on solid blue was rgb={:?} name={:?} family={:?}",
    top.rgb(),
    top.color().name(),
    top.color().family().as_str(),
  );
}

#[test]
fn extract_count_zero_returns_empty() {
  let buf = solid_color_frame([128, 128, 128], 4, 4);
  let frame = RgbFrame::try_new(&buf, 4, 4, 12).expect("frame");
  let dominants = extract(frame, 0);
  assert!(dominants.is_empty());
}

#[test]
fn extract_on_red_blue_split_recovers_both_hues() {
  // 8x8 frame split half red / half blue (rows 0-3 red, rows 4-7 blue).
  // The dominant set should cover both regions.
  let mut buf = Vec::with_capacity(64 * 3);
  for row in 0..8 {
    for _ in 0..8 {
      let rgb = if row < 4 { [255, 0, 0] } else { [0, 0, 255] };
      buf.extend_from_slice(&rgb);
    }
  }
  let frame = RgbFrame::try_new(&buf, 8, 8, 24).expect("frame");
  let dominants = extract(frame, 5);
  assert!(dominants.len() >= 2);
  let has_red = dominants
    .iter()
    .any(|d| d.color().family().as_str().contains("red") || d.color().name().contains("red"));
  let has_blue = dominants
    .iter()
    .any(|d| d.color().family().as_str().contains("blue") || d.color().name().contains("blue"));
  assert!(
    has_red && has_blue,
    "expected red and blue named entries, got: {:?}",
    dominants
      .iter()
      .map(|d| (d.color().name(), d.rgb(), d.population()))
      .collect::<Vec<_>>()
  );
}

/// Pins the population-descending sort on the public API.
#[test]
fn extract_dominants_sorted_by_population_descending() {
  // 8x8 frame: 75% red, 25% blue. Red must come first.
  let mut buf = Vec::with_capacity(64 * 3);
  for row in 0..8 {
    for _ in 0..8 {
      let rgb = if row < 6 { [255, 0, 0] } else { [0, 0, 255] };
      buf.extend_from_slice(&rgb);
    }
  }
  let frame = RgbFrame::try_new(&buf, 8, 8, 24).expect("frame");
  let dominants = extract(frame, 5);
  assert!(dominants.len() >= 2);
  for window in dominants.windows(2) {
    assert!(
      window[0].population() >= window[1].population(),
      "dominants must be sorted by descending population: {:?}",
      dominants
        .iter()
        .map(|d| (d.color().name(), d.population()))
        .collect::<Vec<_>>()
    );
  }
  let top = dominants[0];
  assert!(
    top.color().family().as_str().contains("red") || top.color().name().contains("red"),
    "75%-red frame: top dominant should be red, got {:?}",
    top.color().name()
  );
}

/// `extract(frame, 1)` was returning two entries because MMCQ's
/// internal `target` clamps to ≥2. The public `count` must be a
/// hard upper bound.
#[test]
fn extract_count_one_returns_at_most_one() {
  // 4x4 half-red / half-blue — at least 2 distinct populated bins,
  // so prior to the fix MMCQ would emit 2 dominants.
  let mut buf = Vec::with_capacity(16 * 3);
  for i in 0..16 {
    let rgb = if i < 8 { [255, 0, 0] } else { [0, 0, 255] };
    buf.extend_from_slice(&rgb);
  }
  let frame = RgbFrame::try_new(&buf, 4, 4, 12).expect("frame");
  let dominants = extract(frame, 1);
  assert!(
    dominants.len() <= 1,
    "extract(_, 1) must return at most 1 entry, got {}: {:?}",
    dominants.len(),
    dominants.iter().map(|d| d.rgb()).collect::<Vec<_>>(),
  );
}

/// `phase1_target = (0.75 * count) as usize` truncated toward zero,
/// so for `count` values with a fractional `0.75 * count` (3, 5, 6,
/// 7, 9, …) phase 1 ended one box early vs. the TS reference's
/// effective ceil semantics. That ceded a split to phase 2's
/// `population * volume` scoring, which can pick a different box
/// than phase 1's pure-population scoring. Without an external
/// reference oracle to assert against, the regression here pins the
/// count-exactness invariant at both fractional boundaries.
///
/// Helper: build an `n × n` frame whose `count` palette colors are
/// each used in equal-area stripes.
fn striped_palette_frame(palette: &[[u8; 3]], width: u32, height: u32) -> Vec<u8> {
  let total = (width * height) as usize;
  let mut buf = Vec::with_capacity(total * 3);
  for i in 0..total {
    buf.extend_from_slice(&palette[i % palette.len()]);
  }
  buf
}

#[test]
fn extract_count_3_returns_full_count() {
  // 3-color palette, count=3 → phase1 target = ceil(2.25) = 3.
  // All splits decided by population scoring; phase 2 is a no-op.
  let palette = [[200u8, 30, 30], [30, 200, 30], [30, 30, 200]];
  let buf = striped_palette_frame(&palette, 8, 8);
  let frame = RgbFrame::try_new(&buf, 8, 8, 24).expect("frame");
  let dominants = extract(frame, 3);
  assert_eq!(dominants.len(), 3);
  for d in &dominants {
    assert!(d.population() > 0);
  }
}

#[test]
fn extract_count_7_returns_full_count() {
  // 7-color palette, count=7 → phase1 target = ceil(5.25) = 6.
  // Phase 1 produces 6 boxes via population scoring; phase 2 adds
  // the 7th via population*volume.
  let palette = [
    [200u8, 30, 30],
    [30, 200, 30],
    [30, 30, 200],
    [200, 200, 30],
    [30, 200, 200],
    [200, 30, 200],
    [128, 128, 128],
  ];
  let buf = striped_palette_frame(&palette, 8, 8);
  let frame = RgbFrame::try_new(&buf, 8, 8, 24).expect("frame");
  let dominants = extract(frame, 7);
  assert_eq!(dominants.len(), 7);
  for d in &dominants {
    assert!(d.population() > 0);
  }
}

/// `iterate_split` was counting empty children of a sparse box's
/// median-cut split toward `target`, so when the frame had `count`
/// distinct colors but one of them sat in a wide-empty-range parent,
/// the loop would terminate with an empty half consuming a target
/// slot. The post-quantize zero-population filter then dropped that
/// empty box, and `extract` underfilled (returned `< count` even
/// though `count` real distinct colors were available).
///
/// Construct a frame with 5 distinct quantized colors arranged so
/// one of them is widely separated on the R axis from the others —
/// MMCQ's first split on R will produce a populated/empty pair for
/// the high-R color's parent.
#[test]
fn extract_with_count_distinct_colors_returns_full_count() {
  // Five distinct colors; the last one sits at R=255 while the rest
  // cluster near R=0..32, so the first R-axis split has a wide
  // empty range on the right of the populated low-R cluster.
  let palette: [[u8; 3]; 5] = [
    [10, 200, 10],  // green-ish, low R
    [10, 10, 200],  // blue-ish, low R
    [200, 10, 200], // magenta-ish, mid R
    [10, 200, 200], // cyan-ish, low R
    [255, 10, 10],  // red, high R — the sparse one
  ];
  // 8x8 frame, equal-area distribution of the 5 colors (with 4
  // remainder pixels reused on the first color so totals are
  // balanced enough that all five survive Phase 1's count-only
  // priority).
  let mut buf = Vec::with_capacity(64 * 3);
  for i in 0..64 {
    let rgb = palette[i % 5];
    buf.extend_from_slice(&rgb);
  }
  let frame = RgbFrame::try_new(&buf, 8, 8, 24).expect("frame");
  assert_eq!(frame.rgb(), buf.as_slice());
  assert_eq!(frame.width(), 8);
  assert_eq!(frame.height(), 8);
  assert_eq!(frame.stride(), 24);

  let dominants = extract(frame, 5);
  assert_eq!(
    dominants.len(),
    5,
    "5 distinct colors but extract returned {} dominants: {:?}",
    dominants.len(),
    dominants
      .iter()
      .map(|d| (d.rgb(), d.population()))
      .collect::<Vec<_>>(),
  );
  for d in &dominants {
    assert!(
      d.population() > 0,
      "zero-population dominant: rgb={:?}",
      d.rgb()
    );
  }
}

/// `median_cut` could produce a (populated, empty) split that
/// `iterate_split` accepted, and `quantize` then mapped the empty
/// box's geometric-center fallback to a fabricated `Dominant`. With
/// fewer distinct colors than `count`, the result must contain only
/// real (population > 0) entries.
#[test]
fn extract_no_zero_population_dominants_below_distinct_color_floor() {
  // 8x8 frame with only 2 populated bins (pure red, pure blue).
  // Request 5 dominants — MMCQ will fail to split productively
  // beyond 2 boxes; pre-fix, the surplus 3 boxes were zero-population
  // entries with fabricated geometric-center colors.
  let mut buf = Vec::with_capacity(64 * 3);
  for i in 0..64 {
    let rgb = if i < 32 { [255, 0, 0] } else { [0, 0, 255] };
    buf.extend_from_slice(&rgb);
  }
  let frame = RgbFrame::try_new(&buf, 8, 8, 24).expect("frame");
  let dominants = extract(frame, 5);
  assert!(
    dominants.len() <= 2,
    "frame has 2 distinct colors but extract returned {} dominants: {:?}",
    dominants.len(),
    dominants
      .iter()
      .map(|d| (d.rgb(), d.population()))
      .collect::<Vec<_>>(),
  );
  for d in &dominants {
    assert!(
      d.population() > 0,
      "zero-population dominant in result: rgb={:?} name={:?}",
      d.rgb(),
      d.color().name(),
    );
  }
}

// ---------------------------------------------------------------------
// Rgb48Frame (16-bit-per-channel) integration tests
// ---------------------------------------------------------------------

/// Build a u16 RGB plane filled with one repeated pixel. Stride
/// equals `3 * width` u16 elements (no padding).
fn solid_color_frame_u16(rgb: [u16; 3], width: u32, height: u32) -> Vec<u16> {
  let mut buf = Vec::with_capacity((width * height) as usize * 3);
  for _ in 0..width * height {
    buf.extend_from_slice(&rgb);
  }
  buf
}

/// Smoke test: pure red u16 input must reach a red-family dominant
/// after the `>> 8` downscale + MMCQ + naming pipeline.
#[test]
fn extract_rgb48_on_solid_red_returns_a_red_named_color() {
  let buf = solid_color_frame_u16([0xFF00, 0x0000, 0x0000], 8, 8);
  let frame = Rgb48Frame::try_new(&buf, 8, 8, 24).expect("frame");
  assert_eq!(frame.width(), 8);
  assert_eq!(frame.height(), 8);
  assert_eq!(frame.stride(), 24);
  assert_eq!(frame.rgb16(), buf.as_slice());
  let dominants = extract_rgb48(frame, 5);
  assert!(!dominants.is_empty(), "expected at least one dominant");
  let top = dominants[0];
  assert!(
    top.color().family().as_str().contains("red") || top.color().name().contains("red"),
    "top dominant on solid red u16 was rgb={:?} name={:?} family={:?}",
    top.rgb(),
    top.color().name(),
    top.color().family().as_str(),
  );
}

/// Equivalence: `extract_rgb48(u16-widened-from-u8)` must produce
/// exactly the same dominants as `extract(u8)`. The downscale
/// `(u8 << 8) >> 8 == u8` is bit-exact, so MMCQ + naming sees
/// identical data on both paths.
#[test]
fn extract_rgb48_widened_matches_extract_u8() {
  // Mixed-color frame to exercise more than one MMCQ box.
  let mut buf_u8 = Vec::with_capacity(64 * 3);
  for i in 0..64 {
    let rgb = match i % 4 {
      0 => [200, 30, 30],
      1 => [30, 30, 200],
      2 => [30, 200, 30],
      _ => [180, 180, 180],
    };
    buf_u8.extend_from_slice(&rgb);
  }
  let buf_u16: Vec<u16> = buf_u8.iter().map(|&b| (b as u16) << 8).collect();

  let frame_u8 = RgbFrame::try_new(&buf_u8, 8, 8, 24).expect("u8 frame");
  let frame_u16 = Rgb48Frame::try_new(&buf_u16, 8, 8, 24).expect("u16 frame");

  let d_u8 = extract(frame_u8, 5);
  let d_u16 = extract_rgb48(frame_u16, 5);

  assert_eq!(d_u8.len(), d_u16.len(), "dominant counts must match");
  for (a, b) in d_u8.iter().zip(d_u16.iter()) {
    assert_eq!(
      a.rgb(),
      b.rgb(),
      "u8 and u16-widened paths produced different RGBs"
    );
    assert_eq!(a.population(), b.population(), "populations diverged");
    assert_eq!(
      a.color().name(),
      b.color().name(),
      "named colors diverged: u8={} u16={}",
      a.color().name(),
      b.color().name(),
    );
  }
}

/// `try_new` rejects zero-dimension frames.
#[test]
fn rgb48_try_new_rejects_zero_dimension() {
  let buf = vec![0u16; 12];
  let err = Rgb48Frame::try_new(&buf, 0, 4, 12).unwrap_err();
  assert!(matches!(err, RgbFrameError::ZeroDimension { .. }));
  let err = Rgb48Frame::try_new(&buf, 4, 0, 12).unwrap_err();
  assert!(matches!(err, RgbFrameError::ZeroDimension { .. }));
}

/// `Rgb48Frame::try_new` rejects widths whose `3 * width` overflows u32.
#[test]
fn rgb48_try_new_rejects_width_overflow() {
  let buf: Vec<u16> = Vec::new();
  let err = Rgb48Frame::try_new(&buf, u32::MAX / 2, 1, u32::MAX).unwrap_err();
  assert!(
    matches!(err, RgbFrameError::WidthOverflow { .. }),
    "expected WidthOverflow, got {err:?}",
  );
}

/// `Rgb48Frame::try_new` rejects (stride, height) combos whose product
/// overflows usize. Only meaningful on 32-bit targets — on 64-bit
/// targets `(u32::MAX as usize) * (u32::MAX as usize)` doesn't
/// overflow, so the branch is unreachable in practice.
#[test]
#[cfg(target_pointer_width = "32")]
fn rgb48_try_new_rejects_geometry_overflow() {
  let buf: Vec<u16> = Vec::new();
  let err = Rgb48Frame::try_new(&buf, 1, u32::MAX, u32::MAX).unwrap_err();
  assert!(
    matches!(err, RgbFrameError::GeometryOverflow { .. }),
    "expected GeometryOverflow, got {err:?}",
  );
}

// ---------------------------------------------------------------------
// RgbFrame error-path coverage (8-bit frame)
// ---------------------------------------------------------------------

/// `RgbFrame::try_new` rejects zero width or zero height.
#[test]
fn rgb_try_new_rejects_zero_dimension() {
  let buf = vec![0u8; 12];
  let err = RgbFrame::try_new(&buf, 0, 4, 12).unwrap_err();
  assert!(matches!(err, RgbFrameError::ZeroDimension { .. }));
  let err = RgbFrame::try_new(&buf, 4, 0, 12).unwrap_err();
  assert!(matches!(err, RgbFrameError::ZeroDimension { .. }));
}

/// `RgbFrame::try_new` rejects stride < 3 * width.
#[test]
fn rgb_try_new_rejects_stride_too_small() {
  let buf = vec![0u8; 16];
  let err = RgbFrame::try_new(&buf, 4, 2, 8).unwrap_err();
  assert!(
    matches!(
      err,
      RgbFrameError::StrideTooSmall {
        min_stride: 12,
        stride: 8
      }
    ),
    "expected StrideTooSmall, got {err:?}",
  );
}

/// `RgbFrame::try_new` rejects buffers shorter than `stride * height`.
#[test]
fn rgb_try_new_rejects_plane_too_short() {
  let buf = vec![0u8; 30];
  let err = RgbFrame::try_new(&buf, 4, 4, 12).unwrap_err();
  assert!(
    matches!(
      err,
      RgbFrameError::PlaneTooShort {
        expected: 48,
        actual: 30
      }
    ),
    "expected PlaneTooShort, got {err:?}",
  );
}

/// `RgbFrame::try_new` rejects widths whose `3 * width` overflows u32.
#[test]
fn rgb_try_new_rejects_width_overflow() {
  let buf: Vec<u8> = Vec::new();
  let err = RgbFrame::try_new(&buf, u32::MAX / 2, 1, u32::MAX).unwrap_err();
  assert!(
    matches!(err, RgbFrameError::WidthOverflow { .. }),
    "expected WidthOverflow, got {err:?}",
  );
}

/// `RgbFrame::try_new` rejects (stride, height) combos whose product
/// overflows usize. Only meaningful on 32-bit targets.
#[test]
#[cfg(target_pointer_width = "32")]
fn rgb_try_new_rejects_geometry_overflow() {
  let buf: Vec<u8> = Vec::new();
  let err = RgbFrame::try_new(&buf, 1, u32::MAX, u32::MAX).unwrap_err();
  assert!(
    matches!(err, RgbFrameError::GeometryOverflow { .. }),
    "expected GeometryOverflow, got {err:?}",
  );
}

/// `try_new` rejects stride < 3 * width (in u16 elements).
#[test]
fn rgb48_try_new_rejects_stride_too_small() {
  let buf = vec![0u16; 16];
  // width = 4 → min_stride = 12 u16 elements; supply 8.
  let err = Rgb48Frame::try_new(&buf, 4, 2, 8).unwrap_err();
  assert!(
    matches!(
      err,
      RgbFrameError::StrideTooSmall {
        min_stride: 12,
        stride: 8
      }
    ),
    "expected StrideTooSmall, got {err:?}",
  );
}

/// `try_new` rejects buffers shorter than `stride * height` u16
/// elements.
#[test]
fn rgb48_try_new_rejects_plane_too_short() {
  // width = 4, height = 4, stride = 12 → need 48 u16 elements; supply 30.
  let buf = vec![0u16; 30];
  let err = Rgb48Frame::try_new(&buf, 4, 4, 12).unwrap_err();
  assert!(
    matches!(
      err,
      RgbFrameError::PlaneTooShort {
        expected: 48,
        actual: 30
      }
    ),
    "expected PlaneTooShort, got {err:?}",
  );
}

/// `extract_rgb48(_, 0)` returns an empty Vec — same contract as
/// [`extract`].
#[test]
fn extract_rgb48_count_zero_returns_empty() {
  let buf = solid_color_frame_u16([0x8000, 0x4000, 0x2000], 4, 4);
  let frame = Rgb48Frame::try_new(&buf, 4, 4, 12).expect("frame");
  let dominants = extract_rgb48(frame, 0);
  assert!(dominants.is_empty());
}

// ---------------------------------------------------------------------
// no_alloc-path API: Mmcq::extract with a Buffer (fixed-size array).
// ---------------------------------------------------------------------

/// `Mmcq::extract` writing into a `[Option<Dominant>; N]` buffer —
/// the canonical no_alloc usage. Exercises the same pipeline as
/// `extract` but without the convenience `Vec` wrapper.
#[test]
fn mmcq_extract_into_array_buffer_recovers_red() {
  let buf = solid_color_frame([200, 50, 50], 8, 8);
  let frame = RgbFrame::try_new(&buf, 8, 8, 24).expect("frame");

  let mut mmcq = Mmcq::new_boxed();
  let mut out: [Option<Dominant>; 5] = [const { None }; 5];
  mmcq.extract(frame.pixels(), 5, Algorithm::default(), &mut out);

  let first = out
    .iter()
    .find_map(|o| o.as_ref())
    .expect("expected at least one dominant");
  assert!(
    first.color().family().as_str().contains("red") || first.color().name().contains("red"),
    "expected red-family dominant, got {:?}",
    first.color().name(),
  );
  assert!(first.population() > 0);
}

/// `Mmcq` reuse across calls — verify the workspace can be reused
/// without leaking state from previous calls. Critical for the
/// thread_local-cache pattern.
#[test]
fn mmcq_reuse_resets_state_between_calls() {
  let mut mmcq = Mmcq::new_boxed();

  // First call: red frame.
  let red_buf = solid_color_frame([220, 20, 20], 8, 8);
  let red_frame = RgbFrame::try_new(&red_buf, 8, 8, 24).expect("frame");
  let mut red_out: [Option<Dominant>; 3] = [const { None }; 3];
  mmcq.extract(red_frame.pixels(), 3, Algorithm::default(), &mut red_out);
  let red_first = red_out
    .iter()
    .find_map(|o| o.as_ref())
    .expect("red dominant");
  assert!(
    red_first.color().family().as_str().contains("red") || red_first.color().name().contains("red")
  );

  // Second call: blue frame. Same Mmcq, must NOT remember red state.
  let blue_buf = solid_color_frame([20, 20, 220], 8, 8);
  let blue_frame = RgbFrame::try_new(&blue_buf, 8, 8, 24).expect("frame");
  let mut blue_out: [Option<Dominant>; 3] = [const { None }; 3];
  mmcq.extract(blue_frame.pixels(), 3, Algorithm::default(), &mut blue_out);
  let blue_first = blue_out
    .iter()
    .find_map(|o| o.as_ref())
    .expect("blue dominant");
  assert!(
    blue_first.color().family().as_str().contains("blue")
      || blue_first.color().name().contains("blue"),
    "second-call dominant should be blue, got {:?}",
    blue_first.color().name(),
  );
}

#[test]
fn stack_mmcq() {
  // Workspace placement: `static mut` (no_alloc) or `Mmcq::new_boxed()`
  // (alloc — see `examples/extract.rs`).
  static mut MMCQ: Mmcq = Mmcq::new();

  const W: u32 = 16;
  const H: u32 = 16;
  const STRIDE: u32 = W * 3;

  // Synthetic 16×16 frame on the stack — 75% red, 25% blue.
  let mut buf = [0u8; (STRIDE * H) as usize];
  for row in 0..H as usize {
    let rgb = if row < 12 {
      [220, 30, 30]
    } else {
      [30, 30, 220]
    };
    for col in 0..W as usize {
      let off = row * STRIDE as usize + col * 3;
      buf[off..off + 3].copy_from_slice(&rgb);
    }
  }
  let frame = RgbFrame::try_new(&buf, W, H, STRIDE).expect("frame");

  // Output buffer: fixed-capacity, every slot starts as `None` and
  // `Mmcq::extract` fills the first N positions via the
  // `impl Buffer<Dominant> for [Option<Dominant>; N]` blanket.
  let mut out: [Option<Dominant>; 5] = [const { None }; 5];

  // SAFETY: this is the only access to MMCQ in this single-threaded
  // example. Real `no_std + alloc` consumers should keep the same
  // single-threaded discipline (typical wasm32-unknown-unknown,
  // interrupt-free bare metal); multi-threaded environments should
  // either provide their own synchronization or use the per-call
  // `Mmcq::new_boxed()` path instead.
  #[allow(static_mut_refs)]
  unsafe {
    (*core::ptr::addr_of_mut!(MMCQ)).extract(frame.pixels(), 5, Algorithm::default(), &mut out);
  }

  for slot in out.iter().flatten() {
    println!(
      "rgb={:?}  name={:?}  family={:?}  pop={}  ({:.1}%)",
      slot.rgb(),
      slot.color().name(),
      slot.color().family().as_str(),
      slot.population(),
      slot.percentage(),
    );
  }
}