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 | |
8 | #include "third_party/skia/include/core/SkScalar.h" |
9 | |
10 | namespace flutter { |
11 | |
12 | struct DlColor { |
13 | public: |
14 | constexpr DlColor() : argb(0xFF000000) {} |
15 | constexpr DlColor(uint32_t argb) : argb(argb) {} |
16 | |
17 | static constexpr uint8_t toAlpha(SkScalar opacity) { return toC(fComp: opacity); } |
18 | static constexpr SkScalar toOpacity(uint8_t alpha) { return toF(comp: alpha); } |
19 | |
20 | // clang-format off |
21 | static constexpr DlColor kTransparent() {return 0x00000000;}; |
22 | static constexpr DlColor kBlack() {return 0xFF000000;}; |
23 | static constexpr DlColor kWhite() {return 0xFFFFFFFF;}; |
24 | static constexpr DlColor kRed() {return 0xFFFF0000;}; |
25 | static constexpr DlColor kGreen() {return 0xFF00FF00;}; |
26 | static constexpr DlColor kBlue() {return 0xFF0000FF;}; |
27 | static constexpr DlColor kCyan() {return 0xFF00FFFF;}; |
28 | static constexpr DlColor kMagenta() {return 0xFFFF00FF;}; |
29 | static constexpr DlColor kYellow() {return 0xFFFFFF00;}; |
30 | static constexpr DlColor kDarkGrey() {return 0xFF3F3F3F;}; |
31 | static constexpr DlColor kMidGrey() {return 0xFF808080;}; |
32 | static constexpr DlColor kLightGrey() {return 0xFFC0C0C0;}; |
33 | // clang-format on |
34 | |
35 | uint32_t argb; |
36 | |
37 | constexpr bool isOpaque() const { return getAlpha() == 0xFF; } |
38 | constexpr bool isTransparent() const { return getAlpha() == 0; } |
39 | |
40 | constexpr int getAlpha() const { return argb >> 24; } |
41 | constexpr int getRed() const { return (argb >> 16) & 0xFF; } |
42 | constexpr int getGreen() const { return (argb >> 8) & 0xFF; } |
43 | constexpr int getBlue() const { return argb & 0xFF; } |
44 | |
45 | constexpr float getAlphaF() const { return toF(comp: getAlpha()); } |
46 | constexpr float getRedF() const { return toF(comp: getRed()); } |
47 | constexpr float getGreenF() const { return toF(comp: getGreen()); } |
48 | constexpr float getBlueF() const { return toF(comp: getBlue()); } |
49 | |
50 | constexpr uint32_t premultipliedArgb() const { |
51 | if (isOpaque()) { |
52 | return argb; |
53 | } |
54 | float f = getAlphaF(); |
55 | return (argb & 0xFF000000) | // |
56 | toC(fComp: getRedF() * f) << 16 | // |
57 | toC(fComp: getGreenF() * f) << 8 | // |
58 | toC(fComp: getBlueF() * f); |
59 | } |
60 | |
61 | constexpr DlColor withAlpha(uint8_t alpha) const { // |
62 | return (argb & 0x00FFFFFF) | (alpha << 24); |
63 | } |
64 | constexpr DlColor withRed(uint8_t red) const { // |
65 | return (argb & 0xFF00FFFF) | (red << 16); |
66 | } |
67 | constexpr DlColor withGreen(uint8_t green) const { // |
68 | return (argb & 0xFFFF00FF) | (green << 8); |
69 | } |
70 | constexpr DlColor withBlue(uint8_t blue) const { // |
71 | return (argb & 0xFFFFFF00) | (blue << 0); |
72 | } |
73 | |
74 | constexpr DlColor modulateOpacity(float opacity) const { |
75 | return opacity <= 0 ? withAlpha(alpha: 0) |
76 | : opacity >= 1 ? *this |
77 | : withAlpha(alpha: round(lcpp_x: getAlpha() * opacity)); |
78 | } |
79 | |
80 | operator uint32_t() const { return argb; } |
81 | bool operator==(DlColor const& other) const { return argb == other.argb; } |
82 | bool operator!=(DlColor const& other) const { return argb != other.argb; } |
83 | bool operator==(uint32_t const& other) const { return argb == other; } |
84 | bool operator!=(uint32_t const& other) const { return argb != other; } |
85 | |
86 | private: |
87 | static float toF(uint8_t comp) { return comp * (1.0f / 255); } |
88 | static uint8_t toC(float fComp) { return round(lcpp_x: fComp * 255); } |
89 | }; |
90 | |
91 | } // namespace flutter |
92 | |
93 | #endif // FLUTTER_DISPLAY_LIST_DL_COLOR_H_ |
94 | |