Skip to content

Commit

Permalink
Add support for HTML style hex colors, #ff0000 = red, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
twanvl committed May 12, 2020
1 parent dbb6d34 commit 6d4d973
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Bug fixes:

Template features:
* 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:
* Added type_name function
Expand Down
8 changes: 6 additions & 2 deletions doc/type/color.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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>
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>
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--
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 scripts named colors are represented as [[type:string]]s.
Expand Down
31 changes: 28 additions & 3 deletions src/gfx/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,40 @@ template <> void Writer::handle(const 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) {
UInt r,g,b,a;
if (wxSscanf(v.c_str(),_("rgb(%u,%u,%u)"),&r,&g,&b)) {
return Color(r, g, b);
} else if (wxSscanf(v.c_str(),_("rgba(%u,%u,%u,%u)"),&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")) {
return Color(0,0,0,0);
} else {
Expand All @@ -41,16 +68,14 @@ optional<Color> parse_color(const String& v) {
if (c.Ok()) {
return Color(c);
} else {
return optional<Color>();
return nullopt;
}
}
}

String format_color(Color col) {
if (col.Alpha() == 255) {
return String::Format(_("rgb(%u,%u,%u)"), col.Red(), col.Green(), col.Blue());
} else if (col.Alpha() == 0) {
return _("transparent");
} else {
return String::Format(_("rgba(%u,%u,%u,%u)"), col.Red(), col.Green(), col.Blue(), col.Alpha());
}
Expand Down

0 comments on commit 6d4d973

Please sign in to comment.