mn_color.hpp
Go to the documentation of this file.
1 /*
2 *This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
3 *Copyright (c) 2018-2020 Amber-Sophia Schroeck
4 *
5 *The Mini Thread Library 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, version 3, or (at your option) any later version.
8 
9 *The Mini Thread Library is distributed in the hope that it will be useful, but
10 *WITHOUT ANY WARRANTY; without even the implied warranty of
11 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 *General Public License for more details.
13 *
14 *You should have received a copy of the GNU Lesser General Public
15 *License along with the Mini Thread Library; if not, see
16 *<https://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef __LIBMIN_73d262d0_2534_414e_9be2_0c0c4555de06_H_
20 #define __LIBMIN_73d262d0_2534_414e_9be2_0c0c4555de06_H_
21 
22 #include "../mn_string.hpp"
23 #include "../mn_algorithm.hpp"
24 
25 namespace mn {
26  namespace math {
27 
32  template <typename T>
33  class basic_color {
34  static constexpr double colorcon = 0.003921568627450980392156862745098;
35  public:
36  using value_type = T;
37  using pointer = T*;
39 
40  union {
41  struct {
43  value_type r;
45  value_type g;
47  value_type b;
49  value_type a;
50  };
51  struct {
53  value_type red;
55  value_type green;
57  value_type blue;
59  value_type alpha;
60  };
61  value_type c[4];
62  };
66  basic_color(void)
67  : r(0), g(0), b(0), a(1.0f) {}
77  : r(_r), g(_g), b(_b), a(_a) {}
86  : r(_r), g(_g), b(_b), a(1.0) {}
93  : r(com[0]), g(com[1]), b(com[2]), a(com[3]) {}
101  basic_color(const int _r, const int _g, const int _b)
102  : r((T)(_r) * colorcon), g((T)(_g) * colorcon), b((T)(_b) * colorcon), a(1.0f) {}
111  basic_color(const int _r, const int _g, const int _b, const int _a)
112  : r((T)(_r) * colorcon), g((T)(_g) * colorcon), b((T)(_b) * colorcon), a((T)(_a) * colorcon) {}
118  basic_color(const int* pComponent)
119  : r((T)(pComponent[0]) * colorcon), g((T)(pComponent[1]) * colorcon), b((T)(pComponent[2]) * colorcon), a((T)(pComponent[3]) * colorcon) {}
123  basic_color(const int c)
124  : r(colorcon * (T)(c >> 16)), g(colorcon * (T)(c >> 8)), b(colorcon * (T)(c)), a(colorcon * (T)(c >> 24)) {}
125 
126 
127  self_type& operator = (const self_type& c) {a = c.a; b = c.b; g = c.g; r = c.r; return *this;}
128  self_type& operator += (const self_type& c) {r += c.r; g += c.g; b += c.b; a += c.a; return *this;}
129  self_type& operator -= (const self_type& c) {r -= c.r; g -= c.g; b -= c.b; a -= c.a; return *this;}
130  self_type& operator *= (const self_type& c) {r *= c.r; g *= c.g; b *= c.b; a *= c.a; return *this;}
131  self_type& operator *= (const value_type f) {r *= f; g *= f; b *= f; a *= f; return *this;}
132  self_type& operator /= (const self_type& c) {r /= c.r; g /= c.g; b /= c.b; a /= c.a; return *this;}
133  self_type& operator /= (const value_type f) {r /= f; g /= f; b /= f; a /= f; return *this;}
134 
135  virtual mn::string to_string() {
136  return mn::frmstring("Color: R:%d G:%d B:%d A:%d", red, green, blue, alpha);
137  }
138 
139  operator unsigned long () {
140  return ((a >= 1.0f ? 255 : a <= 0.0f ? 0 : (unsigned long)(a * 255.0f)) << 24) |
141  ((r >= 1.0f ? 255 : r <= 0.0f ? 0 : (unsigned long)(r * 255.0f)) << 16) |
142  ((g >= 1.0f ? 255 : g <= 0.0f ? 0 : (unsigned long)(g * 255.0f)) << 8) |
143  (b >= 1.0f ? 255 : b <= 0.0f ? 0 : (unsigned long)(b * 255.0f));
144  }
145  operator T* () {return (T*)(c);}
146  };
147  template <typename T>
149  return basic_color<T>(a.r + b.r, a.g + b.g, a.b + b.b, a.a + b.a);}
150 
151  template <typename T>
153  return basic_color<T>(a.r - b.r, a.g - b.g, a.b - b.b, a.a - b.a);}
154  template <typename T>
156  return basic_color<T>(-c.r, -c.g, -c.b, c.a);}
157 
158  template <typename T>
160  return basic_color<T>(a.r * b.r, a.g * b.g, a.b * b.b, a.a * b.a);}
161 
162  template <typename T>
163  inline basic_color<T> operator * (const basic_color<T>& c, const T f) {
164  return basic_color<T>(c.r * f, c.g * f, c.b * f, c.a * f);}
165 
166  template <typename T>
167  inline basic_color<T> operator * (const T f, const basic_color<T>& c) {
168  return basic_color<T>(c.r * f, c.g * f, c.b * f, c.a * f);}
169 
170  template <typename T>
172  return basic_color<T>(a.r / b.r, a.g / b.g, a.b / b.b, a.a / b.a);}
173 
174  template <typename T>
175  inline basic_color<T> operator / (const basic_color<T>& c, const T f) {
176  return basic_color<T>(c.r / f, c.g / f, c.b / f, c.a / f);}
177 
178  // Vergleichsoperatoren
179  template <typename T>
180  inline bool operator == (const basic_color<T>& a, const basic_color<T>& b) {
181  if(a.r != b.r) return false; if(a.g != b.g) return false; if(a.b != b.b) return false; return a.a == b.a;}
182 
183  template <typename T>
184  inline bool operator != (const basic_color<T>& a, const basic_color<T>& b) {
185  return !(a == b); }
186 
187  // ******************************************************************
188  // Funktionen deklarieren
189 
193  template <typename T>
195  return basic_color<T>(1.0f - c.r, 1.0f - c.g, 1.0f - c.b, 1.0f - c.a);}
196 
200  template <typename T>
201  inline T brightness(const basic_color<T>& c) {
202  return c.r * 0.299f + c.g * 0.587f + c.b * 0.114f;}
203 
211  template <typename T>
212  inline basic_color<T> interpolate(const basic_color<T>& c1, const basic_color<T>& c2, const T p) {
213  return c1 + p * (c2 - c1);}
214 
219  template <typename T>
220  inline basic_color<T> min(const basic_color<T>& c1, const basic_color<T>& c2) {
221  return basic_color<T>(mn::min<T>(c1.r, c2.r), mn::min<T>(c1.g, c2.g), mn::min<T>(c1.b, c2.b), mn::min<T>(c1.a, c2.a));}
222 
227  template <typename T>
228  inline basic_color<T> max(const basic_color<T>& c1, const basic_color<T>& c2) {
229  return basic_color<T>(mn::max<T>(c1.r, c2.r), mn::max<T>(c1.g, c2.g), mn::max<T>(c1.b, c2.b), mn::max<T>(c1.a, c2.a));}
230 
234  template <typename T>
235  basic_color<T> from_yuv(const T y, const T u, const T v) {
236  return basic_color<T>(T(1.164 * (y - 16) + 1.596*(v - 128)),
237  T(1.164 * (y - 16) - 0.813*(v - 128) - 0.391*(u - 128)),
238  T(1.164 * (y - 16) + 2.018*(u - 128)));
239  }
240 
244  template <typename T>
245  basic_color<T> from_cmy(const T c, const T m, const T y) {
246  return basic_color<T>( T(1.0 - c), T(1.0 - m), T(1.0 - y));
247  }
248 
252  template <typename T>
253  basic_color<T> from_hsv(const T h, const T s, const T v) {
254  if( s == 0 ) return basic_color<T>(v,v,v);
255 
256  T i, f, p, q, t;
257 
258  h /= 60; // sector 0 to 5
259  i = floor( h );
260  f = h - i; // factorial part of h
261  p = v * ( 1 - s );
262  q = v * ( 1 - s * f );
263  t = v * ( 1 - s * ( 1 - f ) );
264 
265  if(i == 0) return basic_color<T>(v, t, p);
266  if(i == 1) return basic_color<T>(q, v, p);
267  if(i == 2) return basic_color<T>(p, v, t);
268  if(i == 3) return basic_color<T>(p, q, v);
269  if(i == 4) return basic_color<T>(t, p, v);
270 
271  return raColor(v, p, q);
272  }
273 
275  }
276 } // namespace mn
277 
278 
279 #endif
A class for RGBA Color handling.
Definition: mn_color.hpp:33
basic_color(void)
Construct a new basic color object.
Definition: mn_color.hpp:66
T * pointer
Definition: mn_color.hpp:37
basic_color(const int _r, const int _g, const int _b)
Construct a new basic color object.
Definition: mn_color.hpp:101
basic_color(value_type *com)
Construct a new basic color object.
Definition: mn_color.hpp:92
basic_color(value_type _r, value_type _g, value_type _b)
Construct a new basic color object.
Definition: mn_color.hpp:85
self_type & operator+=(const self_type &c)
Definition: mn_color.hpp:128
static constexpr double colorcon
Definition: mn_color.hpp:34
T value_type
Definition: mn_color.hpp:36
self_type & operator/=(const self_type &c)
Definition: mn_color.hpp:132
self_type & operator=(const self_type &c)
Definition: mn_color.hpp:127
basic_color(const int c)
Construct a new basic color object.
Definition: mn_color.hpp:123
basic_color(const int _r, const int _g, const int _b, const int _a)
Construct a new basic color object.
Definition: mn_color.hpp:111
self_type & operator*=(const self_type &c)
Definition: mn_color.hpp:130
basic_color(value_type _r, value_type _g, value_type _b, value_type _a)
Construct a new basic color object.
Definition: mn_color.hpp:76
virtual mn::string to_string()
Definition: mn_color.hpp:135
self_type & operator-=(const self_type &c)
Definition: mn_color.hpp:129
basic_color(const int *pComponent)
Construct a new basic color object.
Definition: mn_color.hpp:118
basic_color< T > operator/(const basic_color< T > &a, const basic_color< T > &b)
Definition: mn_color.hpp:171
bool operator!=(const basic_color< T > &a, const basic_color< T > &b)
Definition: mn_color.hpp:184
basic_color< T > interpolate(const basic_color< T > &c1, const basic_color< T > &c2, const T p)
interpolate
Definition: mn_color.hpp:212
basic_color< T > min(const basic_color< T > &c1, const basic_color< T > &c2)
Get the min color.
Definition: mn_color.hpp:220
basic_color< T > operator-(const basic_color< T > &a, const basic_color< T > &b)
Definition: mn_color.hpp:152
basic_color< T > max(const basic_color< T > &c1, const basic_color< T > &c2)
Get the max color.
Definition: mn_color.hpp:228
basic_color< T > operator+(const basic_color< T > &a, const basic_color< T > &b)
Definition: mn_color.hpp:148
basic_color< T > negate(const basic_color< T > &c)
negate
Definition: mn_color.hpp:194
bool operator==(const basic_color< T > &a, const basic_color< T > &b)
Definition: mn_color.hpp:180
basic_color< T > from_cmy(const T c, const T m, const T y)
Create a RGBA color object from a cmy.
Definition: mn_color.hpp:245
basic_color< T > from_hsv(const T h, const T s, const T v)
Create a RGBA color object from a hsv.
Definition: mn_color.hpp:253
basic_color< T > from_yuv(const T y, const T u, const T v)
Create a RGBA color object from a yuv.
Definition: mn_color.hpp:235
T brightness(const basic_color< T > &c)
brightness
Definition: mn_color.hpp:201
basic_color< T > operator*(const basic_color< T > &a, const basic_color< T > &b)
Definition: mn_color.hpp:159
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
Definition: mn_allocator_typetraits.hpp:25