aboutsummaryrefslogtreecommitdiff
path: root/core/color.cpp
diff options
context:
space:
mode:
authorBojidar Marinov2016-11-10 23:06:00 +0200
committerRémi Verschelde2017-01-12 19:15:27 +0100
commit85585c7fc5ef314d42fcc7b28583b22aaff424be (patch)
tree85b10fddb5c8b6af2a39ba8e4fc8ff3c00e25377 /core/color.cpp
parentc5bff5073e04a6ee1dea0a965185b2b2a1c80036 (diff)
downloadgodot-85585c7fc5ef314d42fcc7b28583b22aaff424be.tar.gz
godot-85585c7fc5ef314d42fcc7b28583b22aaff424be.tar.zst
godot-85585c7fc5ef314d42fcc7b28583b22aaff424be.zip
Add named colors to GDScript/core.
Names and values taken from https://en.wikipedia.org/wiki/X11_color_names (cherry picked from commit 23381a530bb4a9c8e8c3e883a7d588bf832cd277)
Diffstat (limited to 'core/color.cpp')
-rw-r--r--core/color.cpp22
1 files changed, 21 insertions, 1 deletions
diff --git a/core/color.cpp b/core/color.cpp
index bf9c94462..532c1bd1c 100644
--- a/core/color.cpp
+++ b/core/color.cpp
@@ -29,6 +29,8 @@
#include "color.h"
#include "math_funcs.h"
#include "print_string.h"
+#include "map.h"
+#include "color_names.inc"
uint32_t Color::to_ARGB32() const {
@@ -327,7 +329,25 @@ bool Color::html_is_valid(const String& p_color) {
}
-
+Color Color::named(const String &p_name) {
+ if (_named_colors.empty()) _populate_named_colors(); // from color_names.inc
+ String name = p_name;
+ // Normalize name
+ name = name.replace(" ", "");
+ name = name.replace("-", "");
+ name = name.replace("_", "");
+ name = name.replace("'", "");
+ name = name.replace(".", "");
+ name = name.to_lower();
+
+ const Map<String, Color>::Element* color = _named_colors.find(name);
+ if(color) {
+ return color->value();
+ } else {
+ ERR_EXPLAIN("Invalid Color Name: "+p_name);
+ ERR_FAIL_V(Color());
+ }
+}
String _to_hex(float p_val) {