Add support for HTML style hex colors, #ff0000 = red, etc.

This commit is contained in:
Twan van Laarhoven
2020-05-12 22:48:56 +02:00
parent dbb6d34bb3
commit 6d4d973645
3 changed files with 35 additions and 5 deletions
+1
View File
@@ -19,6 +19,7 @@ Bug fixes:
Template features: Template features:
* Added <font:...> tag to change the font inside a text field. * Added <font:...> tag to change the font inside a text field.
* Colors can now be written using hex notation, #rrggbb / #rrggbbaa, and short hex notation (#rgb / #rgba)
Scripting: Scripting:
* Added type_name function * Added type_name function
+6 -2
View File
@@ -5,13 +5,17 @@ In files and scritps a color can be represented as
<pre><span class='hl-kw'>rgb</span>(<i>red_component</i>, <i>green_component</i>, <i>blue_component</i>)</pre> <pre><span class='hl-kw'>rgb</span>(<i>red_component</i>, <i>green_component</i>, <i>blue_component</i>)</pre>
where red_component, green_component and blue_component are numbers between 0 and 255 (inclusive). where red_component, green_component and blue_component are numbers between 0 and 255 (inclusive).
In some places MSE also supports colors with a transparency value, notated as In most places MSE also supports colors with a transparency value, notated as
<pre><span class='hl-kw'>rgba</span>(<i>red_component</i>, <i>green_component</i>, <i>blue_component</i>, <i>alpha_component</i>)</pre> <pre><span class='hl-kw'>rgba</span>(<i>red_component</i>, <i>green_component</i>, <i>blue_component</i>, <i>alpha_component</i>)</pre>
An alpha value of @0@ indicates a transparent color, an alpha value of @255@ is completely opaque. An alpha value of @0@ indicates a transparent color, an alpha value of @255@ is completely opaque.
You can also use HTML style hexadecimal colors,
<pre>#<i>rgb</i>, #<i>rgba</i>, #<i>rrggbb</i>, #<i>rrggbbaa</i></pre>
For example, <tt>#ff0000</tt> is red, as is <tt>#f00</tt>
--Named colors-- --Named colors--
MSE also supports named colors, for instance @"white"@ is the same as @rgb(255,255,255)@. MSE also supports named colors, for instance @"white"@ is the same as @rgb(255,255,255)@.
For a full list of supported colors, see [[http://www.wxwidgets.org/manuals/stable/wx_wxcolourdatabase.html|the wxWidgets documentation]]. For a full list of supported colors, see [[https://docs.wxwidgets.org/3.0/classwx_colour_database.html|the wxWidgets documentation]].
In addition, the named color @"transparent"@ stands for the completely transparent color, @rgba(0,0,0,0)@. In addition, the named color @"transparent"@ stands for the completely transparent color, @rgba(0,0,0,0)@.
In scripts named colors are represented as [[type:string]]s. In scripts named colors are represented as [[type:string]]s.
+28 -3
View File
@@ -26,6 +26,18 @@ template <> void Writer::handle(const Color& col) {
handle(format_color(col)); handle(format_color(col));
} }
int parse_hex(Char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
return -1;
}
int parse_hex(Char c1, Char c2) {
int x1 = parse_hex(c1);
int x2 = parse_hex(c2);
if (x1 >= 0 && x2 >= 0) return 16*x1 + x2;
return -1;
}
optional<Color> parse_color(const String& v) { optional<Color> parse_color(const String& v) {
UInt r,g,b,a; UInt r,g,b,a;
@@ -33,6 +45,21 @@ optional<Color> parse_color(const String& v) {
return Color(r, g, b); return Color(r, g, b);
} else if (wxSscanf(v.c_str(),_("rgba(%u,%u,%u,%u)"),&r,&g,&b,&a)) { } else if (wxSscanf(v.c_str(),_("rgba(%u,%u,%u,%u)"),&r,&g,&b,&a)) {
return Color(r, g, b, a); return Color(r, g, b, a);
} else if (v.size() > 0 && v[0] == '#') {
if (v.size() == 4) {
int r = parse_hex(v[1]), g = parse_hex(v[2]), b = parse_hex(v[3]);
if (r >= 0 && g >= 0 && b >= 0) return Color(17 * r, 17 * g, 17 * b);
} else if (v.size() == 5) {
int r = parse_hex(v[1]), g = parse_hex(v[2]), b = parse_hex(v[3]), a = parse_hex(v[4]);
if (r >= 0 && g >= 0 && b >= 0 && a >= 0) return Color(17 * r, 17 * g, 17 * b, 17 * a);
} else if (v.size() == 7) {
int r = parse_hex(v[1], v[2]), g = parse_hex(v[3],v[4]), b = parse_hex(v[5],v[6]);
if (r >= 0 && g >= 0 && b >= 0) return Color(r, g, b);
} else if (v.size() == 9) {
int r = parse_hex(v[1], v[2]), g = parse_hex(v[3],v[4]), b = parse_hex(v[5],v[6]), a = parse_hex(v[7],v[8]);
if (r >= 0 && g >= 0 && b >= 0 && a >= 0) return Color(r, g, b, a);
}
return nullopt;
} else if (v == _("transparent")) { } else if (v == _("transparent")) {
return Color(0,0,0,0); return Color(0,0,0,0);
} else { } else {
@@ -41,7 +68,7 @@ optional<Color> parse_color(const String& v) {
if (c.Ok()) { if (c.Ok()) {
return Color(c); return Color(c);
} else { } else {
return optional<Color>(); return nullopt;
} }
} }
} }
@@ -49,8 +76,6 @@ optional<Color> parse_color(const String& v) {
String format_color(Color col) { String format_color(Color col) {
if (col.Alpha() == 255) { if (col.Alpha() == 255) {
return String::Format(_("rgb(%u,%u,%u)"), col.Red(), col.Green(), col.Blue()); return String::Format(_("rgb(%u,%u,%u)"), col.Red(), col.Green(), col.Blue());
} else if (col.Alpha() == 0) {
return _("transparent");
} else { } else {
return String::Format(_("rgba(%u,%u,%u,%u)"), col.Red(), col.Green(), col.Blue(), col.Alpha()); return String::Format(_("rgba(%u,%u,%u,%u)"), col.Red(), col.Green(), col.Blue(), col.Alpha());
} }