PropertyParserColour.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "PropertyParserColour.h"
  29. #include "ControlledLifetimeResource.h"
  30. #include <algorithm>
  31. #include <cmath>
  32. #include <string.h>
  33. namespace Rml {
  34. // Helper function for hsl->rgb conversion.
  35. static float HSL_f(float h, float s, float l, float n)
  36. {
  37. float k = std::fmod((n + h * (1.0f / 30.0f)), 12.0f);
  38. float a = s * std::min(l, 1.0f - l);
  39. return l - a * std::max(-1.0f, std::min({k - 3.0f, 9.0f - k, 1.0f}));
  40. }
  41. // Ref: https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative
  42. static void HSLAToRGBA(Array<float, 4>& vals)
  43. {
  44. if (vals[1] == 0.0f)
  45. {
  46. vals[0] = vals[1] = vals[2];
  47. }
  48. else
  49. {
  50. float h = std::fmod(vals[0], 360.0f);
  51. if (h < 0)
  52. h += 360.0f;
  53. float s = vals[1];
  54. float l = vals[2];
  55. vals[0] = HSL_f(h, s, l, 0.0f);
  56. vals[1] = HSL_f(h, s, l, 8.0f);
  57. vals[2] = HSL_f(h, s, l, 4.0f);
  58. }
  59. }
  60. struct PropertyParserColourData {
  61. const UnorderedMap<String, Colourb> html_colours = {
  62. {"black", Colourb(0, 0, 0)},
  63. {"silver", Colourb(192, 192, 192)},
  64. {"gray", Colourb(128, 128, 128)},
  65. {"grey", Colourb(128, 128, 128)},
  66. {"white", Colourb(255, 255, 255)},
  67. {"maroon", Colourb(128, 0, 0)},
  68. {"red", Colourb(255, 0, 0)},
  69. {"orange", Colourb(255, 165, 0)},
  70. {"purple", Colourb(128, 0, 128)},
  71. {"fuchsia", Colourb(255, 0, 255)},
  72. {"green", Colourb(0, 128, 0)},
  73. {"lime", Colourb(0, 255, 0)},
  74. {"olive", Colourb(128, 128, 0)},
  75. {"yellow", Colourb(255, 255, 0)},
  76. {"navy", Colourb(0, 0, 128)},
  77. {"blue", Colourb(0, 0, 255)},
  78. {"teal", Colourb(0, 128, 128)},
  79. {"aqua", Colourb(0, 255, 255)},
  80. {"transparent", Colourb(0, 0, 0, 0)},
  81. };
  82. };
  83. ControlledLifetimeResource<PropertyParserColourData> PropertyParserColour::parser_data;
  84. void PropertyParserColour::Initialize()
  85. {
  86. parser_data.Initialize();
  87. }
  88. void PropertyParserColour::Shutdown()
  89. {
  90. parser_data.Shutdown();
  91. }
  92. PropertyParserColour::PropertyParserColour() {}
  93. PropertyParserColour::~PropertyParserColour() {}
  94. bool PropertyParserColour::ParseValue(Property& property, const String& value, const ParameterMap& /*parameters*/) const
  95. {
  96. Colourb colour;
  97. if (!ParseColour(colour, value))
  98. return false;
  99. property.value = Variant(colour);
  100. property.unit = Unit::COLOUR;
  101. return true;
  102. }
  103. bool PropertyParserColour::ParseColour(Colourb& colour, const String& value)
  104. {
  105. if (value.empty())
  106. return false;
  107. colour = {};
  108. // Check for a hex colour.
  109. if (value[0] == '#')
  110. {
  111. char hex_values[4][2] = {{'f', 'f'}, {'f', 'f'}, {'f', 'f'}, {'f', 'f'}};
  112. switch (value.size())
  113. {
  114. // Single hex digit per channel, RGB and alpha.
  115. case 5:
  116. hex_values[3][0] = hex_values[3][1] = value[4];
  117. //-fallthrough
  118. // Single hex digit per channel, RGB only.
  119. case 4:
  120. hex_values[0][0] = hex_values[0][1] = value[1];
  121. hex_values[1][0] = hex_values[1][1] = value[2];
  122. hex_values[2][0] = hex_values[2][1] = value[3];
  123. break;
  124. // Two hex digits per channel, RGB and alpha.
  125. case 9:
  126. hex_values[3][0] = value[7];
  127. hex_values[3][1] = value[8];
  128. //-fallthrough
  129. // Two hex digits per channel, RGB only.
  130. case 7: memcpy(hex_values, &value.c_str()[1], sizeof(char) * 6); break;
  131. default: return false;
  132. }
  133. // Parse each of the colour elements.
  134. for (int i = 0; i < 4; i++)
  135. {
  136. int tens = Math::HexToDecimal(hex_values[i][0]);
  137. int ones = Math::HexToDecimal(hex_values[i][1]);
  138. if (tens == -1 || ones == -1)
  139. return false;
  140. colour[i] = (byte)(tens * 16 + ones);
  141. }
  142. }
  143. else if (value.substr(0, 3) == "rgb" || value.substr(0, 3) == "hsl")
  144. {
  145. StringList values;
  146. values.reserve(4);
  147. size_t find = value.find('(');
  148. if (find == String::npos)
  149. return false;
  150. size_t begin_values = find + 1;
  151. StringUtilities::ExpandString(values, value.substr(begin_values, value.rfind(')') - begin_values), ',');
  152. if (value.substr(0, 3) == "rgb")
  153. {
  154. // Check if we're parsing an 'rgba' or 'rgb' colour declaration.
  155. if (value.size() > 3 && value[3] == 'a')
  156. {
  157. if (values.size() != 4)
  158. return false;
  159. }
  160. else
  161. {
  162. if (values.size() != 3)
  163. return false;
  164. values.push_back("255");
  165. }
  166. // Parse the RGBA values.
  167. for (int i = 0; i < 4; ++i)
  168. {
  169. int component;
  170. // We're parsing a percentage value.
  171. if (values[i].size() > 0 && values[i][values[i].size() - 1] == '%')
  172. component = int((float)atof(values[i].substr(0, values[i].size() - 1).c_str()) * (255.0f / 100.0f));
  173. // We're parsing a 0 -> 255 integer value.
  174. else
  175. component = atoi(values[i].c_str());
  176. colour[i] = (byte)(Math::Clamp(component, 0, 255));
  177. }
  178. }
  179. else
  180. {
  181. // Check if we're parsing an 'hsla' or 'hsl' colour declaration.
  182. if (value.size() > 3 && value[3] == 'a')
  183. {
  184. if (values.size() != 4)
  185. return false;
  186. }
  187. else
  188. {
  189. if (values.size() != 3)
  190. return false;
  191. values.push_back("1.0");
  192. }
  193. // Parse the HSLA values.
  194. Array<float, 4> vals;
  195. // H is a number in degrees, A is a number between 0.0 and 1.0.
  196. for (int i : {0, 3})
  197. vals[i] = (float)atof(values[i].c_str());
  198. // S and L are percentage values.
  199. for (int i : {1, 2})
  200. if (values[i].size() > 0 && values[i][values[i].size() - 1] == '%')
  201. vals[i] = (float)atof(values[i].substr(0, values[i].size() - 1).c_str()) * (1.0f / 100.0f);
  202. else
  203. return false;
  204. HSLAToRGBA(vals);
  205. for (int i = 0; i < 4; ++i)
  206. {
  207. colour[i] = (byte)(Math::Clamp((int)(vals[i] * 255.0f), 0, 255));
  208. }
  209. }
  210. }
  211. else
  212. {
  213. // Check for the specification of an HTML colour.
  214. auto it = parser_data->html_colours.find(StringUtilities::ToLower(value));
  215. if (it == parser_data->html_colours.end())
  216. return false;
  217. else
  218. colour = it->second;
  219. }
  220. return true;
  221. }
  222. } // namespace Rml