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