grafo 0.16.1

A GPU-accelerated rendering library for Rust
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
use super::normalize::{NormalizedGradient, NormalizedSegment};
use super::types::{
    ColorInterpolation, GradientColor, GradientRamp, GradientRampSource, HueComponent,
    HueInterpolationMethod, RAMP_RESOLUTION, RESOLVED_DEGENERATE_EPSILON,
};
use std::sync::Arc;

/// Bakes a gradient ramp: RAMP_RESOLUTION texels of final linear premultiplied RGBA.
/// The texels span from `period_start` to `period_start + period_len` (non-repeating)
/// or from the first stop to the last stop (pad mode).
pub(crate) fn bake_gradient_ramp(ramp_source: &GradientRampSource) -> GradientRamp {
    let normalized = &ramp_source.normalized;
    let interpolation = &ramp_source.interpolation;

    if normalized.is_single_stop {
        let color = color_to_final_linear_premultiplied(&normalized.single_stop_color.unwrap());
        return GradientRamp::Constant(color);
    }

    let first_pos = normalized.stops.first().unwrap().position;
    let last_pos = normalized.stops.last().unwrap().position;
    let span = last_pos - first_pos;

    if span <= RESOLVED_DEGENERATE_EPSILON {
        if has_actual_zero_length_run(normalized) {
            return GradientRamp::Sampled(Arc::new(bake_degenerate_hard_stop_ramp(normalized)));
        }

        let color = color_to_final_linear_premultiplied(&normalized.stops.last().unwrap().color);
        return GradientRamp::Constant(color);
    }

    let mut ramp = [[0.0; 4]; RAMP_RESOLUTION];
    for (index, texel) in ramp.iter_mut().enumerate() {
        let t_normalized = index as f32 / (RAMP_RESOLUTION - 1) as f32;
        let u = first_pos + t_normalized * span;

        let color = evaluate_at_scalar(u, &normalized.segments, &normalized.stops, interpolation);
        *texel = color;
    }
    GradientRamp::Sampled(Arc::new(ramp))
}

fn has_actual_zero_length_run(normalized: &NormalizedGradient) -> bool {
    normalized.stops.len() > 1
        && normalized
            .stops
            .windows(2)
            .any(|pair| (pair[1].position - pair[0].position).abs() <= f32::EPSILON)
}

fn bake_degenerate_hard_stop_ramp(normalized: &NormalizedGradient) -> [[f32; 4]; RAMP_RESOLUTION] {
    let first_stop = normalized.stops.first().unwrap();
    let last_stop = normalized.stops.last().unwrap();

    let first_color = color_to_final_linear_premultiplied(&first_stop.color);
    let last_color = color_to_final_linear_premultiplied(&last_stop.color);
    let transition_index = RAMP_RESOLUTION / 2;

    let mut ramp = [[last_color[0], last_color[1], last_color[2], last_color[3]]; RAMP_RESOLUTION];
    ramp[..transition_index].fill(first_color);
    ramp
}

/// Evaluates the gradient at scalar `u` (after spread-mode folding).
/// Returns final linear premultiplied RGBA.
fn evaluate_at_scalar(
    u: f32,
    segments: &[NormalizedSegment],
    stops: &[super::normalize::NormalizedStop],
    interpolation: &ColorInterpolation,
) -> [f32; 4] {
    if segments.is_empty() {
        return color_to_final_linear_premultiplied(&stops.last().unwrap().color);
    }

    // Find the segment containing u
    // CSS rule: half-open [p_i, p_{i+1}), last stop takes final stop color
    let last_stop_pos = stops.last().unwrap().position;
    if u >= last_stop_pos {
        return color_to_final_linear_premultiplied(&stops.last().unwrap().color);
    }

    // Find segment: last segment where start_position <= u
    let mut segment_index = 0;
    for (i, seg) in segments.iter().enumerate() {
        if u >= seg.start_position {
            segment_index = i;
        } else {
            break;
        }
    }

    let segment = &segments[segment_index];

    // If the segment has zero length (hard stop), use last stop in coincident run
    let seg_len = segment.end_position - segment.start_position;
    if seg_len <= RESOLVED_DEGENERATE_EPSILON {
        return color_to_final_linear_premultiplied(&segment.end_color);
    }

    // Compute interpolation parameter with hint reparameterization
    let x = (u - segment.start_position) / seg_len;
    let p = apply_hint_reparameterization(x, segment.hint, seg_len, segment.start_position);

    interpolate_colors(&segment.start_color, &segment.end_color, p, interpolation)
}

/// Applies the CSS gradient hint reparameterization.
fn apply_hint_reparameterization(
    x: f32,
    hint: Option<f32>,
    segment_len: f32,
    segment_start: f32,
) -> f32 {
    let hint_value = match hint {
        None => return x.clamp(0.0, 1.0),
        Some(h) => h,
    };

    if x <= 0.0 {
        return 0.0;
    }
    if x >= 1.0 {
        return 1.0;
    }

    let m = (hint_value - segment_start) / segment_len;

    if (x - m).abs() < 1e-10 {
        return 0.5;
    }

    if x < m {
        let k0 = (0.5_f32).ln() / m.ln();
        0.5 * (x / m).powf(k0)
    } else {
        let k1 = (0.5_f32).ln() / (1.0 - m).ln();
        1.0 - 0.5 * ((1.0 - x) / (1.0 - m)).powf(k1)
    }
}

// ── Color interpolation ──────────────────────────────────────────────────────

/// Interpolates between two gradient colors at parameter p (0..1).
/// Returns final linear premultiplied RGBA.
fn interpolate_colors(
    color_a: &GradientColor,
    color_b: &GradientColor,
    p: f32,
    interpolation: &ColorInterpolation,
) -> [f32; 4] {
    match interpolation {
        ColorInterpolation::Srgb => interpolate_rectangular(color_a, color_b, p, RectSpace::Srgb),
        ColorInterpolation::SrgbLinear => {
            interpolate_rectangular(color_a, color_b, p, RectSpace::SrgbLinear)
        }
        ColorInterpolation::Oklab => interpolate_rectangular(color_a, color_b, p, RectSpace::Oklab),
        ColorInterpolation::Hsl { hue } => {
            interpolate_cylindrical(color_a, color_b, p, CylSpace::Hsl, *hue)
        }
        ColorInterpolation::Hwb { hue } => {
            interpolate_cylindrical(color_a, color_b, p, CylSpace::Hwb, *hue)
        }
    }
}

#[derive(Debug, Clone, Copy)]
enum RectSpace {
    Srgb,
    SrgbLinear,
    Oklab,
}

#[derive(Debug, Clone, Copy)]
enum CylSpace {
    Hsl,
    Hwb,
}

/// Rectangular (non-hue) interpolation pipeline as specified in the plan.
fn interpolate_rectangular(
    color_a: &GradientColor,
    color_b: &GradientColor,
    p: f32,
    space: RectSpace,
) -> [f32; 4] {
    // Step 1-2: Convert both colors into the interpolation space
    let [ra, ga, ba, aa] = to_rect_space(color_a, space);
    let [rb, gb, bb, ab] = to_rect_space(color_b, space);

    // Step 3: Premultiply
    let (pra, pga, pba) = (ra * aa, ga * aa, ba * aa);
    let (prb, pgb, pbb) = (rb * ab, gb * ab, bb * ab);

    // Step 4: Interpolate premultiplied channels and alpha
    let pr = pra + (prb - pra) * p;
    let pg = pga + (pgb - pga) * p;
    let pb = pba + (pbb - pba) * p;
    let alpha_p = aa + (ab - aa) * p;

    // Step 5-6: Unpremultiply if alpha > 0
    let (ur, ug, ub) = if alpha_p > 0.0 {
        (pr / alpha_p, pg / alpha_p, pb / alpha_p)
    } else {
        (0.0, 0.0, 0.0)
    };

    // Step 7: Convert to final linear output space
    let [lr, lg, lb] = rect_to_linear(ur, ug, ub, space);

    // Step 8: Premultiply with alpha_p for final output
    [lr * alpha_p, lg * alpha_p, lb * alpha_p, alpha_p]
}

fn interpolate_cylindrical(
    color_a: &GradientColor,
    color_b: &GradientColor,
    p: f32,
    space: CylSpace,
    hue_method: HueInterpolationMethod,
) -> [f32; 4] {
    // Step 1: Convert to HSL/HWB
    let (h0, c1_a, c2_a, a_a, h0_powerless) = to_cylindrical(color_a, space);
    let (h1, c1_b, c2_b, a_b, h1_powerless) = to_cylindrical(color_b, space);

    // Step 2: Resolve missing/powerless hue
    let (rh0, rh1) = resolve_hue_pair(h0, h0_powerless, h1, h1_powerless);

    // Step 3: Choose hue path
    let delta = compute_hue_delta(rh0, rh1, hue_method);

    // Step 4: Interpolate
    let h_interp = rem_euclid_f32(rh0 + delta * p, 360.0);
    let alpha_interp = a_a + (a_b - a_a) * p;
    let c1_a_p = c1_a * a_a;
    let c1_b_p = c1_b * a_b;
    let c2_a_p = c2_a * a_a;
    let c2_b_p = c2_b * a_b;
    let c1_p = c1_a_p + (c1_b_p - c1_a_p) * p;
    let c2_p = c2_a_p + (c2_b_p - c2_a_p) * p;
    let (c1_interp, c2_interp) = if alpha_interp > 0.0 {
        (c1_p / alpha_interp, c2_p / alpha_interp)
    } else {
        (0.0, 0.0)
    };

    // Step 5: Convert to final linear output space and premultiply
    let [lr, lg, lb] = cylindrical_to_linear(h_interp, c1_interp, c2_interp, space);
    let alpha_clamped = alpha_interp.clamp(0.0, 1.0);
    [
        lr * alpha_clamped,
        lg * alpha_clamped,
        lb * alpha_clamped,
        alpha_clamped,
    ]
}

// ── Color conversion helpers ─────────────────────────────────────────────────

/// Converts a GradientColor to the specified rectangular interpolation space.
/// Returns [channel0, channel1, channel2, alpha] with alpha clamped to [0,1].
fn to_rect_space(color: &GradientColor, space: RectSpace) -> [f32; 4] {
    // First get the color as [r, g, b, alpha] in sRGB space, handling
    // missing/powerless hue for HSL/HWB.
    let (srgb_r, srgb_g, srgb_b, alpha) = gradient_color_to_srgb(color);
    let alpha = alpha.clamp(0.0, 1.0);

    match space {
        RectSpace::Srgb => [srgb_r, srgb_g, srgb_b, alpha],
        RectSpace::SrgbLinear => {
            let lr = srgb_to_linear(srgb_r);
            let lg = srgb_to_linear(srgb_g);
            let lb = srgb_to_linear(srgb_b);
            [lr, lg, lb, alpha]
        }
        RectSpace::Oklab => {
            let lr = srgb_to_linear(srgb_r);
            let lg = srgb_to_linear(srgb_g);
            let lb = srgb_to_linear(srgb_b);
            let [ol, oa, ob] = linear_rgb_to_oklab(lr, lg, lb);
            [ol, oa, ob, alpha]
        }
    }
}

/// Converts from a rectangular interpolation space back to linear sRGB.
fn rect_to_linear(c0: f32, c1: f32, c2: f32, space: RectSpace) -> [f32; 3] {
    match space {
        RectSpace::Srgb => [srgb_to_linear(c0), srgb_to_linear(c1), srgb_to_linear(c2)],
        RectSpace::SrgbLinear => [c0, c1, c2],
        RectSpace::Oklab => oklab_to_linear_rgb(c0, c1, c2),
    }
}

/// Converts a GradientColor into (hue_degrees, component1, component2, alpha, is_powerless).
fn to_cylindrical(color: &GradientColor, space: CylSpace) -> (f32, f32, f32, f32, bool) {
    match (color, space) {
        (
            GradientColor::Hsl {
                hue,
                saturation,
                lightness,
                alpha,
            },
            CylSpace::Hsl,
        ) => {
            let s_clamped = saturation.clamp(0.0, 1.0);
            let l_clamped = lightness.clamp(0.0, 1.0);
            let (h_deg, is_powerless) = match hue {
                HueComponent::Degrees(deg) => {
                    let h = rem_euclid_f32(*deg, 360.0);
                    let powerless = s_clamped == 0.0 || l_clamped == 0.0 || l_clamped == 1.0;
                    (h, powerless)
                }
                HueComponent::Missing => (0.0, true),
            };
            (h_deg, s_clamped, l_clamped, *alpha, is_powerless)
        }
        (
            GradientColor::Hwb {
                hue,
                whiteness,
                blackness,
                alpha,
            },
            CylSpace::Hwb,
        ) => {
            let mut w = whiteness.max(0.0);
            let mut b = blackness.max(0.0);
            if w + b > 1.0 {
                let sum = w + b;
                w /= sum;
                b /= sum;
            }
            let (h_deg, is_powerless) = match hue {
                HueComponent::Degrees(deg) => {
                    let h = rem_euclid_f32(*deg, 360.0);
                    let powerless = w + b >= 1.0;
                    (h, powerless)
                }
                HueComponent::Missing => (0.0, true),
            };
            (h_deg, w, b, *alpha, is_powerless)
        }
        // Convert any other color space to HSL or HWB through sRGB
        (_, cyl_space) => {
            let (srgb_r, srgb_g, srgb_b, alpha) = gradient_color_to_srgb(color);
            match cyl_space {
                CylSpace::Hsl => {
                    let (h, s, l) = srgb_to_hsl(srgb_r, srgb_g, srgb_b);
                    let powerless = s == 0.0 || l == 0.0 || l == 1.0;
                    (h, s, l, alpha, powerless)
                }
                CylSpace::Hwb => {
                    let (h, w, b) = srgb_to_hwb(srgb_r, srgb_g, srgb_b);
                    let powerless = w + b >= 1.0;
                    (h, w, b, alpha, powerless)
                }
            }
        }
    }
}

fn cylindrical_to_linear(hue: f32, c1: f32, c2: f32, space: CylSpace) -> [f32; 3] {
    let (sr, sg, sb) = match space {
        CylSpace::Hsl => hsl_to_srgb(hue, c1, c2),
        CylSpace::Hwb => hwb_to_srgb(hue, c1, c2),
    };
    [srgb_to_linear(sr), srgb_to_linear(sg), srgb_to_linear(sb)]
}

fn resolve_hue_pair(h0: f32, h0_powerless: bool, h1: f32, h1_powerless: bool) -> (f32, f32) {
    match (h0_powerless, h1_powerless) {
        (false, false) => (h0, h1),
        (true, false) => (h1, h1),
        (false, true) => (h0, h0),
        (true, true) => (0.0, 0.0),
    }
}

fn compute_hue_delta(h0: f32, h1: f32, method: HueInterpolationMethod) -> f32 {
    match method {
        HueInterpolationMethod::Shorter => {
            let mut delta = rem_euclid_f32(h1 - h0 + 180.0, 360.0) - 180.0;
            if delta == -180.0 {
                delta = 180.0;
            }
            delta
        }
        HueInterpolationMethod::Longer => {
            let mut shorter = rem_euclid_f32(h1 - h0 + 180.0, 360.0) - 180.0;
            if shorter == -180.0 {
                shorter = 180.0;
            }
            if shorter == 180.0 {
                -180.0
            } else if shorter > 0.0 {
                shorter - 360.0
            } else {
                shorter + 360.0
            }
        }
        HueInterpolationMethod::Increasing => rem_euclid_f32(h1 - h0, 360.0),
        HueInterpolationMethod::Decreasing => rem_euclid_f32(h1 - h0, 360.0) - 360.0,
    }
}

// ── Color space conversion functions ─────────────────────────────────────────

/// Converts any GradientColor to sRGB (r, g, b, alpha).
/// Missing hue for HSL/HWB is treated as 0 for the purpose of conversion.
fn gradient_color_to_srgb(color: &GradientColor) -> (f32, f32, f32, f32) {
    match color {
        GradientColor::Srgb {
            red,
            green,
            blue,
            alpha,
        } => (*red, *green, *blue, *alpha),
        GradientColor::SrgbLinear {
            red,
            green,
            blue,
            alpha,
        } => (
            linear_to_srgb(*red),
            linear_to_srgb(*green),
            linear_to_srgb(*blue),
            *alpha,
        ),
        GradientColor::Oklab { l, a, b, alpha } => {
            let [lr, lg, lb] = oklab_to_linear_rgb(*l, *a, *b);
            (
                linear_to_srgb(lr),
                linear_to_srgb(lg),
                linear_to_srgb(lb),
                *alpha,
            )
        }
        GradientColor::Hsl {
            hue,
            saturation,
            lightness,
            alpha,
        } => {
            let s_clamped = saturation.clamp(0.0, 1.0);
            let l_clamped = lightness.clamp(0.0, 1.0);
            let h_deg = match hue {
                HueComponent::Degrees(deg) => rem_euclid_f32(*deg, 360.0),
                HueComponent::Missing => 0.0,
            };
            let (r, g, b) = hsl_to_srgb(h_deg, s_clamped, l_clamped);
            (r, g, b, *alpha)
        }
        GradientColor::Hwb {
            hue,
            whiteness,
            blackness,
            alpha,
        } => {
            let mut w = whiteness.max(0.0);
            let mut bk = blackness.max(0.0);
            if w + bk > 1.0 {
                let sum = w + bk;
                w /= sum;
                bk /= sum;
            }
            let h_deg = match hue {
                HueComponent::Degrees(deg) => rem_euclid_f32(*deg, 360.0),
                HueComponent::Missing => 0.0,
            };
            let (r, g, b) = hwb_to_srgb(h_deg, w, bk);
            (r, g, b, *alpha)
        }
    }
}

/// Convert a single authored GradientColor to final linear premultiplied RGBA.
pub(crate) fn color_to_final_linear_premultiplied(color: &GradientColor) -> [f32; 4] {
    let (sr, sg, sb, alpha) = gradient_color_to_srgb(color);
    let alpha = alpha.clamp(0.0, 1.0);
    let lr = srgb_to_linear(sr);
    let lg = srgb_to_linear(sg);
    let lb = srgb_to_linear(sb);
    [lr * alpha, lg * alpha, lb * alpha, alpha]
}

// ── sRGB transfer functions (extended) ───────────────────────────────────────

fn srgb_to_linear(c: f32) -> f32 {
    if c.abs() <= 0.04045 {
        c / 12.92
    } else {
        let sign = c.signum();
        sign * ((c.abs() + 0.055) / 1.055).powf(2.4)
    }
}

fn linear_to_srgb(c: f32) -> f32 {
    if c.abs() <= 0.0031308 {
        c * 12.92
    } else {
        let sign = c.signum();
        sign * (1.055 * c.abs().powf(1.0 / 2.4) - 0.055)
    }
}

// ── Oklab conversion ─────────────────────────────────────────────────────────

#[allow(clippy::excessive_precision)]
fn linear_rgb_to_oklab(r: f32, g: f32, b: f32) -> [f32; 3] {
    let l_ = 0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b;
    let m_ = 0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b;
    let s_ = 0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b;

    let l_c = cbrt(l_);
    let m_c = cbrt(m_);
    let s_c = cbrt(s_);

    [
        0.2104542553 * l_c + 0.7936177850 * m_c - 0.0040720468 * s_c,
        1.9779984951 * l_c - 2.4285922050 * m_c + 0.4505937099 * s_c,
        0.0259040371 * l_c + 0.7827717662 * m_c - 0.8086757660 * s_c,
    ]
}

#[allow(clippy::excessive_precision)]
fn oklab_to_linear_rgb(l: f32, a: f32, b: f32) -> [f32; 3] {
    let l_ = l + 0.3963377774 * a + 0.2158037573 * b;
    let m_ = l - 0.1055613458 * a - 0.0638541728 * b;
    let s_ = l - 0.0894841775 * a - 1.2914855480 * b;

    let l3 = l_ * l_ * l_;
    let m3 = m_ * m_ * m_;
    let s3 = s_ * s_ * s_;

    [
        4.0767416621 * l3 - 3.3077115913 * m3 + 0.2309699292 * s3,
        -1.2684380046 * l3 + 2.6097574011 * m3 - 0.3413193965 * s3,
        -0.0041960863 * l3 - 0.7034186147 * m3 + 1.7076147010 * s3,
    ]
}

fn cbrt(x: f32) -> f32 {
    if x >= 0.0 {
        x.powf(1.0 / 3.0)
    } else {
        -((-x).powf(1.0 / 3.0))
    }
}

// ── HSL/HWB conversions ──────────────────────────────────────────────────────

fn hsl_to_srgb(h: f32, s: f32, l: f32) -> (f32, f32, f32) {
    let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
    let h_prime = h / 60.0;
    let x = c * (1.0 - (h_prime % 2.0 - 1.0).abs());

    let (r1, g1, b1) = if h_prime < 1.0 {
        (c, x, 0.0)
    } else if h_prime < 2.0 {
        (x, c, 0.0)
    } else if h_prime < 3.0 {
        (0.0, c, x)
    } else if h_prime < 4.0 {
        (0.0, x, c)
    } else if h_prime < 5.0 {
        (x, 0.0, c)
    } else {
        (c, 0.0, x)
    };

    let m = l - c / 2.0;
    (r1 + m, g1 + m, b1 + m)
}

fn hwb_to_srgb(h: f32, w: f32, b: f32) -> (f32, f32, f32) {
    // HWB to sRGB: first get the pure hue from HSL with S=1, L=0.5
    let (r, g, bl) = hsl_to_srgb(h, 1.0, 0.5);
    // Then mix with white and black
    let r = r * (1.0 - w - b) + w;
    let g = g * (1.0 - w - b) + w;
    let bl = bl * (1.0 - w - b) + w;
    (r, g, bl)
}

fn srgb_to_hsl(r: f32, g: f32, b: f32) -> (f32, f32, f32) {
    let max = r.max(g).max(b);
    let min = r.min(g).min(b);
    let l = (max + min) / 2.0;

    if (max - min).abs() < 1e-10 {
        return (0.0, 0.0, l);
    }

    let d = max - min;
    let s = if l > 0.5 {
        d / (2.0 - max - min)
    } else {
        d / (max + min)
    };

    let h = if (max - r).abs() < 1e-10 {
        let mut h = (g - b) / d;
        if g < b {
            h += 6.0;
        }
        h
    } else if (max - g).abs() < 1e-10 {
        (b - r) / d + 2.0
    } else {
        (r - g) / d + 4.0
    };

    (rem_euclid_f32(h * 60.0, 360.0), s, l)
}

fn srgb_to_hwb(r: f32, g: f32, b: f32) -> (f32, f32, f32) {
    let (h, _, _) = srgb_to_hsl(r, g, b);
    let w = r.min(g).min(b);
    let bk = 1.0 - r.max(g).max(b);
    (h, w, bk)
}

pub(crate) fn rem_euclid_f32(a: f32, b: f32) -> f32 {
    ((a % b) + b) % b
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::gradient::normalize::NormalizedGradient;
    use crate::gradient::types::{
        ColorInterpolation, GradientColor, GradientCommonDesc, GradientKind, GradientRampSource,
        GradientStop, GradientStopOffset, GradientStopPositions, GradientUnits, SpreadMode,
    };

    fn srgb_color(red: f32, green: f32, blue: f32) -> GradientColor {
        GradientColor::Srgb {
            red,
            green,
            blue,
            alpha: 1.0,
        }
    }

    #[test]
    fn test_srgb_linear_roundtrip() {
        for v in [0.0, 0.04045, 0.5, 1.0, -0.5] {
            let linear = srgb_to_linear(v);
            let back = linear_to_srgb(linear);
            assert!(
                (v - back).abs() < 1e-5,
                "roundtrip failed for {v}: got {back}"
            );
        }
    }

    #[test]
    fn test_oklab_roundtrip() {
        let [ol, oa, ob] = linear_rgb_to_oklab(0.5, 0.3, 0.1);
        let [r, g, b] = oklab_to_linear_rgb(ol, oa, ob);
        assert!((r - 0.5).abs() < 1e-4);
        assert!((g - 0.3).abs() < 1e-4);
        assert!((b - 0.1).abs() < 1e-4);
    }

    #[test]
    fn test_hsl_srgb_roundtrip() {
        let (r, g, b) = hsl_to_srgb(120.0, 1.0, 0.5);
        assert!((r - 0.0).abs() < 1e-5);
        assert!((g - 1.0).abs() < 1e-5);
        assert!((b - 0.0).abs() < 1e-5);
    }

    #[test]
    fn test_rem_euclid() {
        assert!((rem_euclid_f32(-30.0, 360.0) - 330.0).abs() < 1e-5);
        assert!((rem_euclid_f32(370.0, 360.0) - 10.0).abs() < 1e-5);
    }

    #[test]
    fn bake_gradient_ramp_preserves_degenerate_hard_stop_boundary() {
        let common = GradientCommonDesc {
            units: GradientUnits::Local,
            spread: SpreadMode::Pad,
            interpolation: ColorInterpolation::Srgb,
            stops: vec![
                GradientStop {
                    positions: GradientStopPositions::Single(GradientStopOffset::LinearRadial(0.5)),
                    color: srgb_color(1.0, 0.0, 0.0),
                    hint_to_next_segment: None,
                },
                GradientStop {
                    positions: GradientStopPositions::Single(GradientStopOffset::LinearRadial(0.5)),
                    color: srgb_color(0.0, 0.0, 1.0),
                    hint_to_next_segment: None,
                },
            ]
            .into(),
        };

        let normalized = NormalizedGradient::from_common(&common, GradientKind::Linear);
        let ramp = bake_gradient_ramp(&GradientRampSource {
            interpolation: common.interpolation,
            normalized,
        });
        let ramp = ramp.as_slice();
        let transition_index = RAMP_RESOLUTION / 2;

        assert_eq!(
            ramp[transition_index - 1],
            color_to_final_linear_premultiplied(&srgb_color(1.0, 0.0, 0.0))
        );
        assert_eq!(
            ramp[transition_index],
            color_to_final_linear_premultiplied(&srgb_color(0.0, 0.0, 1.0))
        );
    }

    #[test]
    fn bake_gradient_ramp_does_not_create_hard_stop_for_near_degenerate_span() {
        let common = GradientCommonDesc {
            units: GradientUnits::Local,
            spread: SpreadMode::Pad,
            interpolation: ColorInterpolation::Srgb,
            stops: vec![
                GradientStop {
                    positions: GradientStopPositions::Single(GradientStopOffset::LinearRadial(0.0)),
                    color: srgb_color(1.0, 0.0, 0.0),
                    hint_to_next_segment: None,
                },
                GradientStop {
                    positions: GradientStopPositions::Single(GradientStopOffset::LinearRadial(
                        RESOLVED_DEGENERATE_EPSILON * 0.5,
                    )),
                    color: srgb_color(0.0, 0.0, 1.0),
                    hint_to_next_segment: None,
                },
            ]
            .into(),
        };

        let normalized = NormalizedGradient::from_common(&common, GradientKind::Linear);
        let ramp = bake_gradient_ramp(&GradientRampSource {
            interpolation: common.interpolation,
            normalized,
        });
        let ramp = ramp.as_slice();
        let expected = color_to_final_linear_premultiplied(&srgb_color(0.0, 0.0, 1.0));

        assert!(ramp.iter().all(|texel| *texel == expected));
    }

    #[test]
    fn cylindrical_interpolation_premultiplies_non_hue_channels() {
        let color_a = GradientColor::Hsl {
            hue: HueComponent::Degrees(0.0),
            saturation: 1.0,
            lightness: 0.5,
            alpha: 1.0,
        };
        let color_b = GradientColor::Hsl {
            hue: HueComponent::Degrees(120.0),
            saturation: 0.0,
            lightness: 1.0,
            alpha: 0.0,
        };

        let interpolated = interpolate_cylindrical(
            &color_a,
            &color_b,
            0.5,
            CylSpace::Hsl,
            HueInterpolationMethod::Shorter,
        );

        let expected = color_to_final_linear_premultiplied(&GradientColor::Hsl {
            hue: HueComponent::Degrees(0.0),
            saturation: 1.0,
            lightness: 0.5,
            alpha: 0.5,
        });

        assert_eq!(interpolated, expected);
    }
}