MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
color.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <mbgl/util/feature.hpp>
4 
5 #include <cassert>
6 #include <string>
7 #include <array>
8 #include <optional>
9 
10 namespace mbgl {
11 
12 // Stores a premultiplied color, with all four channels ranging from 0..1
13 class Color {
14 public:
15  Color() = default;
16  Color(float r_, float g_, float b_, float a_)
17  : r(r_), g(g_), b(b_), a(a_) {
18  assert(r_ >= 0.0f);
19  assert(r_ <= 1.0f);
20  assert(g_ >= 0.0f);
21  assert(g_ <= 1.0f);
22  assert(b_ >= 0.0f);
23  assert(b_ <= 1.0f);
24  assert(a_ >= 0.0f);
25  assert(a_ <= 1.0f);
26  }
27 
28  float r = 0.0f;
29  float g = 0.0f;
30  float b = 0.0f;
31  float a = 0.0f;
32 
33  static Color black() { return { 0.0f, 0.0f, 0.0f, 1.0f }; };
34  static Color white() { return { 1.0f, 1.0f, 1.0f, 1.0f }; };
35 
36  static Color red() { return { 1.0f, 0.0f, 0.0f, 1.0f }; };
37  static Color green() { return { 0.0f, 1.0f, 0.0f, 1.0f }; };
38  static Color blue() { return { 0.0f, 0.0f, 1.0f, 1.0f }; };
39 
40  static std::optional<Color> parse(const std::string&);
42  std::array<double, 4> toArray() const;
45 };
46 
47 inline bool operator==(const Color& colorA, const Color& colorB) {
48  return colorA.r == colorB.r && colorA.g == colorB.g && colorA.b == colorB.b && colorA.a == colorB.a;
49 }
50 
51 inline bool operator!=(const Color& colorA, const Color& colorB) {
52  return !(colorA == colorB);
53 }
54 
55 inline Color operator*(const Color& color, float alpha) {
56  assert(alpha >= 0.0f);
57  assert(alpha <= 1.0f);
58  return {
59  color.r * alpha,
60  color.g * alpha,
61  color.b * alpha,
62  color.a * alpha
63  };
64 }
65 
66 } // namespace mbgl
float r
Definition: color.hpp:28
float a
Definition: color.hpp:31
float b
Definition: color.hpp:30
static std::optional< Color > parse(const std::string &)
static Color green()
Definition: color.hpp:37
mbgl::Value serialize() const
Color(float r_, float g_, float b_, float a_)
Definition: color.hpp:16
float g
Definition: color.hpp:29
std::string stringify() const
static Color red()
Definition: color.hpp:36
mbgl::Value toObject() const
static Color black()
Definition: color.hpp:33
std::array< double, 4 > toArray() const
static Color white()
Definition: color.hpp:34
Color()=default
static Color blue()
Definition: color.hpp:38
std::unique_ptr< Expression > string(std::unique_ptr< Expression >, std::unique_ptr< Expression > def=nullptr)
Definition: actor.hpp:15
constexpr bool operator==(const CameraOptions &a, const CameraOptions &b)
Definition: camera.hpp:50
mapbox::base::Value Value
Definition: feature.hpp:11
constexpr bool operator!=(const CameraOptions &a, const CameraOptions &b)
Definition: camera.hpp:59
Color operator*(const Color &color, float alpha)
Definition: color.hpp:55