-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.hpp
179 lines (148 loc) · 4.42 KB
/
color.hpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// -*- mode: cuda; -*-
#include <cmath>
#ifndef __LYAP_COLOR_HPP__
#define __LYAP_COLOR_HPP__
static const double COLOR_EPSILON = 1e-6;
#define BOTH __device__ __host__
template<class T,class T4> class COLOR : public T4 {
public:
BOTH COLOR<T,T4>(T _r, T _g, T _b, T _a)
{
this->x = _r;
this->y = _g;
this->z = _b;
this->w = _a;
}
BOTH COLOR<T,T4>()
{
this->x = this->y = this->z = this->w = 0;
}
BOTH COLOR<T,T4> &operator+=(const COLOR<T,T4> &that)
{
this->x += that.x;
this->y += that.y;
this->z += that.z;
this->w += that.w;
return *this;
}
BOTH COLOR<T,T4> &operator-=(const COLOR<T,T4> &that)
{
this->x -= that.x;
this->y -= that.y;
this->z -= that.z;
this->w -= that.w;
return *this;
}
BOTH COLOR<T,T4> &operator*=(const T scale)
{
this->x *= scale;
this->y *= scale;
this->z *= scale;
this->w *= scale;
return *this;
}
BOTH COLOR<T,T4> &operator/=(const T divisor)
{
this->x /= divisor;
this->y /= divisor;
this->z /= divisor;
this->w /= divisor;
return *this;
}
BOTH inline void set(T _r, T _g, T _b, T _a)
{
this->x = _r;
this->y = _g;
this->z = _b;
this->w = _a;
}
BOTH COLOR<T,T4> operator+(const COLOR<T,T4> &that)
{
return COLOR<T,T4>(this->x + that.x, this->y + that.y, this->z + that.z, this->w + that.w);
}
BOTH COLOR<T,T4> operator-(const COLOR<T,T4> &that)
{
return COLOR<T,T4>(this->x - that.x, this->y - that.y, this->z - that.z, this->w - that.w);
}
BOTH COLOR<T,T4> operator*(const T scale)
{
return COLOR<T,T4>(this->x * scale, this->y * scale, this->z * scale, this->w * scale);
}
BOTH COLOR<T,T4> operator/(const T divisor)
{
return COLOR<T,T4>(this->x / divisor, this->y / divisor, this->z / divisor, this->w / divisor);
}
BOTH T mag2()
{
return (this->x*this->x + this->y*this->y + this->z*this->z + this->w*this->w);
}
BOTH T mag()
{
return this->sqrt(this->x*this->x + this->y*this->y + this->z*this->z + this->w*this->w);
}
BOTH COLOR<T,T4> normalized()
{
T mag2 = (this->x*this->x + this->y*this->y + this->z*this->z + this->w*this->w);
if ( mag2 < COLOR_EPSILON ) {
return COLOR<T,T4>();
}
else if (mag2 == 1.0f || (mag2 > (1.0f - COLOR_EPSILON) && mag2 < (1.0f + COLOR_EPSILON))) {
return *this;
}
else {
return *this / this->sqrt(mag2);
}
}
BOTH void normalize()
{
T mag2 = (this->x*this->x + this->y*this->y + this->z*this->z + this->w*this->w);
if ( mag2 < COLOR_EPSILON ) {
this->x = this->y = this->z = this->w = 0;
}
else if (mag2 == 1.0f || (mag2 > (1.0f - COLOR_EPSILON) && mag2 < (1.0f + COLOR_EPSILON))) {
// nop
}
else {
mag2 = this->sqrt(mag2);
this->x /= mag2;
this->y /= mag2;
this->z /= mag2;
this->w /= mag2;
}
}
BOTH static void clamp(T &that)
{
if (that < 0.0) that = 0.0;
else if (that > 1.0) that = 1.0;
}
BOTH static T clamped(T that)
{
return that<0.0 ? 0.0 : that>1.0 ? 1.0 : that;
}
BOTH void clamp()
{
if (this->x < 0.0) this->x = 0.0;
else if (this->x > 1.0) this->x = 1.0;
if (this->y < 0.0) this->y = 0.0;
else if (this->y > 1.0) this->y = 1.0;
if (this->z < 0.0) this->z = 0.0;
else if (this->z > 1.0) this->z = 1.0;
if (this->w < 0.0) this->w = 0.0;
else if (this->w > 1.0) this->w = 1.0;
}
BOTH COLOR<T,T4> clamped()
{
return COLOR<T,T4>(this->x<0.0 ? 0.0 : this->x>1.0 ? 1.0 : this->x,
this->y<0.0 ? 0.0 : this->y>1.0 ? 1.0 : this->y,
this->z<0.0 ? 0.0 : this->z>1.0 ? 1.0 : this->z,
this->w<0.0 ? 0.0 : this->w>1.0 ? 1.0 : this->w);
}
BOTH void to_rgba(unsigned char *rgba)
{
*rgba++ = (unsigned char)(255.0 * this->x);
*rgba++ = (unsigned char)(255.0 * this->y);
*rgba++ = (unsigned char)(255.0 * this->z);
*rgba++ = (unsigned char)(255.0 * this->w);
}
};
#endif