tiny-skia-cpp
A C++20 2D rendering library (port of tiny-skia)
Loading...
Searching...
No Matches
Color.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <array>
7#include <cstdint>
8#include <optional>
9
11#include "tiny_skia/Math.h"
12#include "tiny_skia/pipeline/Pipeline.h"
13
14namespace tiny_skia {
15
17using AlphaU8 = std::uint8_t;
18
19constexpr AlphaU8 kAlphaU8Transparent = 0x00;
20constexpr AlphaU8 kAlphaU8Opaque = 0xFF;
21
22class Color;
25
27class ColorU8 {
28 public:
29 constexpr ColorU8() = default;
30 constexpr ColorU8(AlphaU8 red, AlphaU8 green, AlphaU8 blue, AlphaU8 alpha)
31 : data_{red, green, blue, alpha} {}
32
33 static constexpr ColorU8 fromRgba(AlphaU8 red, AlphaU8 green, AlphaU8 blue, AlphaU8 alpha) {
34 return ColorU8(red, green, blue, alpha);
35 }
36
37 constexpr AlphaU8 red() const { return data_[0]; }
38 constexpr AlphaU8 green() const { return data_[1]; }
39 constexpr AlphaU8 blue() const { return data_[2]; }
40 constexpr AlphaU8 alpha() const { return data_[3]; }
41
42 [[nodiscard]] bool isOpaque() const { return alpha() == kAlphaU8Opaque; }
43
45 [[nodiscard]] PremultipliedColorU8 premultiply() const;
46
47 bool operator==(const ColorU8&) const = default;
48
49 private:
50 std::array<AlphaU8, 4> data_{};
51};
52
55 public:
56 static const PremultipliedColorU8 transparent;
57 constexpr PremultipliedColorU8() = default;
58
60 static std::optional<PremultipliedColorU8> fromRgba(AlphaU8 red, AlphaU8 green, AlphaU8 blue,
61 AlphaU8 alpha);
64 AlphaU8 alpha) {
65 return PremultipliedColorU8(red, green, blue, alpha);
66 }
67
68 constexpr PremultipliedColorU8(AlphaU8 red, AlphaU8 green, AlphaU8 blue, AlphaU8 alpha)
69 : data_{red, green, blue, alpha} {}
70
71 constexpr AlphaU8 red() const { return data_[0]; }
72 constexpr AlphaU8 green() const { return data_[1]; }
73 constexpr AlphaU8 blue() const { return data_[2]; }
74 constexpr AlphaU8 alpha() const { return data_[3]; }
75
76 [[nodiscard]] bool isOpaque() const { return alpha() == kAlphaU8Opaque; }
77
79 [[nodiscard]] ColorU8 demultiply() const;
80
81 bool operator==(const PremultipliedColorU8&) const = default;
82
83 private:
84 std::array<AlphaU8, 4> data_{};
85};
86
88class Color {
89 public:
90 Color() = default;
91 static const Color transparent;
92 static const Color black;
93 static const Color white;
94
95 constexpr Color(NormalizedF32 red, NormalizedF32 green, NormalizedF32 blue, NormalizedF32 alpha)
96 : red_(red), green_(green), blue_(blue), alpha_(alpha) {}
97
99 static Color fromRgbaUnchecked(float red, float green, float blue, float alpha);
101 static std::optional<Color> fromRgba(float red, float green, float blue, float alpha);
103 static Color fromRgba8(AlphaU8 red, AlphaU8 green, AlphaU8 blue, AlphaU8 alpha);
104
105 float red() const { return red_.get(); }
106 float green() const { return green_.get(); }
107 float blue() const { return blue_.get(); }
108 float alpha() const { return alpha_.get(); }
109
110 void setRed(float value) { red_ = NormalizedF32::newClamped(value); }
111 void setGreen(float value) { green_ = NormalizedF32::newClamped(value); }
112 void setBlue(float value) { blue_ = NormalizedF32::newClamped(value); }
113 void setAlpha(float value) { alpha_ = NormalizedF32::newClamped(value); }
114
116 void applyOpacity(float opacity) {
117 alpha_ = NormalizedF32::newClamped(alpha_.get() * bound(0.0f, opacity, 1.0f));
118 }
119
120 [[nodiscard]] bool isOpaque() const { return alpha_ == NormalizedF32::one(); }
121
123 [[nodiscard]] PremultipliedColor premultiply() const;
125 [[nodiscard]] ColorU8 toColorU8() const;
126 bool operator==(const Color&) const = default;
127
128 private:
129 NormalizedF32 red_ = NormalizedF32::zero();
130 NormalizedF32 green_ = NormalizedF32::zero();
131 NormalizedF32 blue_ = NormalizedF32::zero();
132 NormalizedF32 alpha_ = NormalizedF32::zero();
133};
134
137 public:
138 constexpr PremultipliedColor() = default;
140 NormalizedF32 alpha)
141 : red_(red), green_(green), blue_(blue), alpha_(alpha) {}
142
143 float red() const { return red_.get(); }
144 float green() const { return green_.get(); }
145 float blue() const { return blue_.get(); }
146 float alpha() const { return alpha_.get(); }
147
149 [[nodiscard]] Color demultiply() const;
151 [[nodiscard]] PremultipliedColorU8 toColorU8() const;
152
153 bool operator==(const PremultipliedColor&) const = default;
154
155 private:
156 friend class Color;
157 NormalizedF32 red_ = NormalizedF32::zero();
158 NormalizedF32 green_ = NormalizedF32::zero();
159 NormalizedF32 blue_ = NormalizedF32::zero();
160 NormalizedF32 alpha_ = NormalizedF32::zero();
161};
162
164AlphaU8 premultiplyU8(AlphaU8 color, AlphaU8 alpha);
165
167enum class ColorSpace {
168 Linear,
169 Gamma2,
170 SimpleSRGB,
172};
173
175NormalizedF32 expandChannel(ColorSpace colorSpace, NormalizedF32 x);
177Color expandColor(ColorSpace colorSpace, Color color);
179NormalizedF32 compressChannel(ColorSpace colorSpace, NormalizedF32 x);
180
182std::optional<pipeline::Stage> expandStage(ColorSpace colorSpace);
184std::optional<pipeline::Stage> expandDestStage(ColorSpace colorSpace);
186std::optional<pipeline::Stage> compressStage(ColorSpace colorSpace);
187
188} // namespace tiny_skia
std::uint8_t AlphaU8
8-bit alpha value.
Definition Color.h:17
ColorSpace
Colorspace for gamma-correct blending.
Definition Color.h:167
@ Gamma2
Power-of-2 gamma approximation.
@ Linear
Linear RGB (no gamma).
@ FullSRGBGamma
Full sRGB gamma curve.
@ SimpleSRGB
Simplified sRGB transfer function.
Validated floating-point types: NormalizedF32 [0,1], FiniteF32, etc.
8-bit RGBA color (straight alpha).
Definition Color.h:27
PremultipliedColorU8 premultiply() const
Converts to premultiplied alpha.
Floating-point RGBA color [0,1] (straight alpha).
Definition Color.h:88
void applyOpacity(float opacity)
Multiplies alpha by opacity (clamped to [0,1]).
Definition Color.h:116
static Color fromRgbaUnchecked(float red, float green, float blue, float alpha)
Creates without validation (values must be in [0,1]).
PremultipliedColor premultiply() const
Converts to premultiplied alpha.
static Color fromRgba8(AlphaU8 red, AlphaU8 green, AlphaU8 blue, AlphaU8 alpha)
Creates from 8-bit components.
ColorU8 toColorU8() const
Converts to 8-bit color.
static std::optional< Color > fromRgba(float red, float green, float blue, float alpha)
Creates with validation. Returns nullopt if any component is outside [0,1].
A float guaranteed to be in [0, 1].
Definition FloatingPoint.h:12
static NormalizedF32 newClamped(float value)
Clamps to [0,1].
8-bit RGBA color (premultiplied alpha). Internal pixel format.
Definition Color.h:54
static constexpr PremultipliedColorU8 fromRgbaUnchecked(AlphaU8 red, AlphaU8 green, AlphaU8 blue, AlphaU8 alpha)
Creates from components without validation.
Definition Color.h:63
static std::optional< PremultipliedColorU8 > fromRgba(AlphaU8 red, AlphaU8 green, AlphaU8 blue, AlphaU8 alpha)
Creates from components. Returns nullopt if any channel > alpha.
ColorU8 demultiply() const
Converts to straight alpha.
Floating-point RGBA color [0,1] (premultiplied alpha).
Definition Color.h:136
PremultipliedColorU8 toColorU8() const
Converts to 8-bit premultiplied color.
Color demultiply() const
Converts to straight alpha.