1 | #pragma once |
2 | |
3 | #include <mbgl/util/optional.hpp> |
4 | |
5 | #include <cassert> |
6 | #include <string> |
7 | #include <array> |
8 | |
9 | namespace mbgl { |
10 | |
11 | // Stores a premultiplied color, with all four channels ranging from 0..1 |
12 | class Color { |
13 | public: |
14 | Color() = default; |
15 | Color(float r_, float g_, float b_, float a_) |
16 | : r(r_), g(g_), b(b_), a(a_) { |
17 | assert(r_ >= 0.0f); |
18 | assert(r_ <= 1.0f); |
19 | assert(g_ >= 0.0f); |
20 | assert(g_ <= 1.0f); |
21 | assert(b_ >= 0.0f); |
22 | assert(b_ <= 1.0f); |
23 | assert(a_ >= 0.0f); |
24 | assert(a_ <= 1.0f); |
25 | } |
26 | |
27 | float r = 0.0f; |
28 | float g = 0.0f; |
29 | float b = 0.0f; |
30 | float a = 0.0f; |
31 | |
32 | static Color black() { return { 0.0f, 0.0f, 0.0f, 1.0f }; }; |
33 | static Color white() { return { 1.0f, 1.0f, 1.0f, 1.0f }; }; |
34 | |
35 | static Color red() { return { 1.0f, 0.0f, 0.0f, 1.0f }; }; |
36 | static Color green() { return { 0.0f, 1.0f, 0.0f, 1.0f }; }; |
37 | static Color blue() { return { 0.0f, 0.0f, 1.0f, 1.0f }; }; |
38 | |
39 | static optional<Color> parse(const std::string&); |
40 | std::string stringify() const; |
41 | std::array<double, 4> toArray() const; |
42 | }; |
43 | |
44 | inline bool operator==(const Color& colorA, const Color& colorB) { |
45 | return colorA.r == colorB.r && colorA.g == colorB.g && colorA.b == colorB.b && colorA.a == colorB.a; |
46 | } |
47 | |
48 | inline bool operator!=(const Color& colorA, const Color& colorB) { |
49 | return !(colorA == colorB); |
50 | } |
51 | |
52 | inline Color operator*(const Color& color, float alpha) { |
53 | assert(alpha >= 0.0f); |
54 | assert(alpha <= 1.0f); |
55 | return { |
56 | color.r * alpha, |
57 | color.g * alpha, |
58 | color.b * alpha, |
59 | color.a * alpha |
60 | }; |
61 | } |
62 | |
63 | } // namespace mbgl |
64 | |