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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! Alpha blending and compositing in (optionally) zero-dependency Rust.
//!
//! There are three pieces:
//!
//! - A [`CompositeMode`] decides **how much** of the source and destination survive. These are
//! the Porter-Duff operators: [`SourceOver`](CompositeMode::SourceOver),
//! [`Xor`](CompositeMode::Xor), and so on.
//! - A [`BlendMode`] decides **how** their colors are mixed. These are the W3C separable blend
//! modes: [`Multiply`](BlendMode::Multiply), [`Screen`](BlendMode::Screen),
//! [`SoftLight`](BlendMode::SoftLight), and so on.
//! - A [`Composite`] pairs the two. Either half can be used on its own, in which case the other
//! takes its default.
//!
//! Everything is generic over [`channel::Channel`], so [`u8`] and [`f32`] pixels go
//! through the same code at their own precision. There is no separate integer implementation to
//! drift out of sync with the float one.
//!
//! All blending assumes **straight (un-premultiplied) alpha** unless documented otherwise. See
//! [`rgba::Premultiplied`] for the other convention.
//!
//! ## Examples
//!
//! ```rust
//! use alpha_blend::{CompositeMode, RgbaBlend, rgba::F32x4Rgba};
//!
//! let src = F32x4Rgba { r: 1.0, g: 0.0, b: 0.0, a: 0.5 }; // Semi-transparent red
//! let dst = F32x4Rgba { r: 0.0, g: 0.0, b: 1.0, a: 1.0 }; // Opaque blue
//! let blended = CompositeMode::SourceOver.apply(src, dst);
//!
//! // Half red over opaque blue: an even mix, still opaque.
//! assert_eq!(blended, F32x4Rgba { r: 0.5, g: 0.0, b: 0.5, a: 1.0 });
//! ```
//!
//! The same operation on `u8` pixels, and with a blend mode applied:
//!
//! ```rust
//! use alpha_blend::{BlendMode, Composite, CompositeMode, RgbaBlend, rgba::U8x4Rgba};
//!
//! let src = U8x4Rgba::new(255, 128, 0, 128);
//! let dst = U8x4Rgba::new(0, 64, 255, 255);
//!
//! // Plain source-over has a dedicated integer fast path.
//! assert_eq!(CompositeMode::SourceOver.apply(src, dst), src.source_over(dst));
//!
//! // Multiply, composited source-over.
//! let multiply = Composite::new(BlendMode::Multiply, CompositeMode::SourceOver);
//! assert_eq!(multiply.apply(src, dst), U8x4Rgba::new(0, 48, 127, 255));
//! ```
//!
//! ## Rounding
//!
//! Integer channels round to nearest, once per operation, from a widened intermediate.
//! `scale(255, 255) == 255`, and compositing an opaque pixel repeatedly is idempotent. See the
//! [`channel`] module docs for the exact rules and why the usual `>> 8` shift trick is not used.
//!
//! ## Features
//!
//! By default, this crate is `no_std` compatible, and uses [`libm`] for some math operations.
//!
//! Either `std` or `libm` must be enabled.
//!
//! ### `bytemuck`
//!
//! Enables the `bytemuck` crate for zero-copy conversions between types.
//!
//! ### `libm`
//!
//! _This feature is enabled by default._
//!
//! Uses the `libm` crate for math operations.
//!
//! ### `libm-arch`
//!
//! _This feature is enabled by default._
//!
//! Enables the `arch` feature of `libm`.
//!
//! ### `std`
//!
//! Uses the standard library for math operations, such as `f32::round`.
use crate::;
pub
pub use crate::;
/// Blends pixel colors using alpha compositing.
///
/// Implemented by [`Composite`] and, for convenience, by [`BlendMode`] and [`CompositeMode`]
/// on their own. Implement it yourself to provide a specialized or vectorized path; overriding
/// [`RgbaBlend::apply_slice`] is the usual reason to.