Skip to content

Commit b7e7e88

Browse files
committed
rendlay.cpp: Add support in the layout color component to specify colors by hex rgb(a) values.
Also uses the Ensoniq VFX family of layouts to show it working, as well as documenting it.
1 parent 66deabe commit b7e7e88

File tree

6 files changed

+2330
-2275
lines changed

6 files changed

+2330
-2275
lines changed

docs/source/techspecs/layout_files.rst

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ intensity). Alpha ranges from 0.0 (fully transparent) to 1.0 (opaque). Colour
118118
channel values are not pre-multiplied by the alpha value.
119119

120120
Component and view item colour is specified using ``color`` elements.
121-
Meaningful attributes are ``red``, ``green``, ``blue`` and ``alpha``. This
122-
example ``color`` element specifies all channel values:
121+
Meaningful attributes are ``red``, ``green``, ``blue`` and ``alpha`` and ``hex``. This
122+
example ``color`` element specifies all channel values individually:
123123

124124
.. code-block:: XML
125125
@@ -129,6 +129,20 @@ Any omitted channel attributes default to 1.0 (full intensity or opaque). It
129129
is an error if any channel value falls outside the range of 0.0 to 1.0
130130
(inclusive).
131131

132+
Alternatively, you can specify a single hex value, for either RGB (six hex digits)
133+
or RGBA (eight hex digits):
134+
135+
.. code-block:: XML
136+
137+
<color hex="d9664c" />
138+
139+
(RGB) or
140+
141+
.. code-block:: XML
142+
143+
<color hex="d9664cff" />
144+
145+
(RGBA).
132146

133147
.. _layfile-concepts-params:
134148

src/emu/rendlay.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,39 @@ class layout_environment
887887
if (!node)
888888
return render_color{ 1.0F, 1.0F, 1.0F, 1.0F };
889889

890+
if (node->has_attribute("hex"))
891+
{
892+
// Use the single hex attribute
893+
if (node->has_attribute("red") || node->has_attribute("green") || node->has_attribute("blue") || node->has_attribute("alpha"))
894+
throw layout_syntax_error(util::string_format("specify either hex or (red,green,blue) colors"));
895+
896+
const std::string hex{node->get_attribute_string("hex", "")};
897+
auto l = hex.length();
898+
if (l == 6 || l == 8)
899+
{
900+
std::size_t pos;
901+
unsigned long val = std::stoul(hex, &pos, 16);
902+
if (pos != l)
903+
throw layout_syntax_error(util::string_format("illegal hex color %s", hex));
904+
905+
float alpha = 1.0;
906+
size_t offset = 0;
907+
if (l == 8)
908+
{
909+
alpha = BIT(val, 0, 8) / 255.0;
910+
offset = 8;
911+
}
912+
float blue = BIT(val, offset + 0, 8) / 255.0;
913+
float green = BIT(val, offset + 8, 8) / 255.0;
914+
float red = BIT(val, offset + 16, 8) / 255.0;
915+
return render_color { alpha, red, green, blue };
916+
}
917+
else
918+
{
919+
throw layout_syntax_error(util::string_format("illegal hex color %s", hex));
920+
}
921+
}
922+
890923
// parse attributes
891924
render_color const result{
892925
get_attribute_float(*node, "alpha", 1.0F),

0 commit comments

Comments
 (0)