neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
color.inl
Go to the documentation of this file.
1// color.inl
2/*
3 neogfx C++ App/Game Engine
4 Copyright (c) 2015, 2020 Leigh Johnston. All Rights Reserved.
5
6 This program is free software: you can redistribute it and / or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#pragma once
21
22#include <neogfx/neogfx.hpp>
24#include <neogfx/gfx/color.hpp>
25
26namespace neogfx
27{
28 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
33
34 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
39
40 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
42 {
43 return (red() << RedShift) | (green() << GreenShift) | (blue() << BlueShift) | (alpha() << AlphaShift);
44 }
45
46 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
51
52 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
57
58 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
60 {
61 return (red<double>() + green<double>() + blue<double>()) / 3.0;
62 }
63
64 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
66 {
67 return std::abs(intensity() - aOther.intensity()) <= aThreshold;
68 }
69
70 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
72 {
74 (self_type::template red<double>() + aOther.template red<double>()) / 2.0,
75 (self_type::template green<double>() + aOther.template green<double>()) / 2.0,
76 (self_type::template blue<double>() + aOther.template blue<double>()) / 2.0,
77 (base_type::template alpha<double>() + aOther.template alpha<double>()) / 2.0 };
78 }
79
80 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
82 {
84 (self_type::template red<double>() * (1 - aValue) + aOther.template red<double>() * aValue),
85 (self_type::template green<double>() * (1 - aValue) + aOther.template green<double>() * aValue),
86 (self_type::template blue<double>() * (1 - aValue) + aOther.template blue<double>() * aValue),
87 (base_type::template alpha<double>() * (1 - aValue) + aOther.template alpha<double>() * aValue) };
88 }
89
90 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
92 {
93 return to_hsl().lightness() >= aThreshold;
94 }
95
96 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
98 {
99 return to_hsl().lightness() < aThreshold;
100 }
101
102 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
108
109 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
115
116 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
123
124 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
131
132 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
134 {
135 if (light())
136 return darken(aDelta);
137 else
138 return lighten(aDelta);
139 }
140
141 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
143 {
144 if (light())
145 return lighten(aDelta);
146 else
147 return darken(aDelta);
148 }
149
150 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
152 {
153 if (light())
154 return darker(aDelta);
155 else
156 return lighter(aDelta);
157 }
158
159 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
161 {
162 if (light())
163 return lighter(aDelta);
164 else
165 return darker(aDelta);
166 }
167
168 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
174
175 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
182
183 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
190
191 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
196
197 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
199 {
200 view_component newRed = std::numeric_limits<view_component>::max() - red() < aDelta ? std::numeric_limits<view_component>::max() : red() + aDelta;
201 view_component newGreen = std::numeric_limits<view_component>::max() - green() < aDelta ? std::numeric_limits<view_component>::max() : green() + aDelta;
202 view_component newBlue = std::numeric_limits<view_component>::max() - blue() < aDelta ? std::numeric_limits<view_component>::max() : blue() + aDelta;
203 *this = basic_rgb_color<ColorSpace, BaseComponent, ViewComponent, Derived>{ newRed, newGreen, newBlue, alpha() };
204 return static_cast<return_type&>(*this);
205 }
206
207 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
209 {
210 view_component newRed = red() < aDelta ? std::numeric_limits<view_component>::min() : red() - aDelta;
211 view_component newGreen = green() < aDelta ? std::numeric_limits<view_component>::min() : green() - aDelta;
212 view_component newBlue = blue() < aDelta ? std::numeric_limits<view_component>::min() : blue() - aDelta;
213 *this = basic_rgb_color<ColorSpace, BaseComponent, ViewComponent, Derived>{ newRed, newGreen, newBlue, alpha() };
214 return static_cast<return_type&>(*this);
215 }
216
217 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
222
223 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
228
229 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
231 {
232 hsv_color left = to_hsv();
233 hsv_color right = aOther.to_hsv();
234 return std::make_tuple(left.hue(), left.saturation(), left.value(), left.alpha()) < std::make_tuple(right.hue(), right.saturation(), right.value(), right.alpha());
235 }
236
237 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
239 {
240 std::ostringstream result;
241 result << "rgba(" << static_cast<int>(red()) << ", " << static_cast<int>(green()) << ", " << static_cast<int>(blue()) << ", " << alpha() / 255.0 << ");";
242 return result.str();
243 }
244
245 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
247 {
248 std::ostringstream result;
249 result << "#" << std::uppercase << std::hex << std::setfill('0') << std::setw(6) << with_alpha(0).as_argb();
250 if (alpha() != 0xFF)
251 result << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << static_cast<uint32_t>(alpha());
252 return result.str();
253 }
254
255 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
262
263 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
270
271 template <color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
273 {
275 result *= vec4 { aCoefficient, aCoefficient, aCoefficient, aCoefficient };
276 return result;
277 }
278
279 template <typename Elem, typename Traits, color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
280 inline std::basic_ostream<Elem, Traits>& operator<<(std::basic_ostream<Elem, Traits>& aStream, const basic_rgb_color<ColorSpace, BaseComponent, ViewComponent, Derived>& aColor)
281 {
282 aStream << std::setprecision(4) << "[" << aColor.red<double>() << ", " << aColor.green<double>() << ", " << aColor.blue<double>() << ", " << aColor.alpha<double>() << "]";
283 return aStream;
284 }
285
286 template <typename Elem, typename Traits, color_space ColorSpace, typename BaseComponent, typename ViewComponent, typename Derived>
287 inline std::basic_istream<Elem, Traits>& operator>>(std::basic_istream<Elem, Traits>& aStream, basic_rgb_color<ColorSpace, BaseComponent, ViewComponent, Derived>& aColor)
288 {
289 auto previousImbued = aStream.getloc();
290 if (typeid(std::use_facet<std::ctype<char>>(previousImbued)) != typeid(neolib::comma_as_whitespace))
291 aStream.imbue(std::locale{ previousImbued, new neolib::comma_as_whitespace{} });
292 char ignore;
293 aStream >> ignore >> aColor[0] >> aColor[1] >> aColor[2] >> aColor[3] >> ignore;
294 aStream.imbue(previousImbued);
295 return aStream;
296 }
297}
ViewComponent view_component
Definition color.hpp:83
return_type & lighten(view_component aDelta)
Definition color.inl:103
return_type lighter(view_component aDelta) const
Definition color.inl:117
static return_type from_hsv(scalar aHue, scalar aSaturation, scalar aValue, scalar aAlpha=1.0)
Definition color.inl:35
return_type & darken(view_component aDelta)
Definition color.inl:110
bool operator==(const self_type &aOther) const
Definition color.inl:224
return_type operator~() const
Definition color.inl:218
return_type mix(const self_type &aOther, scalar aValue) const
Definition color.inl:81
std::string to_string() const
Definition color.inl:238
return_type shaded(view_component aDelta) const
Definition color.inl:151
return_type & unshade(view_component aDelta)
Definition color.inl:142
hsv_color to_hsv() const
Definition color.inl:53
return_type mid(const self_type &aOther) const
Definition color.inl:71
bool similar_intensity(const self_type &aOther, scalar aThreshold=0.5) const
Definition color.inl:65
return_type unshaded(view_component aDelta) const
Definition color.inl:160
bool operator<(const self_type &aOther) const
Definition color.inl:230
bool light(scalar aThreshold=0.50) const
Definition color.inl:91
return_type inverse() const
Definition color.inl:192
color_return_type_cracker< self_type, derived_type >::return_type return_type
Definition color.hpp:84
argb as_argb() const
Definition color.inl:41
hsl_color to_hsl() const
Definition color.inl:47
bool dark(scalar aThreshold=0.50) const
Definition color.inl:97
return_type same_lightness_as(const self_type &aOther) const
Definition color.inl:176
return_type & operator+=(view_component aDelta)
Definition color.inl:198
std::string to_hex_string() const
Definition color.inl:246
static return_type from_hsl(scalar aHue, scalar aSaturation, scalar aLightness, scalar aAlpha=1.0)
Definition color.inl:29
return_type & shade(view_component aDelta)
Definition color.inl:133
scalar intensity() const
Definition color.inl:59
return_type & operator-=(view_component aDelta)
Definition color.inl:208
return_type with_lightness(scalar aLightness) const
Definition color.inl:184
return_type monochrome() const
Definition color.inl:169
return_type darker(view_component aDelta) const
Definition color.inl:125
ViewComponent view_component
Definition color.hpp:83
void set_lightness(double aLightness)
double lightness() const
void to_rgb(scalar &aRed, scalar &aGreen, scalar &aBlue, scalar &aAlpha) const
double hue() const
double value() const
double saturation() const
double alpha() const
void to_rgb(scalar &aRed, scalar &aGreen, scalar &aBlue, scalar &aAlpha) const
std::basic_istream< Elem, Traits > & operator>>(std::basic_istream< Elem, Traits > &aStream, basic_point< T > &aPoint)
basic_delta< CoordinateType > operator-(const basic_delta< CoordinateType > &left, CoordinateType s)
basic_delta< CoordinateType > operator*(const basic_delta< CoordinateType > &left, CoordinateType s)
basic_delta< CoordinateType > operator+(const basic_delta< CoordinateType > &left, CoordinateType s)
double scalar
Definition numerical.hpp:63