alpha_blend/lib.rs
1//! Alpha blending and compositing in (optionally) zero-dependency Rust.
2//!
3//! ## Features
4//!
5//! By default, this crate is `no_std` compatible, and uses [`libm`] for some math operations.
6//!
7//! ### `std`
8//!
9//! Uses the standard library for math operations, such as `f32::round`.
10//!
11//! Removes the dependency on `libm`.
12//!
13//! ### `bytemuck`
14//!
15//! Enables the `bytemuck` crate for zero-copy conversions between types.
16//!
17//! ### `libm`
18//!
19//! _This feature is enabled by default._
20//!
21//! Uses the `libm` crate for math operations.
22//!
23//! ### `libm-arch`
24//!
25//! _This feature is enabled by default._
26//!
27//! Enables the `arch` feature of `libm`.
28
29#![cfg_attr(not(feature = "std"), no_std)]
30
31use crate::{porter_duff::PorterDuff, rgba::Rgba};
32
33pub(crate) mod math;
34pub mod porter_duff;
35pub mod rgba;
36pub(crate) mod vec4;
37
38/// Supported blend modes by this crate.
39#[derive(Debug, Clone, Copy, PartialEq, Eq)]
40pub enum BlendMode {
41 /// Destination pixels covered by the source pixels are cleared.
42 Clear,
43
44 /// Source pixels are copied to the destination.
45 Source,
46
47 /// Destination pixels are copied to the source.
48 Destination,
49
50 /// Source pixels are copied to the destination, ignoring the alpha channel.
51 SourceOver,
52
53 /// Destination pixels are copied to the source, ignoring the alpha channel.
54 DestinationOver,
55
56 /// Source pixels are copied to the destination, only where the destination is opaque.
57 SourceIn,
58
59 /// Destination pixels are copied to the source, only where the source is opaque.
60 DestinationIn,
61
62 /// Source pixels are copied to the destination, only where the source is opaque.
63 SourceOut,
64
65 /// Destination pixels are copied to the source, only where the destination is opaque.
66 DestinationOut,
67
68 /// Source pixels are copied to the destination, where both source and destination are opaque.
69 SourceAtop,
70
71 /// Destination pixels are copied to the source, where both source and destination are opaque.
72 DestinationAtop,
73
74 /// Source pixels are blended with the destination using the source's alpha channel.
75 Xor,
76
77 /// Source pixels are added to the destination.
78 Plus,
79}
80
81impl BlendMode {
82 /// Returns an [`RgbaBlend`] implementation for this blend mode.
83 #[must_use]
84 fn as_rgba_blend_f32(&self) -> impl RgbaBlend<Channel = f32> {
85 match self {
86 BlendMode::Clear => PorterDuff::CLEAR,
87 BlendMode::Source => PorterDuff::SRC,
88 BlendMode::Destination => PorterDuff::DST,
89 BlendMode::SourceOver => PorterDuff::SRC_OVER,
90 BlendMode::DestinationOver => PorterDuff::DST_OVER,
91 BlendMode::SourceIn => PorterDuff::SRC_IN,
92 BlendMode::DestinationIn => PorterDuff::DST_IN,
93 BlendMode::SourceOut => PorterDuff::SRC_OUT,
94 BlendMode::DestinationOut => PorterDuff::DST_OUT,
95 BlendMode::SourceAtop => PorterDuff::SRC_ATOP,
96 BlendMode::DestinationAtop => PorterDuff::DST_ATOP,
97 BlendMode::Xor => PorterDuff::XOR,
98 BlendMode::Plus => PorterDuff::PLUS,
99 }
100 }
101}
102
103impl RgbaBlend for BlendMode {
104 type Channel = f32;
105
106 fn apply(&self, src: Rgba<Self::Channel>, dst: Rgba<Self::Channel>) -> Rgba<Self::Channel> {
107 self.as_rgba_blend_f32().apply(src, dst)
108 }
109}
110
111/// Blends pixel colors using alpha compositing.
112pub trait RgbaBlend {
113 /// What type of channel this blend mode operates on.
114 type Channel: Copy;
115
116 /// Blends two colors together using this blend mode.
117 fn apply(&self, src: Rgba<Self::Channel>, dst: Rgba<Self::Channel>) -> Rgba<Self::Channel>;
118}