2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* color.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#ifndef COLOR_H
|
|
|
|
#define COLOR_H
|
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/math/math_funcs.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/string/ustring.h"
|
2019-08-10 14:28:17 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
struct Color {
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
float r;
|
|
|
|
float g;
|
|
|
|
float b;
|
|
|
|
float a;
|
2016-03-09 00:00:52 +01:00
|
|
|
};
|
2020-05-12 17:01:17 +02:00
|
|
|
float components[4] = { 0, 0, 0, 1.0 };
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2017-09-07 22:19:44 +02:00
|
|
|
uint32_t to_rgba32() const;
|
|
|
|
uint32_t to_argb32() const;
|
|
|
|
uint32_t to_abgr32() const;
|
2018-06-21 04:14:31 +02:00
|
|
|
uint64_t to_rgba64() const;
|
|
|
|
uint64_t to_argb64() const;
|
|
|
|
uint64_t to_abgr64() const;
|
2014-02-10 02:10:30 +01:00
|
|
|
float get_h() const;
|
|
|
|
float get_s() const;
|
|
|
|
float get_v() const;
|
2017-03-05 16:44:50 +01:00
|
|
|
void set_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-11-09 09:05:35 +01:00
|
|
|
_FORCE_INLINE_ float &operator[](int p_idx) {
|
|
|
|
return components[p_idx];
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-11-09 09:05:35 +01:00
|
|
|
_FORCE_INLINE_ const float &operator[](int p_idx) const {
|
|
|
|
return components[p_idx];
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-11-09 09:05:35 +01:00
|
|
|
bool operator==(const Color &p_color) const {
|
|
|
|
return (r == p_color.r && g == p_color.g && b == p_color.b && a == p_color.a);
|
|
|
|
}
|
|
|
|
bool operator!=(const Color &p_color) const {
|
|
|
|
return (r != p_color.r || g != p_color.g || b != p_color.b || a != p_color.a);
|
2019-09-20 22:58:06 +02:00
|
|
|
}
|
2017-09-11 21:35:59 +02:00
|
|
|
|
2020-11-09 09:05:35 +01:00
|
|
|
Color operator+(const Color &p_color) const;
|
|
|
|
void operator+=(const Color &p_color);
|
|
|
|
|
2017-09-11 21:35:59 +02:00
|
|
|
Color operator-() const;
|
|
|
|
Color operator-(const Color &p_color) const;
|
|
|
|
void operator-=(const Color &p_color);
|
|
|
|
|
|
|
|
Color operator*(const Color &p_color) const;
|
2020-11-09 09:05:35 +01:00
|
|
|
Color operator*(real_t p_rvalue) const;
|
2017-09-11 21:35:59 +02:00
|
|
|
void operator*=(const Color &p_color);
|
2020-11-09 09:05:35 +01:00
|
|
|
void operator*=(real_t p_rvalue);
|
2017-09-11 21:35:59 +02:00
|
|
|
|
|
|
|
Color operator/(const Color &p_color) const;
|
2020-11-09 09:05:35 +01:00
|
|
|
Color operator/(real_t p_rvalue) const;
|
2017-09-11 21:35:59 +02:00
|
|
|
void operator/=(const Color &p_color);
|
2020-11-09 09:05:35 +01:00
|
|
|
void operator/=(real_t p_rvalue);
|
2017-09-11 21:35:59 +02:00
|
|
|
|
2019-10-14 22:33:45 +02:00
|
|
|
bool is_equal_approx(const Color &p_color) const;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void invert();
|
|
|
|
Color inverted() const;
|
|
|
|
|
2020-12-07 09:16:31 +01:00
|
|
|
_FORCE_INLINE_ Color lerp(const Color &p_to, float p_weight) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
Color res = *this;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-12-07 09:16:31 +01:00
|
|
|
res.r += (p_weight * (p_to.r - r));
|
|
|
|
res.g += (p_weight * (p_to.g - g));
|
|
|
|
res.b += (p_weight * (p_to.b - b));
|
|
|
|
res.a += (p_weight * (p_to.a - a));
|
2016-03-09 00:00:52 +01:00
|
|
|
|
|
|
|
return res;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-10-13 06:49:31 +02:00
|
|
|
_FORCE_INLINE_ Color darkened(float p_amount) const {
|
|
|
|
Color res = *this;
|
2018-01-12 04:08:32 +01:00
|
|
|
res.r = res.r * (1.0f - p_amount);
|
|
|
|
res.g = res.g * (1.0f - p_amount);
|
|
|
|
res.b = res.b * (1.0f - p_amount);
|
2017-10-13 06:49:31 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Color lightened(float p_amount) const {
|
|
|
|
Color res = *this;
|
2018-01-12 04:08:32 +01:00
|
|
|
res.r = res.r + (1.0f - res.r) * p_amount;
|
|
|
|
res.g = res.g + (1.0f - res.g) * p_amount;
|
|
|
|
res.b = res.b + (1.0f - res.b) * p_amount;
|
2017-10-13 06:49:31 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
_FORCE_INLINE_ uint32_t to_rgbe9995() const {
|
|
|
|
const float pow2to9 = 512.0f;
|
|
|
|
const float B = 15.0f;
|
|
|
|
const float N = 9.0f;
|
|
|
|
|
2020-11-09 09:05:35 +01:00
|
|
|
float sharedexp = 65408.000f; // Result of: ((pow2to9 - 1.0f) / pow2to9) * powf(2.0f, 31.0f - 15.0f)
|
2017-05-29 02:46:48 +02:00
|
|
|
|
|
|
|
float cRed = MAX(0.0f, MIN(sharedexp, r));
|
|
|
|
float cGreen = MAX(0.0f, MIN(sharedexp, g));
|
|
|
|
float cBlue = MAX(0.0f, MIN(sharedexp, b));
|
|
|
|
|
|
|
|
float cMax = MAX(cRed, MAX(cGreen, cBlue));
|
|
|
|
|
|
|
|
float expp = MAX(-B - 1.0f, floor(Math::log(cMax) / Math_LN2)) + 1.0f + B;
|
|
|
|
|
|
|
|
float sMax = (float)floor((cMax / Math::pow(2.0f, expp - B - N)) + 0.5f);
|
|
|
|
|
|
|
|
float exps = expp + 1.0f;
|
|
|
|
|
|
|
|
if (0.0 <= sMax && sMax < pow2to9) {
|
|
|
|
exps = expp;
|
|
|
|
}
|
|
|
|
|
|
|
|
float sRed = Math::floor((cRed / pow(2.0f, exps - B - N)) + 0.5f);
|
|
|
|
float sGreen = Math::floor((cGreen / pow(2.0f, exps - B - N)) + 0.5f);
|
|
|
|
float sBlue = Math::floor((cBlue / pow(2.0f, exps - B - N)) + 0.5f);
|
|
|
|
|
|
|
|
return (uint32_t(Math::fast_ftoi(sRed)) & 0x1FF) | ((uint32_t(Math::fast_ftoi(sGreen)) & 0x1FF) << 9) | ((uint32_t(Math::fast_ftoi(sBlue)) & 0x1FF) << 18) | ((uint32_t(Math::fast_ftoi(exps)) & 0x1F) << 27);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ Color blend(const Color &p_over) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
Color res;
|
|
|
|
float sa = 1.0 - p_over.a;
|
2017-03-05 16:44:50 +01:00
|
|
|
res.a = a * sa + p_over.a;
|
|
|
|
if (res.a == 0) {
|
|
|
|
return Color(0, 0, 0, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
res.r = (r * a * sa + p_over.r * p_over.a) / res.a;
|
|
|
|
res.g = (g * a * sa + p_over.g * p_over.a) / res.a;
|
|
|
|
res.b = (b * a * sa + p_over.b * p_over.a) / res.a;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-06-16 15:22:26 +02:00
|
|
|
_FORCE_INLINE_ Color to_linear() const {
|
|
|
|
return Color(
|
2017-03-05 16:44:50 +01:00
|
|
|
r < 0.04045 ? r * (1.0 / 12.92) : Math::pow((r + 0.055) * (1.0 / (1 + 0.055)), 2.4),
|
|
|
|
g < 0.04045 ? g * (1.0 / 12.92) : Math::pow((g + 0.055) * (1.0 / (1 + 0.055)), 2.4),
|
|
|
|
b < 0.04045 ? b * (1.0 / 12.92) : Math::pow((b + 0.055) * (1.0 / (1 + 0.055)), 2.4),
|
|
|
|
a);
|
2014-06-16 15:22:26 +02:00
|
|
|
}
|
2017-08-02 20:34:55 +02:00
|
|
|
_FORCE_INLINE_ Color to_srgb() const {
|
|
|
|
return Color(
|
|
|
|
r < 0.0031308 ? 12.92 * r : (1.0 + 0.055) * Math::pow(r, 1.0f / 2.4f) - 0.055,
|
|
|
|
g < 0.0031308 ? 12.92 * g : (1.0 + 0.055) * Math::pow(g, 1.0f / 2.4f) - 0.055,
|
|
|
|
b < 0.0031308 ? 12.92 * b : (1.0 + 0.055) * Math::pow(b, 1.0f / 2.4f) - 0.055, a);
|
|
|
|
}
|
|
|
|
|
2017-08-30 20:30:12 +02:00
|
|
|
static Color hex(uint32_t p_hex);
|
2018-06-21 04:14:31 +02:00
|
|
|
static Color hex64(uint64_t p_hex);
|
2020-09-01 08:03:30 +02:00
|
|
|
static Color html(const String &p_rgba);
|
2017-03-05 16:44:50 +01:00
|
|
|
static bool html_is_valid(const String &p_color);
|
|
|
|
static Color named(const String &p_name);
|
2020-11-07 23:33:38 +01:00
|
|
|
static int get_named_color_count();
|
|
|
|
static String get_named_color_name(int p_idx);
|
|
|
|
static Color get_named_color(int p_idx);
|
2017-03-05 16:44:50 +01:00
|
|
|
String to_html(bool p_alpha = true) const;
|
2018-10-25 16:13:51 +02:00
|
|
|
Color from_hsv(float p_h, float p_s, float p_v, float p_a) const;
|
2019-06-26 15:08:25 +02:00
|
|
|
static Color from_rgbe9995(uint32_t p_rgbe);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ bool operator<(const Color &p_color) const; //used in set keys
|
2014-02-10 02:10:30 +01:00
|
|
|
operator String() const;
|
|
|
|
|
2020-11-09 09:05:35 +01:00
|
|
|
// For the binder.
|
2020-11-07 02:29:22 +01:00
|
|
|
_FORCE_INLINE_ void set_r8(int32_t r8) { r = (CLAMP(r8, 0, 255) / 255.0); }
|
2020-11-09 09:05:35 +01:00
|
|
|
_FORCE_INLINE_ int32_t get_r8() const { return int32_t(CLAMP(r * 255.0, 0.0, 255.0)); }
|
2020-11-07 02:29:22 +01:00
|
|
|
_FORCE_INLINE_ void set_g8(int32_t g8) { g = (CLAMP(g8, 0, 255) / 255.0); }
|
2020-11-09 09:05:35 +01:00
|
|
|
_FORCE_INLINE_ int32_t get_g8() const { return int32_t(CLAMP(g * 255.0, 0.0, 255.0)); }
|
2020-11-07 02:29:22 +01:00
|
|
|
_FORCE_INLINE_ void set_b8(int32_t b8) { b = (CLAMP(b8, 0, 255) / 255.0); }
|
2020-11-09 09:05:35 +01:00
|
|
|
_FORCE_INLINE_ int32_t get_b8() const { return int32_t(CLAMP(b * 255.0, 0.0, 255.0)); }
|
2020-11-07 02:29:22 +01:00
|
|
|
_FORCE_INLINE_ void set_a8(int32_t a8) { a = (CLAMP(a8, 0, 255) / 255.0); }
|
2020-11-09 09:05:35 +01:00
|
|
|
_FORCE_INLINE_ int32_t get_a8() const { return int32_t(CLAMP(a * 255.0, 0.0, 255.0)); }
|
2020-11-07 02:29:22 +01:00
|
|
|
|
2020-11-09 09:05:35 +01:00
|
|
|
_FORCE_INLINE_ void set_h(float p_h) { set_hsv(p_h, get_s(), get_v()); }
|
|
|
|
_FORCE_INLINE_ void set_s(float p_s) { set_hsv(get_h(), p_s, get_v()); }
|
|
|
|
_FORCE_INLINE_ void set_v(float p_v) { set_hsv(get_h(), get_s(), p_v); }
|
2020-11-07 02:29:22 +01:00
|
|
|
|
2020-05-12 17:01:17 +02:00
|
|
|
_FORCE_INLINE_ Color() {}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/**
|
2020-11-09 17:46:03 +01:00
|
|
|
* RGBA construct parameters.
|
|
|
|
* Alpha is not optional as otherwise we can't bind the RGB version for scripting.
|
2014-02-10 02:10:30 +01:00
|
|
|
*/
|
2020-11-09 17:46:03 +01:00
|
|
|
_FORCE_INLINE_ Color(float p_r, float p_g, float p_b, float p_a) {
|
2017-03-05 16:44:50 +01:00
|
|
|
r = p_r;
|
|
|
|
g = p_g;
|
|
|
|
b = p_b;
|
|
|
|
a = p_a;
|
|
|
|
}
|
2019-07-19 02:15:39 +02:00
|
|
|
|
2020-11-09 17:46:03 +01:00
|
|
|
/**
|
|
|
|
* RGB construct parameters.
|
|
|
|
*/
|
|
|
|
_FORCE_INLINE_ Color(float p_r, float p_g, float p_b) {
|
|
|
|
r = p_r;
|
|
|
|
g = p_g;
|
|
|
|
b = p_b;
|
|
|
|
a = 1.0;
|
|
|
|
}
|
|
|
|
|
2019-07-19 02:15:39 +02:00
|
|
|
/**
|
|
|
|
* Construct a Color from another Color, but with the specified alpha value.
|
|
|
|
*/
|
|
|
|
_FORCE_INLINE_ Color(const Color &p_c, float p_a) {
|
|
|
|
r = p_c.r;
|
|
|
|
g = p_c.g;
|
|
|
|
b = p_c.b;
|
|
|
|
a = p_a;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool Color::operator<(const Color &p_color) const {
|
|
|
|
if (r == p_color.r) {
|
|
|
|
if (g == p_color.g) {
|
|
|
|
if (b == p_color.b) {
|
|
|
|
return (a < p_color.a);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
return (b < p_color.b);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
return g < p_color.g;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
return r < p_color.r;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-11-05 03:01:55 +01:00
|
|
|
_FORCE_INLINE_ Color operator*(real_t p_real, const Color &p_color) {
|
2020-09-17 13:56:36 +02:00
|
|
|
return p_color * p_real;
|
|
|
|
}
|
|
|
|
|
2020-03-25 11:10:34 +01:00
|
|
|
#endif // COLOR_H
|