lichtung-plot 0.3.0

Strongly typed scientific plotting with deterministic SVG and PNG output
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
//! Fixed first-party opaque color arithmetic. T5d1 pins the numerical kernel
//! before T5d2/T5d3 route semantic palettes and gradients through it.

#![allow(
    dead_code,
    reason = "T5d1 numerical kernel is wired to active scale consumers in T5d2/T5d3"
)]

use super::OpaqueColor;
use crate::Error;
use num_bigint::BigUint;
use std::cmp::Ordering;

const MATRIX_EDGE_SLACK: f64 = 1.0 / 4_194_304.0;

// The nearest encoded-sRGB byte transitions, in *linear* RGB. For boundary
// k, the encoded source is exactly (k+1/2)/255; the corresponding piecewise
// sRGB transfer is evaluated with 120, 300 and 480 decimal digits, then rounded
// nearest-even to binary64. All three independently selected binary64 tables
// agree. Big-endian u64 stream SHA-256:
// ace388c2947682af22573df8b12dde52e54c10fa706965d3011b159a5a7e340f.
// Using these half-channel thresholds avoids host powf in inverse OKLab output.
#[expect(
    clippy::unreadable_literal,
    reason = "pinned binary64 channel transitions are recorded verbatim"
)]
const ENCODED_HALF_LINEAR_BITS: [u64; 255] = [
    0x3f23e45677c176f7,
    0x3f3dd681b3a23272,
    0x3f48dd6c15b1d4b4,
    0x3f5167cba8c94818,
    0x3f5660e146b9a5d5,
    0x3f5b59f6e4aa0393,
    0x3f602986414d30a8,
    0x3f62a61110455f87,
    0x3f65229bdf3d8e66,
    0x3f679f26ae35bd45,
    0x3f6a1e5a03a8a4b5,
    0x3f6cbf734477e0e7,
    0x3f6f8680590912c1,
    0x3f713a0be3e98ad5,
    0x3f72c4665c6bc58b,
    0x3f74629793a399b0,
    0x3f7614e607554158,
    0x3f77db96ca0c99d9,
    0x3f79b6ed95fb6dba,
    0x3f7ba72cde4cb5ca,
    0x3f7dac95df183297,
    0x3f7fc768ac1bd733,
    0x3f80fbf21f2dc489,
    0x3f821f234061c55c,
    0x3f834d662df32ddc,
    0x3f8486d8e075e9d4,
    0x3f85cb98d9e2545f,
    0x3f871bc32a59ad46,
    0x3f887774749cc7c5,
    0x3f89dec8f23ba5bf,
    0x3f8b51dc7783fa2a,
    0x3f8cd0ca7733ec84,
    0x3f8e5bae05f5ea99,
    0x3f8ff2a1dda9da46,
    0x3f90cae0303fc81d,
    0x3f91a291cdf30864,
    0x3f928072a5a9656b,
    0x3f93648f6d71d8d4,
    0x3f944ef4b4ef6b8f,
    0x3f953faee688f616,
    0x3f9636ca4889ebee,
    0x3f973452fe3532a6,
    0x3f98385508caeef9,
    0x3f9942dc48821f75,
    0x3f9a53f47d76ca86,
    0x3f9b6ba9488d7613,
    0x3f9c8a062c4c9086,
    0x3f9daf168dac664f,
    0x3f9edae5b4de330d,
    0x3fa006bf67056983,
    0x3fa0a3767504c7e3,
    0x3fa1439d7f87bcfa,
    0x3fa1e739f4abdd1c,
    0x3fa28e5135e29dea,
    0x3fa338e8983f0647,
    0x3fa3e70564c063c2,
    0x3fa498acd89a2d57,
    0x3fa54de4257938cc,
    0x3fa606b071c66583,
    0x3fa6c316d8e6dd5e,
    0x3fa7831c6b7a0a43,
    0x3fa846c62f955cea,
    0x3fa90e1920fdffce,
    0x3fa9d91a31608f91,
    0x3faaa7ce4886f087,
    0x3fab7a3a448c57bf,
    0x3fac5062fa0f9c99,
    0x3fad2a4d3463e6bc,
    0x3fae07fdb5bfcb10,
    0x3faee979376ae979,
    0x3fafcec469ea1be6,
    0x3fb05bf1fa95233f,
    0x3fb0d26e3c54ebea,
    0x3fb14ad945d08395,
    0x3fb1c5355e946f1e,
    0x3fb24184ca308d86,
    0x3fb2bfc9c84a7aee,
    0x3fb3400694af6b4b,
    0x3fb3c23d67658241,
    0x3fb4467074bcad3f,
    0x3fb4cca1ed5f04c9,
    0x3fb554d3fe60b982,
    0x3fb5df08d14f9170,
    0x3fb66b428c41f9a1,
    0x3fb6f98351e5b03c,
    0x3fb789cd418e0ac2,
    0x3fb81c227741dc30,
    0x3fb8b0850bc8fe6b,
    0x3fb946f714b98253,
    0x3fb9df7aa4848998,
    0x3fba7a11ca82cd65,
    0x3fbb16be9300d4b9,
    0x3fbbb583074add2e,
    0x3fbc56612db878df,
    0x3fbcf95b09b7e3e6,
    0x3fbd9e729bd913de,
    0x3fbe45a9e1d883c9,
    0x3fbeef02d6a9be77,
    0x3fbf9a7f7281a9a9,
    0x3fc02410d57049f5,
    0x3fc07bf5b94e038a,
    0x3fc0d4ef5cf430b2,
    0x3fc12efeb7311b79,
    0x3fc18a24bd8bbe9d,
    0x3fc1e6626448412f,
    0x3fc243b89e6c58e7,
    0x3fc2a2285dc393e6,
    0x3fc301b292e38aa4,
    0x3fc362582d2ffac4,
    0x3fc3c41a1adecb7a,
    0x3fc426f948fbfc29,
    0x3fc48af6a36d7de4,
    0x3fc4f01314f6f85f,
    0x3fc5564f873d7af6,
    0x3fc5bdace2cb1a51,
    0x3fc6262c0f127b37,
    0x3fc68fcdf2724b0d,
    0x3fc6fa937238a68f,
    0x3fc7667d72a66f3d,
    0x3fc7d38cd6f28fe9,
    0x3fc841c2814d30ed,
    0x3fc8b11f52e2dc70,
    0x3fc921a42bdf9326,
    0x3fc99351eb71d1f9,
    0x3fca06296fcd88fb,
    0x3fca7a2b962f040b,
    0x3fcaef593addc582,
    0x3fcb65b3392f5350,
    0x3fcbdd3a6b89f6d8,
    0x3fcc55efab676fe3,
    0x3fcccfd3d1579b02,
    0x3fcd4ae7b5030ba9,
    0x3fcdc72c2d2d9a5b,
    0x3fce44a20fb8e723,
    0x3fcec34a31a6d0b4,
    0x3fcf4325671be069,
    0x3fcfc4348361ab75,
    0x3fd0233c2c7494c1,
    0x3fd064f8dca68078,
    0x3fd0a750baa9e48d,
    0x3fd0ea442e792155,
    0x3fd12dd39fa6c332,
    0x3fd171ff755e9545,
    0x3fd1b6c81666af86,
    0x3fd1fc2de920806a,
    0x3fd242315389d220,
    0x3fd288d2bb3dcb8f,
    0x3fd2d0128575ed1d,
    0x3fd317f1170b096c,
    0x3fd3606ed4763a09,
    0x3fd3a98c21d1d03f,
    0x3fd3f34962da4212,
    0x3fd43da6faef137c,
    0x3fd488a54d13bc06,
    0x3fd4d444bbf088ca,
    0x3fd52085a9d37af4,
    0x3fd56d6878b122d7,
    0x3fd5baed8a2577aa,
    0x3fd609153f74abf5,
    0x3fd657dff98bfecf,
    0x3fd6a74e190289f3,
    0x3fd6f75ffe1a0cbc,
    0x3fd7481608bfb425,
    0x3fd79970988cdfca,
    0x3fd7eb700cc7e40c,
    0x3fd83e14c464c956,
    0x3fd8915f1e0608a3,
    0x3fd8e54f77fd4542,
    0x3fd939e6304c03f5,
    0x3fd98f23a4a45f6c,
    0x3fd9e5083269ba32,
    0x3fda3b9436b16e15,
    0x3fda92c80e437919,
    0x3fdaeaa4159b27f7,
    0x3fdb4328a8e7be4a,
    0x3fdb9c56240d1c5a,
    0x3fdbf62ce2a462a8,
    0x3fdc50ad3ffc933a,
    0x3fdcabd7971b30b4,
    0x3fdd07ac42bcdb47,
    0x3fdd642b9d55eb84,
    0x3fddc15601130b20,
    0x3fde1f2bc7d9cba5,
    0x3fde7dad4b493b27,
    0x3fdedcdae4ba7707,
    0x3fdf3cb4ed413cbf,
    0x3fdf9d3bbdac78d2,
    0x3fdffe6fae86d3d6,
    0x3fe030288c0b9edc,
    0x3fe061702930bb93,
    0x3fe0930eda934ca2,
    0x3fe0c504cbf2cdca,
    0x3fe0f75228edec22,
    0x3fe129f71d02c75c,
    0x3fe15cf3d38f323d,
    0x3fe1904877d0f24b,
    0x3fe1c3f534e5fea4,
    0x3fe1f7fa35ccbe1b,
    0x3fe22c57a564448b,
    0x3fe2610dae6c8f65,
    0x3fe2961c7b86c188,
    0x3fe2cb8437355e5b,
    0x3fe301450bdc8432,
    0x3fe3375f23c225fb,
    0x3fe36dd2a90e443c,
    0x3fe3a49fc5cb2566,
    0x3fe3dbc6a3e58d79,
    0x3fe413476d2cf4fe,
    0x3fe44b224b53bf61,
    0x3fe4835767ef70a2,
    0x3fe4bbe6ec78e26b,
    0x3fe4f4d1024c7882,
    0x3fe52e15d2aa54a6,
    0x3fe567b586b689cd,
    0x3fe5a1b047794ed4,
    0x3fe5dc063ddf3090,
    0x3fe616b792b94357,
    0x3fe651c46ebd53f4,
    0x3fe68d2cfa861810,
    0x3fe6c8f15e935e0f,
    0x3fe70511c34a3c66,
    0x3fe7418e50f54067,
    0x3fe77e672fc49c8c,
    0x3fe7bb9c87ce563d,
    0x3fe7f92e810e7315,
    0x3fe8371d436725ab,
    0x3fe87568f6a0f9dc,
    0x3fe8b411c26b0098,
    0x3fe8f317ce5afb3e,
    0x3fe9327b41ed8674,
    0x3fe9723c44864498,
    0x3fe9b25afd7007af,
    0x3fe9f2d793dcfaee,
    0x3fea33b22ee6cbcc,
    0x3fea74eaf58ed2a5,
    0x3feab6820ebe3af1,
    0x3feaf877a1462b15,
    0x3feb3acbd3dfebbe,
    0x3feb7d7ecd2d0ee0,
    0x3febc090b3b79644,
    0x3fec0401adf219b8,
    0x3fec47d1e237ecd4,
    0x3fec8c0176cd4466,
    0x3fecd09091df5b70,
    0x3fed157f598497d1,
    0x3fed5acdf3bcae8a,
    0x3feda07c8670c7a5,
    0x3fede68b3773a1c6,
    0x3fee2cfa2c81b559,
    0x3fee73c98b41576d,
    0x3feebaf97942dc33,
    0x3fef028a1c00b929,
    0x3fef4a7b98dfa6eb,
    0x3fef92ce152ec2b7,
    0x3fefdb81b627af90,
];

/// Cartesian OKLab components. All RGB inputs are opaque and channel-exact;
/// opacity is selected by the independent mark/typography role after mapping.
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct Oklab {
    pub(super) l: f64,
    pub(super) a: f64,
    pub(super) b: f64,
}

impl Oklab {
    pub(super) fn from_rgb(rgb: (u8, u8, u8)) -> Result<Self, Error> {
        let red = super::contrast::linear_channel(rgb.0);
        let green = super::contrast::linear_channel(rgb.1);
        let blue = super::contrast::linear_channel(rgb.2);
        // The declared left-associative binary64 evaluation is deliberately
        // not expressed through matrix multiplication, FMA or platform powf.
        let long = (0.412_221_470_8 * red + 0.536_332_536_3 * green) + 0.051_445_992_9 * blue;
        let medium = (0.211_903_498_2 * red + 0.680_699_545_1 * green) + 0.107_396_956_6 * blue;
        let short = (0.088_302_461_9 * red + 0.281_718_837_6 * green) + 0.629_978_700_5 * blue;
        let long = cube_root(long)?;
        let medium = cube_root(medium)?;
        let short = cube_root(short)?;
        Ok(Self {
            l: (0.210_454_255_3 * long + 0.793_617_785_0 * medium) - 0.004_072_046_8 * short,
            a: (1.977_998_495_1 * long - 2.428_592_205_0 * medium) + 0.450_593_709_9 * short,
            b: (0.025_904_037_1 * long + 0.782_771_766_2 * medium) - 0.808_675_766_0 * short,
        })
    }

    /// Convert fixed OKLab coordinates back to linear-sRGB without host
    /// exponentiation. Gamut reduction for generated out-of-range colors is
    /// owned by T5d1b; this branch accepts only in-gamut channel operands.
    pub(super) fn linear_rgb(self) -> Result<[f64; 3], Error> {
        if ![self.l, self.a, self.b].into_iter().all(f64::is_finite) {
            return Err(Error::Render("OKLab coordinates must be finite".into()));
        }
        let long = (self.l + 0.396_337_777_4 * self.a) + 0.215_803_757_3 * self.b;
        let medium = (self.l - 0.105_561_345_8 * self.a) - 0.063_854_172_8 * self.b;
        let short = (self.l - 0.089_484_177_5 * self.a) - 1.291_485_548_0 * self.b;
        let long = (long * long) * long;
        let medium = (medium * medium) * medium;
        let short = (short * short) * short;
        let result = [
            (4.076_741_662_1 * long - 3.307_711_591_3 * medium) + 0.230_969_929_2 * short,
            (-1.268_438_004_6 * long + 2.609_757_401_1 * medium) - 0.341_319_396_5 * short,
            (-0.004_196_086_3 * long - 0.703_418_614_7 * medium) + 1.707_614_701_0 * short,
        ];
        if !result.into_iter().all(f64::is_finite) {
            return Err(Error::Render(
                "OKLab inverse channels are not finite".into(),
            ));
        }
        Ok(result)
    }

    pub(super) fn opaque_rgb(self) -> Result<(u8, u8, u8), Error> {
        let [red, green, blue] = self.linear_rgb()?;
        Ok((
            encoded_channel(red)?,
            encoded_channel(green)?,
            encoded_channel(blue)?,
        ))
    }

    fn in_gamut(self) -> Result<bool, Error> {
        Ok(self
            .linear_rgb()?
            .into_iter()
            .all(|channel| (-MATRIX_EDGE_SLACK..=1.0 + MATRIX_EDGE_SLACK).contains(&channel)))
    }

    /// Fixed 24-step chroma bisection at this source-derived lightness/hue.
    /// The first attempted color is used verbatim when already in gamut;
    /// otherwise every trial is admitted before inverse matrix evaluation.
    pub(super) fn reduced_rgb(self) -> Result<((u8, u8, u8), Self), Error> {
        if self.in_gamut()? {
            return Ok((self.opaque_rgb()?, self));
        }
        let neutral = Self {
            a: 0.0,
            b: 0.0,
            ..self
        };
        if !neutral.in_gamut()? {
            return Err(Error::Render(
                "OKLab neutral lightness is outside the finite sRGB gamut".into(),
            ));
        }
        let (mut lower, mut upper) = (0.0_f64, 1.0_f64);
        for _ in 0..24 {
            let midpoint = f64::midpoint(lower, upper);
            let candidate = Self {
                a: self.a * midpoint,
                b: self.b * midpoint,
                ..self
            };
            if candidate.in_gamut()? {
                lower = midpoint;
            } else {
                upper = midpoint;
            }
        }
        let selected = Self {
            a: self.a * lower,
            b: self.b * lower,
            ..self
        };
        Ok((selected.opaque_rgb()?, selected))
    }
}

/// Interpolate final opaque data colors in Cartesian OKLab. Exact endpoints
/// retain the caller's RGB bytes and avoid an unnecessary matrix roundtrip.
pub(super) fn interpolate(
    first: (u8, u8, u8),
    second: (u8, u8, u8),
    fraction: f64,
) -> Result<(u8, u8, u8), Error> {
    if !fraction.is_finite() || !(0.0..=1.0).contains(&fraction) {
        return Err(Error::Data(
            "OKLab interpolation fraction must be finite in [0,1]".into(),
        ));
    }
    if fraction.to_bits() == 0.0_f64.to_bits() {
        return Ok(first);
    }
    if fraction.to_bits() == 1.0_f64.to_bits() {
        return Ok(second);
    }
    let first = Oklab::from_rgb(first)?;
    let second = Oklab::from_rgb(second)?;
    let interpolate = |low: f64, high: f64| low + (high - low) * fraction;
    Oklab {
        l: interpolate(first.l, second.l),
        a: interpolate(first.a, second.a),
        b: interpolate(first.b, second.b),
    }
    .reduced_rgb()
    .map(|(rgb, _)| rgb)
}

/// Finite, prefix-stable maximin candidates for private qualitative and accent
/// palettes. Hues traverse a normalized rational square ring (no host trig);
/// lightness bands and chroma bands have a fixed order independent of the
/// eventual requested domain size. Each next color compares its final
/// quantized RGB identity and its actual reduced OKLab against prior colors.
const HUE_DIRECTIONS: usize = 64;
const LIGHTNESS_BANDS: [f64; 16] = [
    0.0, -0.05, 0.05, -0.10, 0.10, -0.15, 0.15, -0.20, 0.20, -0.25, 0.25, -0.30, 0.30, -0.35, 0.35,
    0.40,
];
const CHROMA_BANDS: [f64; 8] = [1.0, 0.75, 1.25, 0.5, 1.5, 0.25, 1.75, 0.0];

#[derive(Clone, Copy)]
struct PaletteCharacter {
    lightness: f64,
    chroma: f64,
}

struct CandidateChoice {
    distance: f64,
    index: usize,
    color: OpaqueColor,
    lab: Oklab,
}

#[derive(Clone, Copy)]
struct PaletteCandidate {
    color: OpaqueColor,
    lab: Option<Oklab>,
}

fn candidate_better(distance: f64, index: usize, best: Option<&CandidateChoice>) -> bool {
    best.is_none_or(|choice| {
        distance.total_cmp(&choice.distance).is_gt()
            || distance.to_bits() == choice.distance.to_bits() && index < choice.index
    })
}

fn candidate_lab(character: PaletteCharacter, ordinal: usize) -> Oklab {
    let hue = ordinal % HUE_DIRECTIONS;
    let side = hue / 16;
    let step = f64::from(u32::try_from(hue % 16).expect("ring step is below 16")) / 16.0;
    let (dx, dy) = match side {
        0 => (1.0, -1.0 + 2.0 * step),
        1 => (1.0 - 2.0 * step, 1.0),
        2 => (-1.0, 1.0 - 2.0 * step),
        _ => (-1.0 + 2.0 * step, -1.0),
    };
    let length = (dx * dx + dy * dy).sqrt();
    let band = LIGHTNESS_BANDS[(ordinal / HUE_DIRECTIONS) % LIGHTNESS_BANDS.len()];
    let chroma_band = CHROMA_BANDS[ordinal / (HUE_DIRECTIONS * LIGHTNESS_BANDS.len())];
    let radius = character.chroma * chroma_band;
    Oklab {
        l: (character.lightness + band).clamp(0.05, 0.95),
        a: radius * dx / length,
        b: radius * dy / length,
    }
}

fn select_candidate(
    character: PaletteCharacter,
    colors: &[OpaqueColor],
    labs: &[Oklab],
    candidates: usize,
    generated: &mut Vec<PaletteCandidate>,
) -> Result<(OpaqueColor, Oklab), Error> {
    for ordinal in generated.len()..candidates {
        let (rgb, _) = candidate_lab(character, ordinal).reduced_rgb()?;
        generated.push(PaletteCandidate {
            color: OpaqueColor::rgb(rgb.0, rgb.1, rgb.2),
            lab: None,
        });
    }
    let mut best: Option<CandidateChoice> = None;
    for (ordinal, candidate) in generated.iter_mut().enumerate().take(candidates) {
        let rgb = candidate.color;
        let mut duplicate = false;
        for prior in colors {
            if *prior == rgb {
                duplicate = true;
                break;
            }
        }
        if duplicate {
            continue;
        }
        let actual = if let Some(lab) = candidate.lab {
            lab
        } else {
            let lab = Oklab::from_rgb(rgb.as_rgb())?;
            candidate.lab = Some(lab);
            lab
        };
        let mut minimum = f64::INFINITY;
        for prior in labs {
            let difference = (actual.l - prior.l) * (actual.l - prior.l)
                + (actual.a - prior.a) * (actual.a - prior.a)
                + (actual.b - prior.b) * (actual.b - prior.b);
            minimum = minimum.min(difference);
        }
        if candidate_better(minimum, ordinal, best.as_ref()) {
            best = Some(CandidateChoice {
                distance: minimum,
                index: ordinal,
                color: rgb,
                lab: actual,
            });
        }
    }
    let selected = best.ok_or_else(|| {
        Error::Data("extendable color palette exhausted its finite distinct candidate set".into())
    })?;
    Ok((selected.color, selected.lab))
}

fn admit_candidates(count: usize) -> Result<Vec<PaletteCandidate>, Error> {
    let mut values = Vec::new();
    crate::operation::check()?;
    values
        .try_reserve_exact(count)
        .map_err(|_| Error::Render("cannot reserve palette candidates".into()))?;
    Ok(values)
}

pub(super) fn extend_palette(
    seeds: &[OpaqueColor],
    requested: usize,
) -> Result<Vec<OpaqueColor>, Error> {
    if seeds.is_empty() {
        return Err(Error::Data(
            "extendable color palette requires a nonempty prefix".into(),
        ));
    }
    if requested == 0 {
        return Ok(Vec::new());
    }
    if requested <= seeds.len() {
        let mut prefix = Vec::new();
        crate::operation::check()?;
        prefix
            .try_reserve_exact(requested)
            .map_err(|_| Error::Render("cannot reserve palette prefix".into()))?;
        prefix.extend_from_slice(&seeds[..requested]);
        return Ok(prefix);
    }
    let records = requested
        .checked_add(seeds.len())
        .ok_or_else(|| Error::Render("color palette capacity arithmetic overflowed".into()))?;

    let mut result = Vec::new();
    let mut lab = Vec::new();
    crate::operation::check()?;
    result
        .try_reserve_exact(requested)
        .map_err(|_| Error::Render("cannot reserve resolved color palette".into()))?;
    crate::operation::check()?;
    lab.try_reserve_exact(records)
        .map_err(|_| Error::Render("cannot reserve resolved OKLab colors".into()))?;
    for &color in seeds {
        lab.push(Oklab::from_rgb(color.as_rgb())?);
        if result.len() < requested {
            result.push(color);
        }
    }
    if result.len() == requested {
        return Ok(result);
    }
    let denominator = u32::try_from(seeds.len())
        .map_err(|_| Error::Render("palette seed count exceeds exact color arithmetic".into()))?;
    let (mut lightness, mut chroma) = (0.0_f64, 0.0_f64);
    for seed in &lab {
        lightness += seed.l;
        chroma += (seed.a * seed.a + seed.b * seed.b).sqrt();
    }
    let character = PaletteCharacter {
        lightness: lightness / f64::from(denominator),
        chroma: (chroma / f64::from(denominator)).max(0.08),
    };
    let cap = HUE_DIRECTIONS * LIGHTNESS_BANDS.len() * CHROMA_BANDS.len();
    let max_ordinal = requested - seeds.len() - 1;
    let max_candidates = 128_usize
        .saturating_add(max_ordinal.saturating_mul(32))
        .min(cap);
    let mut generated = admit_candidates(max_candidates)?;
    while result.len() < requested {
        let new_ordinal = result.len() - seeds.len();
        let candidates = 128_usize
            .saturating_add(new_ordinal.saturating_mul(32))
            .min(cap);
        let (rgb, actual) = select_candidate(character, &result, &lab, candidates, &mut generated)?;
        result.push(rgb);
        lab.push(actual);
    }
    Ok(result)
}

fn encoded_channel(value: f64) -> Result<u8, Error> {
    // The inverse coefficients use 10 decimal digits. A 2^-22 slack bounds
    // their finite binary64 roundtrip error at the two exact gamut edges;
    // genuinely out-of-gamut colors go to the explicit T5d1b chroma reducer.
    if !value.is_finite() || !(-MATRIX_EDGE_SLACK..=1.0 + MATRIX_EDGE_SLACK).contains(&value) {
        return Err(Error::Render(
            "OKLab inverse color lies outside the sRGB gamut".into(),
        ));
    }
    let value = value.clamp(0.0, 1.0);
    let (mut lower, mut upper) = (0_usize, ENCODED_HALF_LINEAR_BITS.len());
    while lower < upper {
        let middle = lower + (upper - lower) / 2;
        let threshold = f64::from_bits(ENCODED_HALF_LINEAR_BITS[middle]);
        if value > threshold
            || value.to_bits() == threshold.to_bits() && (middle + 1).is_multiple_of(2)
        {
            lower = middle + 1;
        } else {
            upper = middle;
        }
    }
    u8::try_from(lower).map_err(|_| Error::Render("sRGB channel ordinal overflowed".into()))
}

/// Exact nearest-even binary64 cube root for the nonnegative LMS result of
/// one opaque RGB channel combination. Search the finite ordered binary64
/// bit domain, then compare the exact rational cube of the midpoint between
/// adjacent representable candidates. No host libm call, approximate fallback
/// or iteration-sensitive convergence threshold is involved.
fn cube_root(input: f64) -> Result<f64, Error> {
    if !input.is_finite() || !(0.0..=2.0).contains(&input) {
        return Err(Error::Render(
            "OKLab LMS component is outside its finite domain".into(),
        ));
    }
    if input == 0.0 {
        return Ok(0.0);
    }
    let (mut lower, mut upper) = (0_u64, 2.0_f64.to_bits());
    while upper - lower > 1 {
        let midpoint = lower + (upper - lower) / 2;
        let (significand, exponent) = binary_rational(f64::from_bits(midpoint));
        if compare_cube(significand, exponent, input).is_le() {
            lower = midpoint;
        } else {
            upper = midpoint;
        }
    }
    let (lo_sig, lo_exp) = binary_rational(f64::from_bits(lower));
    let (hi_sig, hi_exp) = binary_rational(f64::from_bits(upper));
    let common_exp = lo_exp.min(hi_exp);
    let middle_sig = (BigUint::from(lo_sig)
        << usize::try_from(lo_exp - common_exp).expect("lower exponent is above common exponent"))
        + (BigUint::from(hi_sig)
            << usize::try_from(hi_exp - common_exp)
                .expect("upper exponent is above common exponent"));
    let (target_sig, target_exp) = binary_rational(input);

    let cube = &middle_sig * &middle_sig * &middle_sig;
    let ordering = compare_rational(
        &cube,
        3 * (common_exp - 1),
        &BigUint::from(target_sig),
        target_exp,
    );
    Ok(f64::from_bits(
        if ordering.is_lt() || ordering.is_eq() && lower & 1 != 0 {
            upper
        } else {
            lower
        },
    ))
}

fn compare_cube(significand: u64, exponent: i32, input: f64) -> Ordering {
    let (target_sig, target_exp) = binary_rational(input);

    let value = BigUint::from(significand);
    compare_rational(
        &(&value * &value * &value),
        3 * exponent,
        &BigUint::from(target_sig),
        target_exp,
    )
}

fn binary_rational(value: f64) -> (u64, i32) {
    let bits = value.to_bits();
    let exponent = ((bits >> 52) & 0x7ff) as i32;
    let fraction = bits & ((1_u64 << 52) - 1);
    if exponent == 0 {
        (fraction, -1074)
    } else {
        (fraction | (1_u64 << 52), exponent - 1023 - 52)
    }
}

fn compare_rational(left: &BigUint, left_exp: i32, right: &BigUint, right_exp: i32) -> Ordering {
    if left_exp >= right_exp {
        (left << usize::try_from(left_exp - right_exp).expect("nonnegative exponent difference"))
            .cmp(right)
    } else {
        left.cmp(
            &(right
                << usize::try_from(right_exp - left_exp).expect("nonnegative exponent difference")),
        )
    }
}