Flutter Engine
The 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
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 // clang-format on
34
35 constexpr bool isOpaque() const { return getAlpha() == 0xFF; }
36 constexpr bool isTransparent() const { return getAlpha() == 0; }
37
38 constexpr int getAlpha() const { return argb_ >> 24; }
39 constexpr int getRed() const { return (argb_ >> 16) & 0xFF; }
40 constexpr int getGreen() const { return (argb_ >> 8) & 0xFF; }
41 constexpr int getBlue() const { return argb_ & 0xFF; }
42
43 constexpr float getAlphaF() const { return toF(getAlpha()); }
44 constexpr float getRedF() const { return toF(getRed()); }
45 constexpr float getGreenF() const { return toF(getGreen()); }
46 constexpr float getBlueF() const { return toF(getBlue()); }
47
48 constexpr uint32_t premultipliedArgb() const {
49 if (isOpaque()) {
50 return argb_;
51 }
52 float f = getAlphaF();
53 return (argb_ & 0xFF000000) | //
54 toC(getRedF() * f) << 16 | //
55 toC(getGreenF() * f) << 8 | //
56 toC(getBlueF() * f);
57 }
58
59 constexpr DlColor withAlpha(uint8_t alpha) const { //
60 return DlColor((argb_ & 0x00FFFFFF) | (alpha << 24));
61 }
62 constexpr DlColor withRed(uint8_t red) const { //
63 return DlColor((argb_ & 0xFF00FFFF) | (red << 16));
64 }
65 constexpr DlColor withGreen(uint8_t green) const { //
66 return DlColor((argb_ & 0xFFFF00FF) | (green << 8));
67 }
68 constexpr DlColor withBlue(uint8_t blue) const { //
69 return DlColor((argb_ & 0xFFFFFF00) | (blue << 0));
70 }
71
72 constexpr DlColor modulateOpacity(float opacity) const {
73 return opacity <= 0 ? withAlpha(0)
74 : opacity >= 1 ? *this
75 : withAlpha(round(getAlpha() * opacity));
76 }
77
78 constexpr uint32_t argb() const { return argb_; }
79
80 bool operator==(DlColor const& other) const { return argb_ == other.argb_; }
81 bool operator!=(DlColor const& other) const { return argb_ != other.argb_; }
82 bool operator==(uint32_t const& other) const { return argb_ == other; }
83 bool operator!=(uint32_t const& other) const { return argb_ != other; }
84
85 private:
86 uint32_t argb_;
87
88 static float toF(uint8_t comp) { return comp * (1.0f / 255); }
89 static uint8_t toC(float fComp) { return round(fComp * 255); }
90};
91
92} // namespace flutter
93
94#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:72
static constexpr DlColor kMagenta()
Definition dl_color.h:28
constexpr float getAlphaF() const
Definition dl_color.h:43
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:39
constexpr DlColor(uint32_t argb)
Definition dl_color.h:15
constexpr int getGreen() const
Definition dl_color.h:40
static constexpr DlColor kBlack()
Definition dl_color.h:22
static constexpr DlColor kYellow()
Definition dl_color.h:29
constexpr DlColor withGreen(uint8_t green) const
Definition dl_color.h:65
static constexpr uint8_t toAlpha(SkScalar opacity)
Definition dl_color.h:17
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:59
constexpr float getRedF() const
Definition dl_color.h:44
constexpr uint32_t premultipliedArgb() const
Definition dl_color.h:48
bool operator==(DlColor const &other) const
Definition dl_color.h:80
bool operator!=(uint32_t const &other) const
Definition dl_color.h:83
constexpr DlColor()
Definition dl_color.h:14
static constexpr DlColor kMidGrey()
Definition dl_color.h:31
constexpr int getBlue() const
Definition dl_color.h:41
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:46
static constexpr DlColor kGreen()
Definition dl_color.h:25
bool operator==(uint32_t const &other) const
Definition dl_color.h:82
constexpr bool isTransparent() const
Definition dl_color.h:36
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:78
bool operator!=(DlColor const &other) const
Definition dl_color.h:81
constexpr DlColor withBlue(uint8_t blue) const
Definition dl_color.h:68
constexpr float getGreenF() const
Definition dl_color.h:45
constexpr int getAlpha() const
Definition dl_color.h:38
constexpr DlColor withRed(uint8_t red) const
Definition dl_color.h:62
constexpr bool isOpaque() const
Definition dl_color.h:35