Color: Bind `from_hsv` as static method

This commit is contained in:
Rémi Verschelde 2021-11-11 09:01:39 +01:00
parent 171a69757f
commit e2e9b08cc7
No known key found for this signature in database
GPG Key ID: C3336907360768E1
4 changed files with 71 additions and 56 deletions

View File

@ -107,6 +107,39 @@ uint64_t Color::to_rgba64() const {
return c;
}
String _to_hex(float p_val) {
int v = Math::round(p_val * 255);
v = CLAMP(v, 0, 255);
String ret;
for (int i = 0; i < 2; i++) {
char32_t c[2] = { 0, 0 };
int lv = v & 0xF;
if (lv < 10) {
c[0] = '0' + lv;
} else {
c[0] = 'a' + lv - 10;
}
v >>= 4;
String cs = (const char32_t *)c;
ret = cs + ret;
}
return ret;
}
String Color::to_html(bool p_alpha) const {
String txt;
txt += _to_hex(r);
txt += _to_hex(g);
txt += _to_hex(b);
if (p_alpha) {
txt += _to_hex(a);
}
return txt;
}
float Color::get_h() const {
float min = MIN(r, g);
min = MIN(min, b);
@ -249,20 +282,6 @@ Color Color::hex64(uint64_t p_hex) {
return Color(r, g, b, a);
}
Color Color::from_rgbe9995(uint32_t p_rgbe) {
float r = p_rgbe & 0x1ff;
float g = (p_rgbe >> 9) & 0x1ff;
float b = (p_rgbe >> 18) & 0x1ff;
float e = (p_rgbe >> 27);
float m = Math::pow(2, e - 15.0 - 9.0);
float rd = r * m;
float gd = g * m;
float bd = b * m;
return Color(rd, gd, bd, 1.0f);
}
static int _parse_col4(const String &p_str, int p_ofs) {
char character = p_str[p_ofs];
@ -428,45 +447,26 @@ Color Color::from_string(const String &p_string, const Color &p_default) {
}
}
String _to_hex(float p_val) {
int v = Math::round(p_val * 255);
v = CLAMP(v, 0, 255);
String ret;
for (int i = 0; i < 2; i++) {
char32_t c[2] = { 0, 0 };
int lv = v & 0xF;
if (lv < 10) {
c[0] = '0' + lv;
} else {
c[0] = 'a' + lv - 10;
}
v >>= 4;
String cs = (const char32_t *)c;
ret = cs + ret;
}
return ret;
}
String Color::to_html(bool p_alpha) const {
String txt;
txt += _to_hex(r);
txt += _to_hex(g);
txt += _to_hex(b);
if (p_alpha) {
txt += _to_hex(a);
}
return txt;
}
Color Color::from_hsv(float p_h, float p_s, float p_v, float p_a) const {
Color Color::from_hsv(float p_h, float p_s, float p_v, float p_alpha) {
Color c;
c.set_hsv(p_h, p_s, p_v, p_a);
c.set_hsv(p_h, p_s, p_v, p_alpha);
return c;
}
Color Color::from_rgbe9995(uint32_t p_rgbe) {
float r = p_rgbe & 0x1ff;
float g = (p_rgbe >> 9) & 0x1ff;
float b = (p_rgbe >> 18) & 0x1ff;
float e = (p_rgbe >> 27);
float m = Math::pow(2, e - 15.0 - 9.0);
float rd = r * m;
float gd = g * m;
float bd = b * m;
return Color(rd, gd, bd, 1.0f);
}
Color::operator String() const {
return "(" + String::num(r, 4) + ", " + String::num(g, 4) + ", " + String::num(b, 4) + ", " + String::num(a, 4) + ")";
}

View File

@ -51,6 +51,7 @@ struct Color {
uint64_t to_rgba64() const;
uint64_t to_argb64() const;
uint64_t to_abgr64() const;
String to_html(bool p_alpha = true) const;
float get_h() const;
float get_s() const;
float get_v() const;
@ -189,8 +190,7 @@ struct Color {
static String get_named_color_name(int p_idx);
static Color get_named_color(int p_idx);
static Color from_string(const String &p_string, const Color &p_default);
String to_html(bool p_alpha = true) const;
Color from_hsv(float p_h, float p_s, float p_v, float p_a) const;
static Color from_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0);
static Color from_rgbe9995(uint32_t p_rgbe);
_FORCE_INLINE_ bool operator<(const Color &p_color) const; //used in set keys

View File

@ -1422,8 +1422,6 @@ static void _register_variant_builtin_methods() {
bind_method(String, sha1_buffer, sarray(), varray());
bind_method(String, sha256_buffer, sarray(), varray());
bind_method(String, is_empty, sarray(), varray());
// FIXME: Static function, not sure how to bind
//bind_method(String, humanize_size, sarray("size"), varray());
bind_method(String, is_absolute_path, sarray(), varray());
bind_method(String, is_relative_path, sarray(), varray());
@ -1635,17 +1633,15 @@ static void _register_variant_builtin_methods() {
bind_method(Color, to_argb64, sarray(), varray());
bind_method(Color, to_abgr64, sarray(), varray());
bind_method(Color, to_rgba64, sarray(), varray());
bind_method(Color, to_html, sarray("with_alpha"), varray(true));
bind_method(Color, clamp, sarray("min", "max"), varray(Color(0, 0, 0, 0), Color(1, 1, 1, 1)));
bind_method(Color, inverted, sarray(), varray());
bind_method(Color, lerp, sarray("to", "weight"), varray());
bind_method(Color, lightened, sarray("amount"), varray());
bind_method(Color, darkened, sarray("amount"), varray());
bind_method(Color, to_html, sarray("with_alpha"), varray(true));
bind_method(Color, blend, sarray("over"), varray());
// FIXME: Color is immutable, need to probably find a way to do this via constructor
//ADDFUNC4R(COLOR, COLOR, Color, from_hsv, FLOAT, "h", FLOAT, "s", FLOAT, "v", FLOAT, "a", varray(1.0));
bind_method(Color, is_equal_approx, sarray("to"), varray());
bind_static_method(Color, hex, sarray("hex"), varray());
@ -1657,6 +1653,7 @@ static void _register_variant_builtin_methods() {
bind_static_method(Color, get_named_color_name, sarray("idx"), varray());
bind_static_method(Color, get_named_color, sarray("idx"), varray());
bind_static_method(Color, from_string, sarray("str", "default"), varray());
bind_static_method(Color, from_hsv, sarray("h", "s", "v", "alpha"), varray(1.0));
bind_static_method(Color, from_rgbe9995, sarray("rgbe"), varray());
/* RID */

View File

@ -147,6 +147,24 @@
<description>
</description>
</method>
<method name="from_hsv" qualifiers="static">
<return type="Color" />
<argument index="0" name="h" type="float" />
<argument index="1" name="s" type="float" />
<argument index="2" name="v" type="float" />
<argument index="3" name="alpha" type="float" default="1.0" />
<description>
Constructs a color from an [url=https://en.wikipedia.org/wiki/HSL_and_HSV]HSV profile[/url]. [code]h[/code] (hue), [code]s[/code] (saturation), and [code]v[/code] (value) are typically between 0 and 1.
[codeblocks]
[gdscript]
var c = Color.from_hsv(0.58, 0.5, 0.79, 0.8)
[/gdscript]
[csharp]
var c = Color.FromHsv(0.58f, 0.5f, 0.79f, 0.8f);
[/csharp]
[/codeblocks]
</description>
</method>
<method name="from_rgbe9995" qualifiers="static">
<return type="Color" />
<argument index="0" name="rgbe" type="int" />