1 | /** |
2 | * Copyright (c) 2006-2023 LOVE Development Team |
3 | * |
4 | * This software is provided 'as-is', without any express or implied |
5 | * warranty. In no event will the authors be held liable for any damages |
6 | * arising from the use of this software. |
7 | * |
8 | * Permission is granted to anyone to use this software for any purpose, |
9 | * including commercial applications, and to alter it and redistribute it |
10 | * freely, subject to the following restrictions: |
11 | * |
12 | * 1. The origin of this software must not be misrepresented; you must not |
13 | * claim that you wrote the original software. If you use this software |
14 | * in a product, an acknowledgment in the product documentation would be |
15 | * appreciated but is not required. |
16 | * 2. Altered source versions must be plainly marked as such, and must not be |
17 | * misrepresented as being the original software. |
18 | * 3. This notice may not be removed or altered from any source distribution. |
19 | **/ |
20 | |
21 | #ifndef LOVE_COLOR_H |
22 | #define LOVE_COLOR_H |
23 | |
24 | namespace love |
25 | { |
26 | |
27 | template <typename T> |
28 | struct ColorT |
29 | { |
30 | T r; |
31 | T g; |
32 | T b; |
33 | T a; |
34 | |
35 | ColorT() : r(0), g(0), b(0), a(0) {} |
36 | ColorT(T r_, T g_, T b_, T a_) : r(r_), g(g_), b(b_), a(a_) {} |
37 | void set(T r_, T g_, T b_, T a_) |
38 | { |
39 | r = r_; |
40 | g = g_; |
41 | b = b_; |
42 | a = a_; |
43 | } |
44 | |
45 | bool operator==(const ColorT<T> &other) const; |
46 | bool operator!=(const ColorT<T> &other) const; |
47 | |
48 | ColorT<T> operator+=(const ColorT<T> &other); |
49 | ColorT<T> operator*=(const ColorT<T> &other); |
50 | ColorT<T> operator*=(T s); |
51 | ColorT<T> operator/=(T s); |
52 | }; |
53 | |
54 | template <typename T> |
55 | bool ColorT<T>::operator==(const ColorT<T> &other) const |
56 | { |
57 | return r == other.r && g == other.g && b == other.b && a == other.a; |
58 | } |
59 | |
60 | template <typename T> |
61 | bool ColorT<T>::operator!=(const ColorT<T> &other) const |
62 | { |
63 | return !(operator==(other)); |
64 | } |
65 | |
66 | template <typename T> |
67 | ColorT<T> ColorT<T>::operator+=(const ColorT<T> &other) |
68 | { |
69 | r += other.r; |
70 | g += other.g; |
71 | b += other.b; |
72 | a += other.a; |
73 | return *this; |
74 | } |
75 | |
76 | template <typename T> |
77 | ColorT<T> ColorT<T>::operator*=(const ColorT<T> &other) |
78 | { |
79 | r *= other.r; |
80 | g *= other.g; |
81 | b *= other.b; |
82 | a *= other.a; |
83 | return *this; |
84 | } |
85 | |
86 | template <typename T> |
87 | ColorT<T> ColorT<T>::operator*=(T s) |
88 | { |
89 | r *= s; |
90 | g *= s; |
91 | b *= s; |
92 | a *= s; |
93 | return *this; |
94 | } |
95 | |
96 | template <typename T> |
97 | ColorT<T> ColorT<T>::operator/=(T s) |
98 | { |
99 | r /= s; |
100 | g /= s; |
101 | b /= s; |
102 | a /= s; |
103 | return *this; |
104 | } |
105 | |
106 | template <typename T> |
107 | ColorT<T> operator+(const ColorT<T> &a, const ColorT<T> &b) |
108 | { |
109 | ColorT<T> tmp(a); |
110 | return tmp += b; |
111 | } |
112 | |
113 | template <typename T> |
114 | ColorT<T> operator*(const ColorT<T> &a, const ColorT<T> &b) |
115 | { |
116 | ColorT<T> res; |
117 | res.r = a.r * b.r; |
118 | res.g = a.g * b.g; |
119 | res.b = a.b * b.b; |
120 | res.a = a.a * b.a; |
121 | return res; |
122 | } |
123 | |
124 | template <typename T> |
125 | ColorT<T> operator*(const ColorT<T> &a, T s) |
126 | { |
127 | ColorT<T> tmp(a); |
128 | return tmp *= s; |
129 | } |
130 | |
131 | template <typename T> |
132 | ColorT<T> operator/(const ColorT<T> &a, T s) |
133 | { |
134 | ColorT<T> tmp(a); |
135 | return tmp /= s; |
136 | } |
137 | |
138 | typedef ColorT<unsigned char> Color32; |
139 | typedef ColorT<float> Colorf; |
140 | |
141 | inline Color32 toColor32(Colorf cf) |
142 | { |
143 | return Color32((unsigned char) (cf.r * 255.0f), |
144 | (unsigned char) (cf.g * 255.0f), |
145 | (unsigned char) (cf.b * 255.0f), |
146 | (unsigned char) (cf.a * 255.0f)); |
147 | } |
148 | |
149 | inline Colorf toColorf(Color32 c) |
150 | { |
151 | return Colorf(c.r / 255.0f, c.g / 255.0f, c.b / 255.0f, c.a / 255.0f); |
152 | } |
153 | |
154 | } // love |
155 | |
156 | #endif // LOVE_COLOR_H |
157 | |