Plasma Engine  2.0
Loading...
Searching...
No Matches
Color_inl.h
1#pragma once
2
4{
5#if PL_ENABLED(PL_MATH_CHECK_FOR_NAN)
6 // Initialize all data to NaN in debug mode to find problems with uninitialized data easier.
7 const float TypeNaN = plMath::NaN<float>();
8 r = TypeNaN;
9 g = TypeNaN;
10 b = TypeNaN;
11 a = TypeNaN;
12#endif
13}
14
15PL_FORCE_INLINE constexpr plColor::plColor(float fLinearRed, float fLinearGreen, float fLinearBlue, float fLinearAlpha /* = 1.0f */)
16 : r(fLinearRed)
17 , g(fLinearGreen)
18 , b(fLinearBlue)
19 , a(fLinearAlpha)
20{
21}
22
24{
25 *this = cc;
26}
27
29{
30 *this = cc;
31}
32
33inline void plColor::SetRGB(float fLinearRed, float fLinearGreen, float fLinearBlue)
34{
35 r = fLinearRed;
36 g = fLinearGreen;
37 b = fLinearBlue;
38}
39
40inline void plColor::SetRGBA(float fLinearRed, float fLinearGreen, float fLinearBlue, float fLinearAlpha /* = 1.0f */)
41{
42 r = fLinearRed;
43 g = fLinearGreen;
44 b = fLinearBlue;
45 a = fLinearAlpha;
46}
47
48inline plColor plColor::MakeFromKelvin(plUInt32 uiKelvin)
49{
50 plColor finalColor;
51 float kelvin = plMath::Clamp(uiKelvin, 1000u, 40000u) / 1000.0f;
52 float kelvin2 = kelvin * kelvin;
53
54 // Red
55 finalColor.r = kelvin < 6.570f ? 1.0f : plMath::Clamp((1.35651f + 0.216422f * kelvin + 0.000633715f * kelvin2) / (-3.24223f + 0.918711f * kelvin), 0.0f, 1.0f);
56 // Green
57 finalColor.g = kelvin < 6.570f ? plMath::Clamp((-399.809f + 414.271f * kelvin + 111.543f * kelvin2) / (2779.24f + 164.143f * kelvin + 84.7356f * kelvin2), 0.0f, 1.0f) : plMath::Clamp((1370.38f + 734.616f * kelvin + 0.689955f * kelvin2) / (-4625.69f + 1699.87f * kelvin), 0.0f, 1.0f);
58 // Blue
59 finalColor.b = kelvin > 6.570f ? 1.0f : plMath::Clamp((348.963f - 523.53f * kelvin + 183.62f * kelvin2) / (2848.82f - 214.52f * kelvin + 78.8614f * kelvin2), 0.0f, 1.0f);
60
61 return finalColor;
62}
63
64// http://en.wikipedia.org/wiki/Luminance_%28relative%29
65PL_FORCE_INLINE float plColor::GetLuminance() const
66{
67 return 0.2126f * r + 0.7152f * g + 0.0722f * b;
68}
69
71{
72 PL_NAN_ASSERT(this);
73 PL_ASSERT_DEBUG(IsNormalized(), "Cannot invert a color that has values outside the [0; 1] range");
74
75 return plColor(1.0f - r, 1.0f - g, 1.0f - b, 1.0f - a);
76}
77
78inline bool plColor::IsNaN() const
79{
80 if (plMath::IsNaN(r))
81 return true;
82 if (plMath::IsNaN(g))
83 return true;
84 if (plMath::IsNaN(b))
85 return true;
86 if (plMath::IsNaN(a))
87 return true;
88
89 return false;
90}
91
92inline void plColor::operator+=(const plColor& rhs)
93{
94 PL_NAN_ASSERT(this);
95 PL_NAN_ASSERT(&rhs);
96
97 r += rhs.r;
98 g += rhs.g;
99 b += rhs.b;
100 a += rhs.a;
101}
102
103inline void plColor::operator-=(const plColor& rhs)
104{
105 PL_NAN_ASSERT(this);
106 PL_NAN_ASSERT(&rhs);
107
108 r -= rhs.r;
109 g -= rhs.g;
110 b -= rhs.b;
111 a -= rhs.a;
112}
113
114inline void plColor::operator*=(const plColor& rhs)
115{
116 PL_NAN_ASSERT(this);
117 PL_NAN_ASSERT(&rhs);
118
119 r *= rhs.r;
120 g *= rhs.g;
121 b *= rhs.b;
122 a *= rhs.a;
123}
124inline void plColor::operator*=(float f)
125{
126 r *= f;
127 g *= f;
128 b *= f;
129 a *= f;
130
131 PL_NAN_ASSERT(this);
132}
133
134inline bool plColor::IsIdenticalRGB(const plColor& rhs) const
135{
136 PL_NAN_ASSERT(this);
137 PL_NAN_ASSERT(&rhs);
138
139 return r == rhs.r && g == rhs.g && b == rhs.b;
140}
141
142inline bool plColor::IsIdenticalRGBA(const plColor& rhs) const
143{
144 PL_NAN_ASSERT(this);
145 PL_NAN_ASSERT(&rhs);
146
147 return r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a;
148}
149
150inline plColor plColor::WithAlpha(float fAlpha) const
151{
152 return plColor(r, g, b, fAlpha);
153}
154
155inline const plColor operator+(const plColor& c1, const plColor& c2)
156{
157 PL_NAN_ASSERT(&c1);
158 PL_NAN_ASSERT(&c2);
159
160 return plColor(c1.r + c2.r, c1.g + c2.g, c1.b + c2.b, c1.a + c2.a);
161}
162
163inline const plColor operator-(const plColor& c1, const plColor& c2)
164{
165 PL_NAN_ASSERT(&c1);
166 PL_NAN_ASSERT(&c2);
167
168 return plColor(c1.r - c2.r, c1.g - c2.g, c1.b - c2.b, c1.a - c2.a);
169}
170
171inline const plColor operator*(const plColor& c1, const plColor& c2)
172{
173 PL_NAN_ASSERT(&c1);
174 PL_NAN_ASSERT(&c2);
175
176 return plColor(c1.r * c2.r, c1.g * c2.g, c1.b * c2.b, c1.a * c2.a);
177}
178
179inline const plColor operator*(float f, const plColor& c)
180{
181 PL_NAN_ASSERT(&c);
182
183 return plColor(c.r * f, c.g * f, c.b * f, c.a * f);
184}
185
186inline const plColor operator*(const plColor& c, float f)
187{
188 PL_NAN_ASSERT(&c);
189
190 return plColor(c.r * f, c.g * f, c.b * f, c.a * f);
191}
192
193inline const plColor operator*(const plMat4& lhs, const plColor& rhs)
194{
195 plColor r = rhs;
196 r *= lhs;
197 return r;
198}
199
200inline const plColor operator/(const plColor& c, float f)
201{
202 PL_NAN_ASSERT(&c);
203
204 float f_inv = 1.0f / f;
205 return plColor(c.r * f_inv, c.g * f_inv, c.b * f_inv, c.a * f_inv);
206}
207
208PL_ALWAYS_INLINE bool operator==(const plColor& c1, const plColor& c2)
209{
210 return c1.IsIdenticalRGBA(c2);
211}
212
213PL_ALWAYS_INLINE bool operator!=(const plColor& c1, const plColor& c2)
214{
215 return !c1.IsIdenticalRGBA(c2);
216}
217
218PL_FORCE_INLINE bool operator<(const plColor& c1, const plColor& c2)
219{
220 if (c1.r < c2.r)
221 return true;
222 if (c1.r > c2.r)
223 return false;
224 if (c1.g < c2.g)
225 return true;
226 if (c1.g > c2.g)
227 return false;
228 if (c1.b < c2.b)
229 return true;
230 if (c1.b > c2.b)
231 return false;
232
233 return (c1.a < c2.a);
234}
A 8bit per channel unsigned normalized (values interpreted as 0-1) color storage format that represen...
Definition Color8UNorm.h:99
plColor represents an RGBA color in linear color space. Values are stored as float,...
Definition Color.h:44
float GetLuminance() const
Computes the perceived luminance. Assumes linear color space (http://en.wikipedia....
Definition Color_inl.h:65
bool IsNaN() const
Returns true, if any of r, g, b or a is NaN.
Definition Color_inl.h:78
void operator-=(const plColor &rhs)
Subtracts rhs component-wise from this vector.
Definition Color_inl.h:103
bool IsIdenticalRGB(const plColor &rhs) const
Equality Check (bitwise). Only compares RGB, ignores Alpha.
Definition Color_inl.h:134
plColor GetInvertedColor() const
[tested]
Definition Color_inl.h:70
plColor WithAlpha(float fAlpha) const
Returns the current color but with changes the alpha value to the given value.
Definition Color_inl.h:150
void operator+=(const plColor &rhs)
Adds rhs component-wise to this color.
Definition Color_inl.h:92
void SetRGB(float fLinearRed, float fLinearGreen, float fLinearBlue)
Sets the RGB components, ignores alpha.
Definition Color_inl.h:33
bool IsIdenticalRGBA(const plColor &rhs) const
Equality Check (bitwise). Compares all four components.
Definition Color_inl.h:142
void SetRGBA(float fLinearRed, float fLinearGreen, float fLinearBlue, float fLinearAlpha=1.0f)
Sets all four RGBA components.
Definition Color_inl.h:40
void operator*=(const plColor &rhs)
Multiplies rhs component-wise with this color.
Definition Color_inl.h:114
plColor()
default-constructed color is uninitialized (for speed)
Definition Color_inl.h:3
bool IsNormalized() const
Returns if the color is in the Range [0; 1] on all 4 channels.
Definition Color.cpp:33
static plColor MakeFromKelvin(plUInt32 uiKelvin)
Returns a color created from the kelvin temperature. https://wikipedia.org/wiki/Color_temperature Ori...
Definition Color_inl.h:48
A 8bit per channel unsigned normalized (values interpreted as 0-1) color storage format that represen...
Definition Color8UNorm.h:61
constexpr PL_ALWAYS_INLINE T Clamp(T value, T min_val, T max_val)
Clamps "value" to the range [min; max]. Returns "value", if it is inside the range already.
Definition Math_inl.h:51
constexpr TYPE NaN()
Returns the value for NaN as the template type. Returns zero, if the type does not support NaN.
Definition Constants_inl.h:58