Skip to content

mix

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).

use helpers4::color::mix;

Cargo feature color (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features color

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["color"] }
pub fn mix(a: Rgb, b: Rgb, t: f64) -> Rgb
ParameterTypeDescription
aRgbThe color at t = 0.
bRgbThe color at t = 1.
tf64How far from a towards b, 0.0..=1.0.

Rgb — The blended color.

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);

src/color/mix.rs