Flutter Engine
 
Loading...
Searching...
No Matches
dl_color.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_DISPLAY_LIST_DL_COLOR_H_
6#define FLUTTER_DISPLAY_LIST_DL_COLOR_H_
7
9
10namespace flutter {
11
12// These should match the enumerations defined in //lib/ui/painting.dart.
13enum class DlColorSpace { kSRGB = 0, kExtendedSRGB = 1, kDisplayP3 = 2 };
14
15/// A representation of a color.
16///
17/// The color belongs to a DlColorSpace. Using deprecated integer data accessors
18/// on colors not in the kSRGB colorspace can lead to data loss. Using the
19/// floating point accessors and constructors that were added for wide-gamut
20/// support are preferred.
21struct DlColor {
22 public:
23 constexpr DlColor()
24 : alpha_(1.f),
25 red_(0.f),
26 green_(0.f),
27 blue_(0.f),
28 color_space_(DlColorSpace::kSRGB) {}
29 constexpr explicit DlColor(uint32_t argb)
30 : alpha_(toF((argb >> 24) & 0xff)),
31 red_(toF((argb >> 16) & 0xff)),
32 green_(toF((argb >> 8) & 0xff)),
33 blue_(toF((argb >> 0) & 0xff)),
34 color_space_(DlColorSpace::kSRGB) {}
35 constexpr DlColor(DlScalar alpha,
36 DlScalar red,
37 DlScalar green,
38 DlScalar blue,
39 DlColorSpace colorspace)
40 : alpha_(std::clamp(alpha, 0.0f, 1.0f)),
41 red_(red),
42 green_(green),
43 blue_(blue),
44 color_space_(colorspace) {}
45
46 /// @brief Construct a 32 bit color from floating point R, G, B, and A color
47 /// channels.
48 static constexpr DlColor RGBA(DlScalar r,
49 DlScalar g,
50 DlScalar b,
51 DlScalar a) {
52 return ARGB(a, r, g, b);
53 }
54
55 /// @brief Construct a 32 bit color from floating point A, R, G, and B color
56 /// channels.
57 static constexpr DlColor ARGB(DlScalar a,
58 DlScalar r,
59 DlScalar g,
60 DlScalar b) {
61 return DlColor(a, r, g, b, DlColorSpace::kSRGB);
62 }
63
64 static inline uint8_t toAlpha(DlScalar opacity) { return toC(opacity); }
65 static constexpr DlScalar toOpacity(uint8_t alpha) { return toF(alpha); }
66
67 // clang-format off
68 static constexpr DlColor kTransparent() {return DlColor(0x00000000);};
69 static constexpr DlColor kBlack() {return DlColor(0xFF000000);};
70 static constexpr DlColor kWhite() {return DlColor(0xFFFFFFFF);};
71 static constexpr DlColor kRed() {return DlColor(0xFFFF0000);};
72 static constexpr DlColor kGreen() {return DlColor(0xFF00FF00);};
73 static constexpr DlColor kBlue() {return DlColor(0xFF0000FF);};
74 static constexpr DlColor kCyan() {return DlColor(0xFF00FFFF);};
75 static constexpr DlColor kMagenta() {return DlColor(0xFFFF00FF);};
76 static constexpr DlColor kYellow() {return DlColor(0xFFFFFF00);};
77 static constexpr DlColor kDarkGrey() {return DlColor(0xFF3F3F3F);};
78 static constexpr DlColor kMidGrey() {return DlColor(0xFF808080);};
79 static constexpr DlColor kLightGrey() {return DlColor(0xFFC0C0C0);};
80 static constexpr DlColor kAliceBlue() {return DlColor(0xFFF0F8FF);};
81 static constexpr DlColor kFuchsia() {return DlColor(0xFFFF00FF);};
82 static constexpr DlColor kMaroon() {return DlColor(0xFF800000);};
83 static constexpr DlColor kSkyBlue() {return DlColor(0xFF87CEEB);};
84 static constexpr DlColor kCornflowerBlue() {return DlColor(0xFF6495ED);};
85 static constexpr DlColor kCrimson() {return DlColor(0xFFFF5733);};
86 static constexpr DlColor kAqua() {return DlColor(0xFF00FFFF);};
87 static constexpr DlColor kOrange() {return DlColor(0xFFFFA500);};
88 static constexpr DlColor kPurple() {return DlColor(0xFF800080);};
89 static constexpr DlColor kLimeGreen() {return DlColor(0xFF32CD32);};
90 static constexpr DlColor kGreenYellow() {return DlColor(0xFFADFF2F);};
91 static constexpr DlColor kDarkMagenta() {return DlColor(0xFF8B008B);};
92 static constexpr DlColor kOrangeRed() {return DlColor(0xFFFF4500);};
93 static constexpr DlColor kDarkGreen() {return DlColor(0xFF006400);};
94 static constexpr DlColor kChartreuse() {return DlColor(0xFF7FFF00);};
95 // clang-format on
96
97 constexpr bool isOpaque() const { return alpha_ >= 1.f; }
98 constexpr bool isTransparent() const { return alpha_ <= 0.f; }
99
100 ///\deprecated Use floating point accessors to avoid data loss when using wide
101 /// gamut colors.
102 inline int getAlpha() const { return toC(alpha_); }
103 ///\deprecated Use floating point accessors to avoid data loss when using wide
104 /// gamut colors.
105 inline int getRed() const { return toC(red_); }
106 ///\deprecated Use floating point accessors to avoid data loss when using wide
107 /// gamut colors.
108 inline int getGreen() const { return toC(green_); }
109 ///\deprecated Use floating point accessors to avoid data loss when using wide
110 /// gamut colors.
111 inline int getBlue() const { return toC(blue_); }
112
113 constexpr DlScalar getAlphaF() const { return alpha_; }
114 constexpr DlScalar getRedF() const { return red_; }
115 constexpr DlScalar getGreenF() const { return green_; }
116 constexpr DlScalar getBlueF() const { return blue_; }
117
118 constexpr DlColorSpace getColorSpace() const { return color_space_; }
119
120 inline DlColor withAlpha(uint8_t alpha) const { //
121 return DlColor((argb() & 0x00FFFFFF) | (alpha << 24));
122 }
123 inline DlColor withRed(uint8_t red) const { //
124 return DlColor((argb() & 0xFF00FFFF) | (red << 16));
125 }
126 inline DlColor withGreen(uint8_t green) const { //
127 return DlColor((argb() & 0xFFFF00FF) | (green << 8));
128 }
129 inline DlColor withBlue(uint8_t blue) const { //
130 return DlColor((argb() & 0xFFFFFF00) | (blue << 0));
131 }
132 constexpr DlColor withAlphaF(float alpha) const { //
133 return DlColor(alpha, red_, green_, blue_, color_space_);
134 }
135 constexpr DlColor withRedF(float red) const { //
136 return DlColor(alpha_, red, green_, blue_, color_space_);
137 }
138 constexpr DlColor withGreenF(float green) const { //
139 return DlColor(alpha_, red_, green, blue_, color_space_);
140 }
141 constexpr DlColor withBlueF(float blue) const { //
142 return DlColor(alpha_, red_, green_, blue, color_space_);
143 }
144 /// Performs a colorspace transformation.
145 ///
146 /// This isn't just a replacement of the color space field, the new color
147 /// components are calculated.
148 DlColor withColorSpace(DlColorSpace color_space) const;
149
150 constexpr DlColor modulateOpacity(DlScalar opacity) const {
151 return opacity <= 0 ? withAlpha(0)
152 : opacity >= 1 ? *this
153 : withAlpha(round(getAlpha() * opacity));
154 }
155
156 ///\deprecated Use floating point accessors to avoid data loss when using wide
157 /// gamut colors.
158 inline uint32_t argb() const {
159 if (color_space_ != DlColorSpace::kSRGB) {
161 }
162 return toC(alpha_) << 24 | //
163 toC(red_) << 16 | //
164 toC(green_) << 8 | //
165 toC(blue_) << 0;
166 }
167
168 /// Checks that no difference in color components exceeds the delta.
169 ///
170 /// This doesn't check against the actual distance between the colors in some
171 /// space.
172 bool isClose(DlColor const& other, DlScalar delta = 1.0f / 256.0f) {
173 return color_space_ == other.color_space_ &&
174 std::abs(alpha_ - other.alpha_) < delta &&
175 std::abs(red_ - other.red_) < delta &&
176 std::abs(green_ - other.green_) < delta &&
177 std::abs(blue_ - other.blue_) < delta;
178 }
179 bool operator==(DlColor const& other) const {
180 return alpha_ == other.alpha_ && red_ == other.red_ &&
181 green_ == other.green_ && blue_ == other.blue_ &&
182 color_space_ == other.color_space_;
183 }
184
185 private:
186 DlScalar alpha_;
187 DlScalar red_;
188 DlScalar green_;
189 DlScalar blue_;
190 DlColorSpace color_space_;
191
192 static constexpr DlScalar toF(uint8_t comp) { return comp * (1.0f / 255); }
193 static inline uint8_t toC(DlScalar fComp) { return std::round(fComp * 255); }
194};
195
196} // namespace flutter
197
198#endif // FLUTTER_DISPLAY_LIST_DL_COLOR_H_
impeller::Scalar DlScalar
@ kExtendedSRGB
Definition image.h:18
@ kSRGB
Definition image.h:17
DlColorSpace
Definition dl_color.h:13
Definition ref_ptr.h:261
static constexpr DlColor kMagenta()
Definition dl_color.h:75
static constexpr DlColor kWhite()
Definition dl_color.h:70
static constexpr DlColor kBlue()
Definition dl_color.h:73
static constexpr DlColor kDarkMagenta()
Definition dl_color.h:91
DlColor withBlue(uint8_t blue) const
Definition dl_color.h:129
constexpr DlColor(uint32_t argb)
Definition dl_color.h:29
static constexpr DlColor RGBA(DlScalar r, DlScalar g, DlScalar b, DlScalar a)
Construct a 32 bit color from floating point R, G, B, and A color channels.
Definition dl_color.h:48
static constexpr DlColor kBlack()
Definition dl_color.h:69
static constexpr DlColor kCrimson()
Definition dl_color.h:85
static constexpr DlColor kMaroon()
Definition dl_color.h:82
static constexpr DlColor ARGB(DlScalar a, DlScalar r, DlScalar g, DlScalar b)
Construct a 32 bit color from floating point A, R, G, and B color channels.
Definition dl_color.h:57
static constexpr DlScalar toOpacity(uint8_t alpha)
Definition dl_color.h:65
static constexpr DlColor kAqua()
Definition dl_color.h:86
static constexpr DlColor kYellow()
Definition dl_color.h:76
static constexpr DlColor kPurple()
Definition dl_color.h:88
int getAlpha() const
Definition dl_color.h:102
static constexpr DlColor kFuchsia()
Definition dl_color.h:81
static constexpr DlColor kLightGrey()
Definition dl_color.h:79
static constexpr DlColor kChartreuse()
Definition dl_color.h:94
constexpr DlColorSpace getColorSpace() const
Definition dl_color.h:118
int getRed() const
Definition dl_color.h:105
static constexpr DlColor kCornflowerBlue()
Definition dl_color.h:84
bool operator==(DlColor const &other) const
Definition dl_color.h:179
static constexpr DlColor kDarkGreen()
Definition dl_color.h:93
static constexpr DlColor kAliceBlue()
Definition dl_color.h:80
constexpr DlColor()
Definition dl_color.h:23
constexpr DlScalar getRedF() const
Definition dl_color.h:114
constexpr DlScalar getAlphaF() const
Definition dl_color.h:113
DlColor withColorSpace(DlColorSpace color_space) const
Definition dl_color.cc:40
constexpr DlScalar getBlueF() const
Definition dl_color.h:116
static constexpr DlColor kMidGrey()
Definition dl_color.h:78
constexpr DlColor withAlphaF(float alpha) const
Definition dl_color.h:132
constexpr DlScalar getGreenF() const
Definition dl_color.h:115
static constexpr DlColor kTransparent()
Definition dl_color.h:68
static uint8_t toAlpha(DlScalar opacity)
Definition dl_color.h:64
static constexpr DlColor kRed()
Definition dl_color.h:71
static constexpr DlColor kGreen()
Definition dl_color.h:72
constexpr DlColor(DlScalar alpha, DlScalar red, DlScalar green, DlScalar blue, DlColorSpace colorspace)
Definition dl_color.h:35
static constexpr DlColor kOrange()
Definition dl_color.h:87
constexpr DlColor withRedF(float red) const
Definition dl_color.h:135
constexpr DlColor withGreenF(float green) const
Definition dl_color.h:138
constexpr DlColor withBlueF(float blue) const
Definition dl_color.h:141
DlColor withGreen(uint8_t green) const
Definition dl_color.h:126
int getGreen() const
Definition dl_color.h:108
constexpr bool isTransparent() const
Definition dl_color.h:98
int getBlue() const
Definition dl_color.h:111
static constexpr DlColor kDarkGrey()
Definition dl_color.h:77
static constexpr DlColor kCyan()
Definition dl_color.h:74
static constexpr DlColor kSkyBlue()
Definition dl_color.h:83
static constexpr DlColor kLimeGreen()
Definition dl_color.h:89
static constexpr DlColor kOrangeRed()
Definition dl_color.h:92
static constexpr DlColor kGreenYellow()
Definition dl_color.h:90
bool isClose(DlColor const &other, DlScalar delta=1.0f/256.0f)
Definition dl_color.h:172
DlColor withRed(uint8_t red) const
Definition dl_color.h:123
uint32_t argb() const
Definition dl_color.h:158
constexpr bool isOpaque() const
Definition dl_color.h:97
DlColor withAlpha(uint8_t alpha) const
Definition dl_color.h:120
constexpr DlColor modulateOpacity(DlScalar opacity) const
Definition dl_color.h:150