-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColor.h
154 lines (152 loc) · 9.29 KB
/
Color.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Color.h
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright (c) 2019-2024 Adrian Maurer. All rights reserved.
// Distributed under the MIT software license (http://www.opensource.org/licenses/mit-license.php).
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#ifndef AMA_COLOR_H
#define AMA_COLOR_H
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
namespace ama {
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Color<T>
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
class Color
{
// ELEMENT DATA
public:
T r, g, b;
// CONSTRUCTION / DESTRUCTION / ASSIGNMENT
public:
constexpr Color() noexcept : r(T(0)), g(T(0)), b(T(0)) {}
constexpr Color(const T v) noexcept : r(v), g(v), b(v) {}
constexpr Color(const T vr, const T vg, const T vb) noexcept : r(vr), g(vg), b(vb) {}
// ELEMENT FUNCTIONS
public:
constexpr Color& operator+=(const Color<T>& c) noexcept {r += c.r, g += c.g, b += c.b; return *this;}
constexpr Color& operator-=(const Color<T>& c) noexcept {r -= c.r, g -= c.g, b -= c.b; return *this;}
constexpr Color& operator*=(const Color<T>& c) noexcept {r *= c.r, g *= c.g, b *= c.b; return *this;}
constexpr Color& operator/=(const Color<T>& c) noexcept {r /= c.r, g /= c.g, b /= c.b; return *this;}
constexpr Color& operator+=(const T v) noexcept {r += v, g += v, b += v; return *this;}
constexpr Color& operator-=(const T v) noexcept {r -= v, g -= v, b -= v; return *this;}
constexpr Color& operator*=(const T v) noexcept {r *= v, g *= v, b *= v; return *this;}
constexpr Color& operator/=(const T v) noexcept {r /= v, g /= v, b /= v; return *this;}
constexpr const T& operator[](const int) const noexcept;
constexpr T& operator[](const int) noexcept;
};
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Color<T> - ELEMENT FUNCTIONS
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr const T& Color<T>::operator[](const int i) const noexcept
{
switch (i)
{
case 0: return r;
case 1: return g;
default: return b;
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr T& Color<T>::operator[](const int i) noexcept
{
switch (i)
{
case 0: return r;
case 1: return g;
default: return b;
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Color<T> - GOBAL FUNCTIONS
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Color<T> operator+(Color<T> c) noexcept
{
return c;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Color<T> operator-(Color<T> c) noexcept
{
return {-c.r, -c.g, -c.b};
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Color<T> operator+(Color<T> c1, const Color<T>& c2) noexcept
{
return c1 += c2;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Color<T> operator-(Color<T> c1, const Color<T>& c2) noexcept
{
return c1 -= c2;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Color<T> operator*(Color<T> c1, const Color<T>& c2) noexcept
{
return c1 *= c2;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Color<T> operator/(Color<T> c1, const Color<T>& c2) noexcept
{
return c1 /= c2;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Color<T> operator+(Color<T> c, const T v) noexcept
{
return c += v;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Color<T> operator+(const T v, Color<T> c) noexcept
{
return c += v;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Color<T> operator-(Color<T> c, const T v) noexcept
{
return c -= v;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Color<T> operator-(const T v, const Color<T>& c) noexcept
{
return {v - c.r, v - c.g, v - c.b};
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Color<T> operator*(Color<T> c, const T v) noexcept
{
return c *= v;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Color<T> operator*(const T v, Color<T> c) noexcept
{
return c *= v;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Color<T> operator/(Color<T> c, const T v) noexcept
{
return c /= v;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
constexpr Color<T> operator/(const T v, const Color<T>& c) noexcept
{
return {v / c.r, v / c.g, v / c.b};
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
} //namespace ama
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#endif // AMA_COLOR_H
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------