Simulant  21.12-194
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
colour.h
1 /* * Copyright (c) 2011-2017 Luke Benstead https://simulant-engine.appspot.com
2  *
3  * This file is part of Simulant.
4  *
5  * Simulant is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Simulant is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with Simulant. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef COLOUR_H
20 #define COLOUR_H
21 
22 #include <cstdint>
23 #include <string>
24 
25 #include "generic/range_value.h"
26 
27 namespace smlt {
28 
29 struct Colour {
30  float r, g, b, a;
31 
32  Colour():
33  r(1.0), g(1.0), b(1.0), a(1.0) {}
34 
35  static Colour from_bytes(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
36  return Colour(float(r) / 255.0f, float(g) / 255.0f, float(b) / 255.0f, float(a) / 255.0f);
37  }
38 
39  Colour(float r, float g, float b, float a):
40  r(r), g(g), b(b), a(a) {}
41 
42  Colour operator*(const float rhs) const {
43  return Colour(r * rhs, g * rhs, b * rhs, a * rhs);
44  }
45 
46  Colour operator*=(const float rhs) {
47  *this = *this * rhs;
48  return *this;
49  }
50 
51  Colour operator-(const Colour& rhs) const {
52  return Colour(r - rhs.r, g - rhs.g, b - rhs.b, a - rhs.a);
53  }
54 
55  Colour operator-=(const Colour& rhs) {
56  *this = *this - rhs;
57  return *this;
58  }
59 
60  Colour operator+(const Colour& rhs) const {
61  return Colour(r + rhs.r, g + rhs.g, b + rhs.b, a + rhs.a);
62  }
63 
64  Colour operator+=(const Colour& rhs) {
65  *this = *this + rhs;
66  return *this;
67  }
68 
69  bool operator==(const Colour& rhs) const {
70  return this->r == rhs.r && this->g == rhs.g && this->b == rhs.b && this->a == rhs.a;
71  }
72 
73  bool operator!=(const Colour& rhs) const {
74  return this->r != rhs.r || this->g != rhs.g || this->b != rhs.b || this->a != rhs.a;
75  }
76 
77  bool operator<(const Colour& rhs) const {
78  if(r < rhs.r) {
79  return true;
80  } else if (r == rhs.r) {
81  if(g < rhs.g) {
82  return true;
83  } else if(g == rhs.g) {
84  if(b < rhs.b) {
85  return true;
86  } else if(b == rhs.b) {
87  if(a < rhs.a) {
88  return true;
89  }
90  }
91  }
92  }
93 
94  return false;
95  }
96 
97 
98  Colour lerp(const Colour& end, float t) const;
99 
100  std::string to_hex_string() const;
101  static Colour from_hex_string(const std::string& hex_string);
102 
103  static const Colour BLACK;
104  static const Colour GREY;
105  static const Colour WHITE;
106  static const Colour RED;
107  static const Colour GREEN;
108  static const Colour BLUE;
109  static const Colour YELLOW;
110  static const Colour PURPLE;
111  static const Colour TURQUOISE;
112  static const Colour NONE;
113 
114 };
115 
116 /* 16bit packed colour in ARGB4444 format. This is useful for minimising
117  * ram usage when colour fidelity isn't important. */
119 public:
121  PackedColour4444(const Colour& c);
122 
123  PackedColour4444& operator=(const PackedColour4444&) = default;
124  PackedColour4444& operator=(const Colour& rhs);
125 
126  bool operator==(const PackedColour4444& rhs) const;
127  bool operator==(const Colour& rhs) const;
128 
129  bool operator!=(const Colour& rhs) const {
130  return !(*this == rhs);
131  }
132 
133  operator Colour() {
134  Colour c(rf(), gf(), bf(), af());
135  return c;
136  }
137 
138  void set_alpha(NormalizedFloat a);
139 
140  uint8_t r8() const;
141  uint8_t g8() const;
142  uint8_t b8() const;
143  uint8_t a8() const;
144 
147  float rf() const;
148 
151  float gf() const;
152 
155  float bf() const;
156 
159  float af() const;
160 
161 private:
162  uint16_t colour_;
163 };
164 
165 
166 std::ostream& operator<<(std::ostream& stream, const Colour& c);
167 }
168 
169 #endif // COLOUR_H
smlt::PackedColour4444
Definition: colour.h:118
smlt
Definition: animation.cpp:25
smlt::PackedColour4444::rf
float rf() const
Definition: colour.cpp:140
smlt::RangeValue
Definition: range_value.h:24
smlt::PackedColour4444::bf
float bf() const
Definition: colour.cpp:149
smlt::PackedColour4444::af
float af() const
Definition: colour.cpp:153
smlt::PackedColour4444::gf
float gf() const
Definition: colour.cpp:145
smlt::Colour
Definition: colour.h:29