summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorBernhard Liebl <Bernhard.Liebl@gmx.org>2018-01-18 22:03:34 +0100
committerBernhard Liebl <Bernhard.Liebl@gmx.org>2018-01-18 22:13:00 +0100
commitbe5517123101fe669ebfc3d3f16a0431ca2804dd (patch)
tree6539df7c4a91d1629a90ac2442a50f0eb07f3d66 /core
parentd0d511b0f8d9cb4d48c861b2c097fa15876f432c (diff)
downloadredot-engine-be5517123101fe669ebfc3d3f16a0431ca2804dd.tar.gz
Add Color.from_hsv()
Diffstat (limited to 'core')
-rw-r--r--core/color.cpp49
-rw-r--r--core/color.h1
-rw-r--r--core/variant_call.cpp2
3 files changed, 52 insertions, 0 deletions
diff --git a/core/color.cpp b/core/color.cpp
index a0568d26ed..27d8e9b891 100644
--- a/core/color.cpp
+++ b/core/color.cpp
@@ -400,6 +400,55 @@ String Color::to_html(bool p_alpha) const {
return txt;
}
+Color Color::from_hsv(float p_h, float p_s, float p_v, float p_a) {
+
+ const float h_ = p_h / 60.0f;
+ const float c = p_v * p_s;
+ const float x = c * (1.0f - Math::abs(Math::fmod(h_, 2.0f) - 1.0f));
+ float r, g, b;
+
+ switch ((int)h_) {
+ case 0: {
+ r = c;
+ g = x;
+ b = 0;
+ } break;
+ case 1: {
+ r = x;
+ g = c;
+ b = 0;
+ } break;
+ case 2: {
+ r = 0;
+ g = c;
+ b = x;
+ } break;
+ case 3: {
+ r = 0;
+ g = x;
+ b = c;
+ } break;
+ case 4: {
+ r = x;
+ g = 0;
+ b = c;
+ } break;
+ case 5: {
+ r = c;
+ g = 0;
+ b = x;
+ } break;
+ default: {
+ r = 0;
+ g = 0;
+ b = 0;
+ } break;
+ }
+
+ const float m = p_v - c;
+ return Color(m + r, m + g, m + b, p_a);
+}
+
float Color::gray() const {
return (r + g + b) / 3.0;
diff --git a/core/color.h b/core/color.h
index 9af8fb0a78..a2015a34d6 100644
--- a/core/color.h
+++ b/core/color.h
@@ -190,6 +190,7 @@ struct Color {
static bool html_is_valid(const String &p_color);
static Color named(const String &p_name);
String to_html(bool p_alpha = true) const;
+ Color from_hsv(float p_h, float p_s, float p_v, float p_a);
_FORCE_INLINE_ bool operator<(const Color &p_color) const; //used in set keys
operator String() const;
diff --git a/core/variant_call.cpp b/core/variant_call.cpp
index e9f7af3f63..d963124717 100644
--- a/core/variant_call.cpp
+++ b/core/variant_call.cpp
@@ -443,6 +443,7 @@ struct _VariantCall {
VCALL_LOCALMEM1R(Color, lightened);
VCALL_LOCALMEM1R(Color, darkened);
VCALL_LOCALMEM1R(Color, to_html);
+ VCALL_LOCALMEM4R(Color, from_hsv);
VCALL_LOCALMEM0R(RID, get_id);
@@ -1589,6 +1590,7 @@ void register_variant_methods() {
ADDFUNC1R(COLOR, COLOR, Color, lightened, REAL, "amount", varray());
ADDFUNC1R(COLOR, COLOR, Color, darkened, REAL, "amount", varray());
ADDFUNC1R(COLOR, STRING, Color, to_html, BOOL, "with_alpha", varray(true));
+ ADDFUNC4R(COLOR, COLOR, Color, from_hsv, REAL, "h", REAL, "s", REAL, "v", REAL, "a", varray(1.0));
ADDFUNC0R(_RID, INT, RID, get_id, varray());