Simulant  21.12-574
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  /* Read count colour components from buf. All other components will be zero
40  * except alpha which will be one. */
41  Colour(const float* buf, std::size_t count):
42  r(0), g(0), b(0), a(1) {
43  if(count > 0) {
44  r = *buf++;
45  }
46  if(count > 1) {
47  g = *buf++;
48  }
49  if(count > 2) {
50  b = *buf++;
51  }
52  if(count > 3) {
53  a = *buf++;
54  }
55  }
56 
57  Colour(float r, float g, float b, float a):
58  r(r), g(g), b(b), a(a) {}
59 
60  Colour operator*(const float rhs) const {
61  return Colour(r * rhs, g * rhs, b * rhs, a * rhs);
62  }
63 
64  Colour operator*=(const float rhs) {
65  *this = *this * rhs;
66  return *this;
67  }
68 
69  Colour operator-(const Colour& rhs) const {
70  return Colour(r - rhs.r, g - rhs.g, b - rhs.b, a - rhs.a);
71  }
72 
73  Colour operator-=(const Colour& rhs) {
74  *this = *this - rhs;
75  return *this;
76  }
77 
78  Colour operator+(const Colour& rhs) const {
79  return Colour(r + rhs.r, g + rhs.g, b + rhs.b, a + rhs.a);
80  }
81 
82  Colour operator+=(const Colour& rhs) {
83  *this = *this + rhs;
84  return *this;
85  }
86 
87  bool operator==(const Colour& rhs) const {
88  return this->r == rhs.r && this->g == rhs.g && this->b == rhs.b && this->a == rhs.a;
89  }
90 
91  bool operator!=(const Colour& rhs) const {
92  return this->r != rhs.r || this->g != rhs.g || this->b != rhs.b || this->a != rhs.a;
93  }
94 
95  bool operator<(const Colour& rhs) const {
96  if(r < rhs.r) {
97  return true;
98  } else if (r == rhs.r) {
99  if(g < rhs.g) {
100  return true;
101  } else if(g == rhs.g) {
102  if(b < rhs.b) {
103  return true;
104  } else if(b == rhs.b) {
105  if(a < rhs.a) {
106  return true;
107  }
108  }
109  }
110  }
111 
112  return false;
113  }
114 
115 
116  Colour lerp(const Colour& end, float t) const;
117 
118  std::string to_hex_string() const;
119  static Colour from_hex_string(const std::string& hex_string);
120 
121  static const Colour BLACK;
122  static const Colour GREY;
123  static const Colour WHITE;
124  static const Colour RED;
125  static const Colour GREEN;
126  static const Colour BLUE;
127  static const Colour YELLOW;
128  static const Colour PURPLE;
129  static const Colour TURQUOISE;
130  static const Colour NONE;
131 
132 };
133 
134 /* 16bit packed colour in ARGB4444 format. This is useful for minimising
135  * ram usage when colour fidelity isn't important. */
137 public:
139  PackedColour4444(const Colour& c);
140 
141  PackedColour4444& operator=(const PackedColour4444&) = default;
142  PackedColour4444& operator=(const Colour& rhs);
143 
144  bool operator==(const PackedColour4444& rhs) const;
145  bool operator==(const Colour& rhs) const;
146 
147  bool operator!=(const Colour& rhs) const {
148  return !(*this == rhs);
149  }
150 
151  operator Colour() {
152  Colour c(rf(), gf(), bf(), af());
153  return c;
154  }
155 
156  void set_alpha(NormalizedFloat a);
157 
158  uint8_t r8() const;
159  uint8_t g8() const;
160  uint8_t b8() const;
161  uint8_t a8() const;
162 
165  float rf() const;
166 
169  float gf() const;
170 
173  float bf() const;
174 
177  float af() const;
178 
179 private:
180  uint16_t colour_;
181 };
182 
183 
184 std::ostream& operator<<(std::ostream& stream, const Colour& c);
185 }
186 
187 #endif // COLOUR_H
smlt::PackedColour4444
Definition: colour.h:136
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