aest 0.1.0
Collection of C++ utility libraries
Loading...
Searching...
No Matches
Compositing.cppm
Go to the documentation of this file.
1
8module;
9#include <array>
10#include <concepts>
11#include <string_view>
12export module aest.colors:compositing;
13
14import :color;
15import :converter;
16import :gamut;
17import :space;
18
19namespace aest::colors
20{
29export template <typename T>
30concept LinearSpace = ColorType<T> && std::derived_from<T, ColorArithmetic<T>>;
31
45export template <LinearSpace Space>
46class Premultiplied final : public ColorBase<Premultiplied<Space>>
47{
49 static constexpr std::string_view TYPE_PREFIX = "Premultiplied<";
50
52 static constexpr auto TYPE_CHARS =
53 join_names<TYPE_PREFIX.size() + Space::TYPE.size() + 1>({TYPE_PREFIX, Space::TYPE, ">"});
54
55public:
57 static constexpr std::string_view TYPE{TYPE_CHARS.data(), TYPE_CHARS.size()};
58
60 constexpr Premultiplied()
61 : Premultiplied(Space{0.0f, 0.0f, 0.0f, 1.0f})
62 {}
63
72 constexpr Premultiplied(const float ch1, const float ch2, const float ch3, const float a = 1.0f)
73 : Premultiplied(Space{ch1, ch2, ch3, a})
74 {}
75
78
85 friend constexpr Premultiplied operator+(const Premultiplied& lhs,
86 const Premultiplied& rhs) noexcept
87 {
88 return Premultiplied{lhs.ch1() + rhs.ch1(), lhs.ch2() + rhs.ch2(), lhs.ch3() + rhs.ch3(),
89 lhs.alpha() + rhs.alpha()};
90 }
91
99 friend constexpr Premultiplied operator*(const Premultiplied& color,
100 const float scalar) noexcept
101 {
102 return Premultiplied{color.ch1() * scalar, color.ch2() * scalar, color.ch3() * scalar,
103 color.alpha() * scalar};
104 }
105
113 friend constexpr Premultiplied operator*(const float scalar,
114 const Premultiplied& color) noexcept
115 {
116 return color * scalar;
117 }
118
124 constexpr Premultiplied& operator+=(const Premultiplied& other) noexcept
125 {
126 return *this = *this + other;
127 }
128
135 constexpr Premultiplied& operator*=(const float scalar) noexcept
136 {
137 return *this = *this * scalar;
138 }
139
140
141private:
146 explicit constexpr Premultiplied(const Space& clamped)
147 : ColorBase<Premultiplied>(clamped.alpha(), clamped.ch1(), clamped.ch2(), clamped.ch3())
148 {}
149};
150
155template <LinearSpace Space>
157{
159 using type = Space;
160};
161
166template <LinearSpace Space>
167struct Converter<Premultiplied<Space>, Space>
168{
174 static constexpr Premultiplied<Space> convert(const Space& color)
175 {
176 const float alpha = color.alpha();
177 return Premultiplied<Space>{color.ch1() * alpha, color.ch2() * alpha, color.ch3() * alpha,
178 alpha};
179 }
180};
181
186template <LinearSpace Space>
187struct Converter<Space, Premultiplied<Space>>
188{
195 static constexpr Space convert(const Premultiplied<Space>& color)
196 {
197 const float alpha = color.alpha();
198 if (alpha == 0)
199 return Space{0.0f, 0.0f, 0.0f, 0.0f};
200
201 return Space{color.ch1() / alpha, color.ch2() / alpha, color.ch3() / alpha, alpha};
202 }
203};
204
209template <LinearSpace Space>
211{
213 using type = Space;
214};
215
237export template <typename Space = void, ColorType Foreground, ColorType Background>
242constexpr Background over(const Foreground& foreground,
243 const Background& background,
244 [[maybe_unused]] In<Space> space = {})
245{
246 using Working = Premultiplied<WorkingSpace<Space, Background>>;
247
248 const Working front = convert<Working>(foreground);
249 return convert<Background>(front + convert<Working>(background) * (1 - front.alpha()));
250}
251} // namespace aest::colors
constexpr std::array< char, Size > join_names(const std::initializer_list< std::string_view > parts)
Concatenate names into a character array, for a color space name built at compile time.
Definition Color.cppm:100
constexpr Background over(const Foreground &foreground, const Background &background, In< Space > space={})
Composites a color over another: Porter-Duff source-over.
A color of a linear space with premultiplied alpha: each color channel is already multiplied by alpha...
constexpr Premultiplied & operator+=(const Premultiplied &other) noexcept
Adds another color to this color in place.
friend constexpr Premultiplied operator+(const Premultiplied &lhs, const Premultiplied &rhs) noexcept
Adds two colors, alpha included, as when one adds its light and coverage on top of the other.
constexpr Premultiplied(const Space &clamped)
Construct a color from channels already clamped by Space.
constexpr Premultiplied & operator*=(const float scalar) noexcept
Scales every channel of this color in place, alpha included.
static constexpr std::string_view TYPE_PREFIX
Beginning of the space name, before the name of Space.
constexpr Premultiplied()
Default constructor initializes the color to opaque black (0, 0, 0, 1).
friend constexpr Premultiplied operator*(const Premultiplied &color, const float scalar) noexcept
Scales every channel of a color, alpha included, as when fading it.
static constexpr std::string_view TYPE
Color space name, used by the std::formatter specialization.
constexpr Premultiplied(const float ch1, const float ch2, const float ch3, const float a=1.0f)
Construct a color from premultiplied channel values.
static constexpr auto TYPE_CHARS
The space name, Premultiplied< + Space::TYPE + >.
friend constexpr Premultiplied operator*(const float scalar, const Premultiplied &color) noexcept
Scales every channel of a color, alpha included, as when fading it.
Concept to check if a type is a color.
Definition Color.cppm:448
A color space that convert() can convert ColorIn to: directly through a Converter,...
A color space whose channels are proportional to light, so that adding and scaling them is physically...
constexpr float ch2() const noexcept
Definition Color.cppm:191
constexpr float ch1() const noexcept
Definition Color.cppm:183
constexpr float ch3() const noexcept
Definition Color.cppm:199
constexpr ColorBase(const float alpha, const float channel1, const float channel2, const float channel3)
Definition Color.cppm:158
constexpr float alpha() const noexcept
Definition Color.cppm:175
static constexpr Premultiplied< Space > convert(const Space &color)
Multiplies each color channel of color by its alpha.
static constexpr Space convert(const Premultiplied< Space > &color)
Divides each color channel of color by its alpha.
Converts colors from one color space to another.
Tag naming the color space an operation works in.
Definition Space.cppm:30
The linear space that operations in linear light, such as mix() and over(), use for a color type when...
Definition Space.cppm:53
Names the color space a space converts through, joining every space in one tree.