Skip to content

Commit 6d4d973

Browse files
committed
Add support for HTML style hex colors, #ff0000 = red, etc.
1 parent dbb6d34 commit 6d4d973

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Bug fixes:
1919

2020
Template features:
2121
* Added <font:...> tag to change the font inside a text field.
22+
* Colors can now be written using hex notation, #rrggbb / #rrggbbaa, and short hex notation (#rgb / #rgba)
2223

2324
Scripting:
2425
* Added type_name function

doc/type/color.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ In files and scritps a color can be represented as
55
<pre><span class='hl-kw'>rgb</span>(<i>red_component</i>, <i>green_component</i>, <i>blue_component</i>)</pre>
66
where red_component, green_component and blue_component are numbers between 0 and 255 (inclusive).
77

8-
In some places MSE also supports colors with a transparency value, notated as
8+
In most places MSE also supports colors with a transparency value, notated as
99
<pre><span class='hl-kw'>rgba</span>(<i>red_component</i>, <i>green_component</i>, <i>blue_component</i>, <i>alpha_component</i>)</pre>
1010
An alpha value of @0@ indicates a transparent color, an alpha value of @255@ is completely opaque.
1111

12+
You can also use HTML style hexadecimal colors,
13+
<pre>#<i>rgb</i>, #<i>rgba</i>, #<i>rrggbb</i>, #<i>rrggbbaa</i></pre>
14+
For example, <tt>#ff0000</tt> is red, as is <tt>#f00</tt>
15+
1216
--Named colors--
1317
MSE also supports named colors, for instance @"white"@ is the same as @rgb(255,255,255)@.
14-
For a full list of supported colors, see [[http://www.wxwidgets.org/manuals/stable/wx_wxcolourdatabase.html|the wxWidgets documentation]].
18+
For a full list of supported colors, see [[https://docs.wxwidgets.org/3.0/classwx_colour_database.html|the wxWidgets documentation]].
1519
In addition, the named color @"transparent"@ stands for the completely transparent color, @rgba(0,0,0,0)@.
1620

1721
In scripts named colors are represented as [[type:string]]s.

src/gfx/color.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,40 @@ template <> void Writer::handle(const Color& col) {
2626
handle(format_color(col));
2727
}
2828

29+
int parse_hex(Char c) {
30+
if (c >= '0' && c <= '9') return c - '0';
31+
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
32+
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
33+
return -1;
34+
}
35+
int parse_hex(Char c1, Char c2) {
36+
int x1 = parse_hex(c1);
37+
int x2 = parse_hex(c2);
38+
if (x1 >= 0 && x2 >= 0) return 16*x1 + x2;
39+
return -1;
40+
}
2941

3042
optional<Color> parse_color(const String& v) {
3143
UInt r,g,b,a;
3244
if (wxSscanf(v.c_str(),_("rgb(%u,%u,%u)"),&r,&g,&b)) {
3345
return Color(r, g, b);
3446
} else if (wxSscanf(v.c_str(),_("rgba(%u,%u,%u,%u)"),&r,&g,&b,&a)) {
3547
return Color(r, g, b, a);
48+
} else if (v.size() > 0 && v[0] == '#') {
49+
if (v.size() == 4) {
50+
int r = parse_hex(v[1]), g = parse_hex(v[2]), b = parse_hex(v[3]);
51+
if (r >= 0 && g >= 0 && b >= 0) return Color(17 * r, 17 * g, 17 * b);
52+
} else if (v.size() == 5) {
53+
int r = parse_hex(v[1]), g = parse_hex(v[2]), b = parse_hex(v[3]), a = parse_hex(v[4]);
54+
if (r >= 0 && g >= 0 && b >= 0 && a >= 0) return Color(17 * r, 17 * g, 17 * b, 17 * a);
55+
} else if (v.size() == 7) {
56+
int r = parse_hex(v[1], v[2]), g = parse_hex(v[3],v[4]), b = parse_hex(v[5],v[6]);
57+
if (r >= 0 && g >= 0 && b >= 0) return Color(r, g, b);
58+
} else if (v.size() == 9) {
59+
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]);
60+
if (r >= 0 && g >= 0 && b >= 0 && a >= 0) return Color(r, g, b, a);
61+
}
62+
return nullopt;
3663
} else if (v == _("transparent")) {
3764
return Color(0,0,0,0);
3865
} else {
@@ -41,16 +68,14 @@ optional<Color> parse_color(const String& v) {
4168
if (c.Ok()) {
4269
return Color(c);
4370
} else {
44-
return optional<Color>();
71+
return nullopt;
4572
}
4673
}
4774
}
4875

4976
String format_color(Color col) {
5077
if (col.Alpha() == 255) {
5178
return String::Format(_("rgb(%u,%u,%u)"), col.Red(), col.Green(), col.Blue());
52-
} else if (col.Alpha() == 0) {
53-
return _("transparent");
5479
} else {
5580
return String::Format(_("rgba(%u,%u,%u,%u)"), col.Red(), col.Green(), col.Blue(), col.Alpha());
5681
}

0 commit comments

Comments
 (0)