Debian Codegraph @

src/blend.rs

raw symbols SCIP
1 //! Color blending and blending equations.
2 //!
3 //! Palette offers both OpenGL style blending equations, as well as most of the
4 //! SVG composition operators (also common in photo manipulation software). The
5 //! composition operators are all implemented in the [`Compose`] and [`Blend`]
6 //! traits, and ready to use with any appropriate color type:
7 //!
8 //! ```
9 //! use palette::{blend::Blend, LinSrgba};
10 //!
11 //! let a = LinSrgba::new(0.2, 0.5, 0.1, 0.8);
12 //! let b = LinSrgba::new(0.6, 0.3, 0.5, 0.1);
13 //! let c = a.overlay(b);
14 //! ```
15 //!
16 //! Blending equations can be defined using the [`Equations`] type, which is
17 //! then passed to the `blend` function, from the `Blend` trait:
18 //!
19 //! ```
20 //! use palette::LinSrgba;
21 //! use palette::blend::{BlendWith, Equations, Parameter};
22 //!
23 //! let blend_mode = Equations::from_parameters(
24 //! Parameter::SourceAlpha,
25 //! Parameter::OneMinusSourceAlpha
26 //! );
27 //!
28 //! let a = LinSrgba::new(0.2, 0.5, 0.1, 0.8);
29 //! let b = LinSrgba::new(0.6, 0.3, 0.5, 0.1);
30 //! let c = a.blend_with(b, blend_mode);
31 //! ```
32 //!
33 //! Note that blending will use [premultiplied alpha](crate::blend::PreAlpha),
34 //! which may result in loss of some color information in some cases. One such
35 //! case is that a completely transparent resultant color will become black.
36
37 use craterust-analyzer cargo palette 0.7.6 crate/::{
38 castrust-analyzer cargo palette 0.7.6 cast/::{selfrust-analyzer cargo palette 0.7.6 cast/, ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#},
39 clamprust-analyzer cargo palette 0.7.6 clamp().,
40 numrust-analyzer cargo palette 0.7.6 num/::{Arithmeticsrust-analyzer cargo palette 0.7.6 num/Arithmetics#, Clamprust-analyzer cargo palette 0.7.6 num/Clamp#, Onerust-analyzer cargo palette 0.7.6 num/One#, Realrust-analyzer cargo palette 0.7.6 num/Real#, Zerorust-analyzer cargo palette 0.7.6 num/Zero#},
41 stimulusrust-analyzer cargo palette 0.7.6 stimulus/::Stimulusrust-analyzer cargo palette 0.7.6 stimulus/Stimulus#,
42 };
43
44 pub use selfrust-analyzer cargo palette 0.7.6 blend/::{
45 blendrust-analyzer cargo palette 0.7.6 blend/blend/::Blendrust-analyzer cargo palette 0.7.6 blend/blend/Blend#,
46 blend_withrust-analyzer cargo palette 0.7.6 blend/blend_with/::BlendWithrust-analyzer cargo palette 0.7.6 blend/blend_with/BlendWith#,
47 composerust-analyzer cargo palette 0.7.6 blend/compose/::Composerust-analyzer cargo palette 0.7.6 blend/compose/Compose#,
48 equationsrust-analyzer cargo palette 0.7.6 blend/equations/::{Equationrust-analyzer cargo palette 0.7.6 blend/equations/Equation#, Equationsrust-analyzer cargo palette 0.7.6 blend/equations/Equations#, Parameterrust-analyzer cargo palette 0.7.6 blend/equations/Parameter#, Parametersrust-analyzer cargo palette 0.7.6 blend/equations/Parameters#},
49 pre_alpharust-analyzer cargo palette 0.7.6 blend/pre_alpha/::PreAlpharust-analyzer cargo palette 0.7.6 blend/pre_alpha/PreAlpha#,
50 };
51
52 #[allow(clippy::module_inception)]
53 mod blendrust-analyzer cargo palette 0.7.6 blend/blend/;
54 mod blend_withrust-analyzer cargo palette 0.7.6 blend/blend_with/;
55 mod composerust-analyzer cargo palette 0.7.6 blend/compose/;
56 mod equationsrust-analyzer cargo palette 0.7.6 blend/equations/;
57 mod pre_alpharust-analyzer cargo palette 0.7.6 blend/pre_alpha/;
58
59 #[cfg(test)]
60 mod testrust-analyzer cargo palette 0.7.6 blend/test/;
61
62 /// A trait for custom blend functions.
63 pub trait BlendFunctionrust-analyzer cargo palette 0.7.6 blend/BlendFunction#<C>
64 where
65 C: Premultiplyrust-analyzer cargo palette 0.7.6 blend/Premultiply#,
66 {
67 /// Apply this blend function to a pair of colors.
68 #[must_use]
69 fn apply_torust-analyzer cargo palette 0.7.6 blend/BlendFunction#apply_to().(self, source: PreAlpharust-analyzer cargo palette 0.7.6 blend/pre_alpha/PreAlpha#<C>, destination: PreAlpharust-analyzer cargo palette 0.7.6 blend/pre_alpha/PreAlpha#<C>) -> PreAlpharust-analyzer cargo palette 0.7.6 blend/pre_alpha/PreAlpha#<C>;
70 }
71
72 impl<C, F> BlendFunctionrust-analyzer cargo palette 0.7.6 blend/BlendFunction#<C> for F
73 where
74 C: Premultiplyrust-analyzer cargo palette 0.7.6 blend/Premultiply#,
75 F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(PreAlpharust-analyzer cargo palette 0.7.6 blend/pre_alpha/PreAlpha#<C>, PreAlpharust-analyzer cargo palette 0.7.6 blend/pre_alpha/PreAlpha#<C>) -> PreAlpharust-analyzer cargo palette 0.7.6 blend/pre_alpha/PreAlpha#<C>,
76 {
77 #[inline]
78 fn apply_torust-analyzer cargo palette 0.7.6 blend/impl#[F][`BlendFunction<C>`]apply_to().(self, source: PreAlpharust-analyzer cargo palette 0.7.6 blend/pre_alpha/PreAlpha#<C>, destination: PreAlpharust-analyzer cargo palette 0.7.6 blend/pre_alpha/PreAlpha#<C>) -> PreAlpharust-analyzer cargo palette 0.7.6 blend/pre_alpha/PreAlpha#<C> {
79 (self)(source, destination)
80 }
81 }
82
83 /// Alpha masking and unmasking.
84 pub trait Premultiplyrust-analyzer cargo palette 0.7.6 blend/Premultiply#: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# {
85 /// The color's component type.
86 type Scalarrust-analyzer cargo palette 0.7.6 blend/Premultiply#Scalar#: Realrust-analyzer cargo palette 0.7.6 num/Real# + Stimulusrust-analyzer cargo palette 0.7.6 stimulus/Stimulus#;
87
88 /// Alpha mask the color.
89 ///
90 /// This is done by multiplying the color's component by `alpha`.
91 #[must_use]
92 fn premultiplyrust-analyzer cargo palette 0.7.6 blend/Premultiply#premultiply().(self, alpha: Self::Scalarrust-analyzer cargo palette 0.7.6 blend/Premultiply#Scalar#) -> PreAlpharust-analyzer cargo palette 0.7.6 blend/pre_alpha/PreAlpha#<Self>;
93
94 /// Alpha unmask the color, resulting in a color and transparency pair.
95 ///
96 /// This is done by dividing the masked color's component by `alpha`, or
97 /// returning a black color if `alpha` is `0`.
98 #[must_use]
99 fn unpremultiplyrust-analyzer cargo palette 0.7.6 blend/Premultiply#unpremultiply().(premultiplied: PreAlpharust-analyzer cargo palette 0.7.6 blend/pre_alpha/PreAlpha#<Self>) -> (Self, Self::Scalarrust-analyzer cargo palette 0.7.6 blend/Premultiply#Scalar#);
100 }
101
102 fn blend_alpharust-analyzer cargo palette 0.7.6 blend/blend_alpha().<T>(src: T, dst: T) -> T
103 where
104 T: Zerorust-analyzer cargo palette 0.7.6 num/Zero# + Onerust-analyzer cargo palette 0.7.6 num/One# + Arithmeticsrust-analyzer cargo palette 0.7.6 num/Arithmetics# + Clamprust-analyzer cargo palette 0.7.6 num/Clamp# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#,
105 {
106 clamprust-analyzer cargo palette 0.7.6 clamp().(src.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().&dst rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().src rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().dst, T::zerorust-analyzer cargo palette 0.7.6 num/Zero#zero().(), T::onerust-analyzer cargo palette 0.7.6 num/One#one().())
107 }
108
109 fn zip_colorsrust-analyzer cargo palette 0.7.6 blend/zip_colors().<'a, C, T, const N: usize>(
110 src: C,
111 dst: &'a mut C,
112 ) -> impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = (T, &'a mut T)>
113 where
114 C: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#<Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# = [T; N]>,
115 T: 'a,
116 {
117 IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#::into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().(castrust-analyzer cargo palette 0.7.6 cast/::into_arrayrust-analyzer cargo palette 0.7.6 cast/array/into_array().(src)).ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(castrust-analyzer cargo palette 0.7.6 cast/::into_array_mutrust-analyzer cargo palette 0.7.6 cast/array/into_array_mut().(dst))
118 }