26 constexpr explicit TColor(T value = 0.0) noexcept :
TColor(value, value, value, value) {}
27 constexpr explicit TColor(T value, T alpha) noexcept :
TColor(value, value, value, alpha) {}
29 constexpr explicit TColor(T red, T green, T blue, T alpha = 1.0) noexcept : r(red), g(green), b(blue), a(alpha) {}
34 template<
typename U>
constexpr explicit TColor(
const TColor<U>& other) noexcept : r(T(other.r)), g(T(other.g)), b(T(other.b)), a(T(other.a)) {}
53 const double M = std::max({r, g, b});
54 const double m = std::min({r, g, b});
55 const double C = M - m;
57 const double L = 0.5 * (M + m);
58 const double S = L > 0.99999 ? 0.0 : C / (1.0 - std::abs(2.0 * L - 1.0));
59 const double H = [&]() {
65 H_prime = ((g - b) / C);
67 H_prime = ((b - r) / C) + 2.0;
69 H_prime = ((r - g) / C) + 4.0;
75 return H_prime > 0.0 ? H_prime / 6.0 : H_prime / 6.0 + 1.0;
86 const double C = (1.0 - std::abs(2.0 * L - 1.0)) * S;
87 const double m = L - 0.5 * C;
89 const double H_prime = H * 6.0;
90 const double H_prime_mod_2 = [&]() {
93 }
else if(H_prime < 4.0) {
95 }
else if(H_prime <= 6.0) {
102 const double X = C * (1.0 - std::abs(H_prime_mod_2 - 1.0));
104 const TColor rgb_color_base = [&]() {
106 return TColor(C, X, 0.0, 0.0);
107 }
else if(H_prime < 2.0) {
108 return TColor(X, C, 0.0, 0.0);
109 }
else if(H_prime < 3.0) {
110 return TColor(0.0, C, X, 0.0);
111 }
else if(H_prime < 4.0) {
112 return TColor(0.0, X, C, 0.0);
113 }
else if(H_prime < 5.0) {
114 return TColor(X, 0.0, C, 0.0);
115 }
else if(H_prime <= 6.0) {
116 return TColor(C, 0.0, X, 0.0);
122 return rgb_color_base +
TColor(m, m, m);
138 hsl.
b = Math::clamp(hsl.
b, 0.0, 1.0);
150 [[nodiscard]]
constexpr TColor invert(T amount = 0.0,
bool applyOnAlpha =
false) const noexcept {
151 return TColor((T(1.0) - amount) - r, (T(1.0) - amount) - g, (T(1.0) - amount) - b, applyOnAlpha ? (T(1.0) - amount) - a : a);
175 const T s = saturation;
177 return TColor(clamp((0.213 + 0.787 * s) * r + (0.715 - 0.715 * s) * g + (0.072 - 0.072 * s) * b, 0.0, 1.0),
178 clamp((0.213 - 0.213 * s) * r + (0.715 + 0.285 * s) * g + (0.072 - 0.072 * s) * b, 0.0, 1.0),
179 clamp((0.213 - 0.213 * s) * r + (0.715 - 0.715 * s) * g + (0.072 + 0.928 * s) * b, 0.0, 1.0), a);
202 const T invRhs = T(1) / rhs;
204 return TColor(r * invRhs, g * invRhs, b * invRhs, a * invRhs);
261 assert(rhs.
r != T(0));
262 assert(rhs.
g != T(0));
263 assert(rhs.
b != T(0));
264 assert(rhs.
a != T(0));
276 const T invRhs = T(1) / rhs;
287 constexpr bool operator==(
const TColor& rhs)
const {
return r == rhs.
r && g == rhs.
g && b == rhs.
b && a == rhs.
a; }
306 template<
typename T>
const TColor<T> TColor<T>::white{1, 1, 1, 1};
307 template<
typename T>
const TColor<T> TColor<T>::black{0, 0, 0, 1};
308 template<
typename T>
const TColor<T> TColor<T>::red{1, 0, 0, 1};
309 template<
typename T>
const TColor<T> TColor<T>::green{0, 1, 0, 1};
310 template<
typename T>
const TColor<T> TColor<T>::blue{0, 0, 1, 1};
323template<
typename T>
struct std::hash<CeresEngine::Math::TColor<T>> {
324 using Type = CeresEngine::Math::TColor<T>;
325 constexpr size_t operator()(
const Type& obj)
const {
return std::hash<CeresEngine::TVector4<T>>{}(
CeresEngine::TVector4<T>(obj)); }
328#define REFL_DECLARE_CE_COLOR(T)
#define CE_ASSERT_UNDEFINED()
Definition Macros.hpp:326
#define CE_SCRIPT_EXPORT(...)
The CE_SCRIPT_EXPORT macro marks a class or method as exportable and available in scripting environme...
Definition Macros.hpp:247
#define REFL_DECLARE_CE_COLOR(T)
Definition Color.hpp:328
constexpr TColor changeGreen(T green) const
Sets the green channel of a color to a new value.
Definition Color.hpp:43
constexpr TColor(T value, T alpha) noexcept
Definition Color.hpp:27
constexpr TColor operator/(T rhs) const
Definition Color.hpp:200
constexpr TColor(const TColor &) noexcept=default
static const TColor zero
Definition Color.hpp:293
constexpr TColor saturate(T saturation) const noexcept
Saturates the color.
Definition Color.hpp:174
constexpr bool operator==(const TColor &rhs) const
Definition Color.hpp:287
constexpr TColor operator/(const TColor &rhs) const
Definition Color.hpp:198
T b
Definition Color.hpp:23
constexpr TColor & operator*=(T rhs)
Definition Color.hpp:252
constexpr TColor & operator-=(const TColor &rhs)
Definition Color.hpp:226
TColor toRGB() const
Definition Color.hpp:81
static const TColor red
Definition Color.hpp:296
constexpr TColor operator*(const TColor &rhs) const
Definition Color.hpp:197
constexpr TColor changeRed(T red) const
Sets the red channel of a color to a new value.
Definition Color.hpp:40
constexpr TColor opacity(T amount) const noexcept
Applies transparency to the color.
Definition Color.hpp:163
T a
Definition Color.hpp:23
static TColor fromString(StringView string)
T r
Definition Color.hpp:23
constexpr TColor & operator/=(const TColor &rhs)
Definition Color.hpp:260
constexpr TColor & operator/=(T rhs)
Definition Color.hpp:274
constexpr TColor & operator=(const TColor &) noexcept=default
constexpr TColor operator*(T rhs) const
Definition Color.hpp:196
static const TColor white
Definition Color.hpp:294
static const TColor black
Definition Color.hpp:295
constexpr TColor & operator-=(T rhs)
Definition Color.hpp:235
constexpr TColor & operator*=(const TColor &rhs)
Definition Color.hpp:243
constexpr bool operator!=(const TColor &rhs) const
Definition Color.hpp:288
constexpr TColor changeAlpha(T alpha) const
Sets the alpha channel of a color to a new value.
Definition Color.hpp:49
constexpr TColor(const TColor< U > &other) noexcept
Definition Color.hpp:34
static const TColor green
Definition Color.hpp:297
T g
Definition Color.hpp:23
constexpr TColor toHSL() const
Definition Color.hpp:52
static const TColor blue
Definition Color.hpp:298
constexpr TColor & operator+=(const TColor &rhs)
Definition Color.hpp:209
constexpr TColor invert(T amount=0.0, bool applyOnAlpha=false) const noexcept
Inverts the color value.
Definition Color.hpp:150
constexpr TColor darken(T amount) const
Makes the color darker.
Definition Color.hpp:135
constexpr TColor changeBlue(T blue) const
Sets the blue channel of a color to a new value.
Definition Color.hpp:46
constexpr TColor operator+(const TColor &rhs) const
Definition Color.hpp:194
constexpr TColor & operator+=(T rhs)
Definition Color.hpp:218
constexpr TColor(T red, T green, T blue, T alpha=1.0) noexcept
Definition Color.hpp:29
constexpr TColor operator-(const TColor &rhs) const
Definition Color.hpp:195
constexpr TColor(T value=0.0) noexcept
Definition Color.hpp:26
friend constexpr TColor operator*(T lhs, const TColor &rhs)
Definition Color.hpp:207
BasicStringView< char > StringView
Narrow string view used for handling narrow encoded text in UTF-8.
Definition String.hpp:190
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
TVector< 4, T > TVector4
A four dimensional vector type that uses a internal representation of type T
Definition Vector.hpp:115