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