use crate::{stimulus::Stimulus, Alpha}; use super::{BlendFunction, PreAlpha, Premultiply}; /// Blending with a custom blend function. /// /// This is a convenience trait that makes it possible to use [`BlendFunction`] /// via a method on the source color. This makes custom blending more similar to /// how the [`Compose`][super::Compose] and [`Blend`][super::Blend] are used, /// including automatic pre-multiplication. pub trait BlendWith { /// The base color type of `Self`. type Color: Premultiply; /// Blend self, as the source color, with `destination`, using /// `blend_function`. Anything that implements [`BlendFunction`] is /// acceptable, including functions and closures. /// /// ``` /// use palette::{LinSrgb, LinSrgba}; /// use palette::blend::{BlendWith, PreAlpha}; /// /// type PreRgba = PreAlpha>; /// /// fn blend_mode(a: PreRgba, b: PreRgba) -> PreRgba { /// PreAlpha { /// color: LinSrgb::new(a.red * b.green, a.green * b.blue, a.blue * b.red), /// alpha: a.alpha * b.alpha, /// } /// } /// /// let a = LinSrgba::new(0.2, 0.5, 0.1, 0.8); /// let b = LinSrgba::new(0.6, 0.3, 0.5, 0.1); /// let c = a.blend_with(b, blend_mode); /// ``` #[must_use] fn blend_with(self, destination: Self, blend_function: F) -> Self where F: BlendFunction; } impl BlendWith for PreAlpha where C: Premultiply, { type Color = C; #[inline] fn blend_with(self, other: Self, blend_function: F) -> Self where F: BlendFunction, { blend_function.apply_to(self, other) } } impl BlendWith for Alpha where C: Premultiply, { type Color = C; fn blend_with(self, destination: Self, blend_function: F) -> Self where F: crate::blend::BlendFunction, { self.premultiply() .blend_with(destination.premultiply(), blend_function) .unpremultiply() } } impl BlendWith for C where C: Premultiply, C::Scalar: Stimulus, { type Color = C; fn blend_with(self, other: Self, blend_function: F) -> Self where F: BlendFunction, { PreAlpha::new_opaque(self) .blend_with(PreAlpha::new_opaque(other), blend_function) .unpremultiply() .color } }