Chira Engine
A customizable MIT-licensed game engine.
Color.h
1 #pragma once
2 
3 #include <algorithm>
4 
5 namespace chira {
6 
7 struct ColorR {
8  float r;
9  ColorR(float R) // NOLINT(google-explicit-constructor)
10  : r(std::clamp(R, 0.f, 1.f)) {}
11  ColorR()
12  : ColorR(0) {}
13  bool operator==(const ColorR& other) const {
14  return this->r == other.r;
15  }
16  bool operator!=(const ColorR& other) const {
17  return !this->operator==(other);
18  }
19 };
20 
21 struct ColorRG {
22  float r;
23  float g;
24  ColorRG(float R, float G)
25  : r(std::clamp(R, 0.f, 1.f))
26  , g(std::clamp(G, 0.f, 1.f)) {}
27  ColorRG(float all) // NOLINT(google-explicit-constructor)
28  : ColorRG(all, all) {}
29  ColorRG()
30  : ColorRG(0) {}
31  bool operator==(const ColorRG& other) const {
32  return this->r == other.r && this->g == other.g;
33  }
34  bool operator!=(const ColorRG& other) const {
35  return !this->operator==(other);
36  }
37 };
38 
39 struct ColorRGB {
40  float r;
41  float g;
42  float b;
43  ColorRGB(float R, float G, float B)
44  : r(std::clamp(R, 0.f, 1.f))
45  , g(std::clamp(G, 0.f, 1.f))
46  , b(std::clamp(B, 0.f, 1.f)) {}
47  ColorRGB(float all) // NOLINT(google-explicit-constructor)
48  : ColorRGB(all, all, all) {}
49  ColorRGB()
50  : ColorRGB(0) {}
51  bool operator==(const ColorRGB& other) const {
52  return this->r == other.r && this->g == other.g && this->b == other.b;
53  }
54  bool operator!=(const ColorRGB& other) const {
55  return !this->operator==(other);
56  }
57 };
58 
59 struct ColorRGBA {
60  float r;
61  float g;
62  float b;
63  float a;
64  ColorRGBA(float R, float G, float B, float A = 1.f)
65  : r(std::clamp(R, 0.f, 1.f))
66  , g(std::clamp(G, 0.f, 1.f))
67  , b(std::clamp(B, 0.f, 1.f))
68  , a(std::clamp(A, 0.f, 1.f)) {}
69  ColorRGBA(float all, float A = 1.f) // NOLINT(google-explicit-constructor)
70  : ColorRGBA(all, all, all, A) {}
71  ColorRGBA(ColorRGB color, float A = 1.f) // NOLINT(google-explicit-constructor)
72  : ColorRGBA(color.r, color.g, color.b, A) {}
73  ColorRGBA()
74  : ColorRGBA(0) {}
75  bool operator==(const ColorRGBA& other) const {
76  return this->r == other.r && this->g == other.g && this->b == other.b && this->a == other.a;
77  }
78  bool operator!=(const ColorRGBA& other) const {
79  return !this->operator==(other);
80  }
81 };
82 
83 } // namespace chira