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
// This file is part of helpers4.
// Copyright (C) 2025 baxyz
// SPDX-License-Identifier: LGPL-3.0-or-later
use Rgb;
/// Blends two colors channel by channel.
///
/// `t` is how far to go from `a` to `b`: `0.0` gives `a`, `1.0` gives `b`, `0.5` the midpoint. It
/// is clamped to `0.0..=1.0` (a `NaN` counts as `0.0`), and each channel is rounded to the nearest
/// integer. The blend happens in sRGB, without gamma correction, like CSS `color-mix(in srgb)`.
///
/// # Arguments
///
/// - `a` - The color at `t = 0`.
/// - `b` - The color at `t = 1`.
/// - `t` - How far from `a` towards `b`, `0.0..=1.0`.
///
/// # Returns
///
/// The blended color.
///
/// # Examples
///
/// ```
/// use helpers4::color::{mix, Rgb};
///
/// let red = Rgb::new(255, 0, 0);
/// let blue = Rgb::new(0, 0, 255);
/// assert_eq!(mix(red, blue, 0.5), Rgb::new(128, 0, 128));
/// assert_eq!(mix(red, blue, 0.0), red);
/// ```
// a blend of two u8s stays in 0..=255