use crate::channel::Channel;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum BlendMode {
#[default]
Normal,
Multiply,
Screen,
Overlay,
Darken,
Lighten,
ColorDodge,
ColorBurn,
HardLight,
SoftLight,
Difference,
Exclusion,
}
impl BlendMode {
pub const ALL: [Self; 12] = [
Self::Normal,
Self::Multiply,
Self::Screen,
Self::Overlay,
Self::Darken,
Self::Lighten,
Self::ColorDodge,
Self::ColorBurn,
Self::HardLight,
Self::SoftLight,
Self::Difference,
Self::Exclusion,
];
#[must_use]
pub fn mix<C: Channel>(self, cb: C, cs: C) -> C {
match self {
Self::Normal => cs,
Self::Multiply => cb.scale(cs),
Self::Screen => cb.complement().scale(cs.complement()).complement(),
Self::Overlay => Self::HardLight.mix(cs, cb),
Self::Darken => cb.min_channel(cs),
Self::Lighten => cb.max_channel(cs),
Self::ColorDodge => {
if cb.is_zero() {
C::ZERO
} else if cs.is_one() {
C::ONE
} else {
cb.div_normalized(cs.complement())
}
}
Self::ColorBurn => {
if cb.is_one() {
C::ONE
} else if cs.is_zero() {
C::ZERO
} else {
cb.complement().div_normalized(cs).complement()
}
}
Self::HardLight => {
if cs <= C::HALF {
Self::Multiply.mix(cb, cs.saturating_add(cs))
} else {
Self::Screen.mix(cb, cs.double_minus_one())
}
}
Self::SoftLight => soft_light(cb, cs),
Self::Difference => cb.abs_diff(cs),
Self::Exclusion => C::sum_of_products(cb, cs.complement(), cs, cb.complement()),
}
}
}
fn soft_light<C: Channel>(cb: C, cs: C) -> C {
if cs <= C::HALF {
let factor = cs.saturating_add(cs).complement();
cb.saturating_sub(factor.scale(cb).scale(cb.complement()))
} else {
let d = cb.soft_light_d();
cb.saturating_add(cs.double_minus_one().scale(d.saturating_sub(cb)))
}
}
#[cfg(test)]
#[allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::float_cmp,
clippy::suboptimal_flops
)]
mod tests {
use super::*;
#[allow(clippy::match_same_arms, clippy::suboptimal_flops)]
fn spec_reference(mode: BlendMode, cb: f64, cs: f64) -> f64 {
fn screen(cb: f64, cs: f64) -> f64 {
cb + cs - cb * cs
}
fn hard_light(cb: f64, cs: f64) -> f64 {
if cs <= 0.5 {
cb * (2.0 * cs)
} else {
screen(cb, 2.0 * cs - 1.0)
}
}
match mode {
BlendMode::Normal => cs,
BlendMode::Multiply => cb * cs,
BlendMode::Screen => screen(cb, cs),
BlendMode::Overlay => hard_light(cs, cb),
BlendMode::Darken => cb.min(cs),
BlendMode::Lighten => cb.max(cs),
BlendMode::ColorDodge => {
if cb == 0.0 {
0.0
} else if cs == 1.0 {
1.0
} else {
(cb / (1.0 - cs)).min(1.0)
}
}
BlendMode::ColorBurn => {
if cb == 1.0 {
1.0
} else if cs == 0.0 {
0.0
} else {
1.0 - ((1.0 - cb) / cs).min(1.0)
}
}
BlendMode::HardLight => hard_light(cb, cs),
BlendMode::SoftLight => {
let d = if cb <= 0.25 {
((16.0 * cb - 12.0) * cb + 4.0) * cb
} else {
cb.sqrt()
};
if cs <= 0.5 {
cb - (1.0 - 2.0 * cs) * cb * (1.0 - cb)
} else {
cb + (2.0 * cs - 1.0) * (d - cb)
}
}
BlendMode::Difference => (cb - cs).abs(),
BlendMode::Exclusion => cb + cs - 2.0 * cb * cs,
}
}
#[test]
fn all_contains_every_variant_once() {
for (i, a) in BlendMode::ALL.iter().enumerate() {
for b in &BlendMode::ALL[i + 1..] {
assert_ne!(a, b, "duplicate in ALL");
}
}
}
#[test]
fn default_is_normal() {
assert_eq!(BlendMode::default(), BlendMode::Normal);
}
#[test]
fn f32_matches_spec() {
for mode in BlendMode::ALL {
for cb in 0..=64u8 {
for cs in 0..=64u8 {
let (cb, cs) = (f32::from(cb) / 64.0, f32::from(cs) / 64.0);
let got = mode.mix(cb, cs);
let want = spec_reference(mode, f64::from(cb), f64::from(cs));
assert!(
(f64::from(got) - want).abs() < 1e-6,
"{mode:?}(cb={cb}, cs={cs}): got {got}, want {want}",
);
}
}
}
}
#[test]
fn u8_matches_spec_within_one_lsb() {
for mode in BlendMode::ALL {
let mut worst = 0u8;
for cb in 0..=255u8 {
for cs in 0..=255u8 {
let got = mode.mix(cb, cs);
let want = spec_reference(mode, f64::from(cb) / 255.0, f64::from(cs) / 255.0);
let want = (want.clamp(0.0, 1.0) * 255.0 + 0.5) as u8;
let delta = got.abs_diff(want);
assert!(
delta <= 1,
"{mode:?}(cb={cb}, cs={cs}): got {got}, want {want}",
);
worst = worst.max(delta);
}
}
let may_be_inexact = matches!(
mode,
BlendMode::ColorDodge | BlendMode::ColorBurn | BlendMode::SoftLight
);
assert!(
worst == 0 || may_be_inexact,
"{mode:?} became inexact (worst {worst})",
);
}
}
#[test]
fn normal_returns_the_source() {
for cb in 0..=255u8 {
for cs in 0..=255u8 {
assert_eq!(BlendMode::Normal.mix(cb, cs), cs);
}
}
}
#[test]
fn multiply_and_screen_are_complements() {
for cb in 0..=255u8 {
for cs in 0..=255u8 {
let multiply = BlendMode::Multiply.mix(cb, cs);
let screen = BlendMode::Screen.mix(cb.complement(), cs.complement());
assert_eq!(multiply, screen.complement(), "cb={cb} cs={cs}");
}
}
}
#[test]
fn overlay_is_hard_light_swapped() {
for cb in 0..=255u8 {
for cs in 0..=255u8 {
assert_eq!(
BlendMode::Overlay.mix(cb, cs),
BlendMode::HardLight.mix(cs, cb),
"cb={cb} cs={cs}",
);
}
}
}
#[test]
fn identity_elements() {
for cb in 0..=255u8 {
assert_eq!(BlendMode::Multiply.mix(cb, 255), cb, "multiply by white");
assert_eq!(BlendMode::Multiply.mix(cb, 0), 0, "multiply by black");
assert_eq!(BlendMode::Screen.mix(cb, 0), cb, "screen with black");
assert_eq!(BlendMode::Screen.mix(cb, 255), 255, "screen with white");
assert_eq!(BlendMode::Difference.mix(cb, 0), cb);
assert_eq!(BlendMode::Exclusion.mix(cb, 0), cb);
assert_eq!(BlendMode::Darken.mix(cb, 255), cb);
assert_eq!(BlendMode::Lighten.mix(cb, 0), cb);
assert_eq!(
BlendMode::SoftLight.mix(cb, 127),
cb,
"soft light is neutral at 0.5"
);
}
}
#[test]
fn multiply_by_white_never_drifts() {
let mut v = 200u8;
for _ in 0..1000 {
v = BlendMode::Multiply.mix(v, 255);
}
assert_eq!(v, 200);
}
#[test]
fn dodge_and_burn_edge_cases() {
assert_eq!(BlendMode::ColorDodge.mix(0u8, 128), 0, "Cb == 0 wins");
assert_eq!(
BlendMode::ColorDodge.mix(0u8, 255),
0,
"Cb == 0 beats Cs == 1"
);
assert_eq!(BlendMode::ColorDodge.mix(128u8, 255), 255);
assert_eq!(BlendMode::ColorDodge.mix(128u8, 128), 255, "saturates");
assert_eq!(BlendMode::ColorBurn.mix(255u8, 128), 255, "Cb == 1 wins");
assert_eq!(
BlendMode::ColorBurn.mix(255u8, 0),
255,
"Cb == 1 beats Cs == 0"
);
assert_eq!(BlendMode::ColorBurn.mix(128u8, 0), 0);
assert_eq!(BlendMode::ColorBurn.mix(128u8, 128), 2);
}
#[test]
fn hard_light_boundary() {
assert_eq!(BlendMode::HardLight.mix(200u8, 127), 200u8.scale(254));
assert_eq!(
BlendMode::HardLight.mix(200u8, 128),
BlendMode::Screen.mix(200, 1),
);
}
#[test]
fn hard_light_upper_half_does_not_saturate_away() {
assert_eq!(BlendMode::HardLight.mix(0u8, 129), 3);
assert_eq!(BlendMode::HardLight.mix(0u8, 255), 255);
assert_eq!(BlendMode::Overlay.mix(129u8, 0), 3);
for cs in 128..=255u8 {
assert_eq!(
BlendMode::HardLight.mix(0, cs),
cs.double_minus_one(),
"Screen with a black backdrop is the identity; cs={cs}",
);
}
}
#[test]
fn soft_light_is_bounded_by_the_backdrop_direction() {
for cb in 1..255u8 {
for cs in 0..=127u8 {
assert!(BlendMode::SoftLight.mix(cb, cs) <= cb, "cb={cb} cs={cs}");
}
for cs in 127..=255u8 {
assert!(BlendMode::SoftLight.mix(cb, cs) >= cb, "cb={cb} cs={cs}");
}
}
}
#[test]
fn saturating_modes_never_wrap() {
for cb in 0..=255u8 {
for cs in 0..=255u8 {
let _ = BlendMode::Exclusion.mix(cb, cs);
let _ = BlendMode::Difference.mix(cb, cs);
}
}
assert_eq!(BlendMode::Exclusion.mix(255u8, 255), 0);
assert_eq!(BlendMode::Difference.mix(255u8, 255), 0);
assert_eq!(BlendMode::Difference.mix(0u8, 255), 255);
assert_eq!(BlendMode::Exclusion.mix(4u8, 255), 251);
assert_eq!(BlendMode::Exclusion.mix(255u8, 4), 251);
}
}