PropertyParserColour.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. #include <utility>
  34. namespace Rml {
  35. // Helper function for hsl->rgb conversion.
  36. static float HSL_f(float h, float s, float l, float n)
  37. {
  38. float k = std::fmod((n + h * (1.0f / 30.0f)), 12.0f);
  39. float a = s * std::min(l, 1.0f - l);
  40. return l - a * std::max(-1.0f, std::min({k - 3.0f, 9.0f - k, 1.0f}));
  41. }
  42. // Reference: https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative
  43. static void HSLAToRGBA(Array<float, 4>& vals)
  44. {
  45. if (vals[1] == 0.0f)
  46. {
  47. vals[0] = vals[1] = vals[2];
  48. }
  49. else
  50. {
  51. float h = std::fmod(vals[0], 360.0f);
  52. if (h < 0)
  53. h += 360.0f;
  54. float s = vals[1];
  55. float l = vals[2];
  56. vals[0] = HSL_f(h, s, l, 0.0f);
  57. vals[1] = HSL_f(h, s, l, 8.0f);
  58. vals[2] = HSL_f(h, s, l, 4.0f);
  59. }
  60. }
  61. // Reference: https://en.wikipedia.org/wiki/SRGB#Definition
  62. static float InverseSRGBNonlinearTransfer(float channel)
  63. {
  64. return channel > 0.0031308f ? 1.055f * std::pow(channel, 1.0f / 2.4f) - 0.055f : 12.92f * channel;
  65. }
  66. // Reference: https://en.wikipedia.org/wiki/CIELAB_color_space#Converting_between_CIELAB_and_CIE_XYZ_coordinates
  67. static void CIELABToRGBA(Array<float, 4>& values)
  68. {
  69. float y_double_prime = (values[0] + 16.0f) / 116.0f;
  70. float x_double_prime = (values[1] / 500.0f) + y_double_prime;
  71. float z_double_prime = y_double_prime - (values[2] / 200.0f);
  72. float x_prime = (x_double_prime * x_double_prime * x_double_prime) > 0.008856f ? (x_double_prime * x_double_prime * x_double_prime)
  73. : (x_double_prime - (16.0f / 116.0f)) / 7.787f;
  74. float y_prime = (y_double_prime * y_double_prime * y_double_prime) > 0.008856f ? (y_double_prime * y_double_prime * y_double_prime)
  75. : (y_double_prime - (16.0f / 116.0f)) / 7.787f;
  76. float z_prime = (z_double_prime * z_double_prime * z_double_prime) > 0.008856f ? (z_double_prime * z_double_prime * z_double_prime)
  77. : (z_double_prime - (16.0f / 116.0f)) / 7.787f;
  78. static const Vector3f illuminant_d65_multiplicands(0.95047f, 1.0f, 1.08883f);
  79. float x = x_prime * illuminant_d65_multiplicands.x;
  80. float y = y_prime * illuminant_d65_multiplicands.y;
  81. float z = z_prime * illuminant_d65_multiplicands.z;
  82. static constexpr Array<Array<float, 3>, 3> xyz_to_srgb_matrix{
  83. Array<float, 3>{+3.2404548f, -1.5371389f, -0.4985315f},
  84. Array<float, 3>{-0.9692664f, +1.8760109f, +0.0415561f},
  85. Array<float, 3>{+0.0556434f, -0.2040259f, +1.0572252f},
  86. };
  87. float r = xyz_to_srgb_matrix[0][0] * x + xyz_to_srgb_matrix[0][1] * y + xyz_to_srgb_matrix[0][2] * z;
  88. float g = xyz_to_srgb_matrix[1][0] * x + xyz_to_srgb_matrix[1][1] * y + xyz_to_srgb_matrix[1][2] * z;
  89. float b = xyz_to_srgb_matrix[2][0] * x + xyz_to_srgb_matrix[2][1] * y + xyz_to_srgb_matrix[2][2] * z;
  90. values[0] = Math::Clamp(InverseSRGBNonlinearTransfer(r), 0.0f, 1.0f);
  91. values[1] = Math::Clamp(InverseSRGBNonlinearTransfer(g), 0.0f, 1.0f);
  92. values[2] = Math::Clamp(InverseSRGBNonlinearTransfer(b), 0.0f, 1.0f);
  93. }
  94. // References: https://en.wikipedia.org/wiki/Oklab_color_space#Conversions_between_color_spaces and https://bottosson.github.io/posts/oklab/
  95. static void OklabToRGBA(Array<float, 4>& values)
  96. {
  97. static constexpr Array<Array<float, 3>, 3> oklab_to_lms_prime_matrix{
  98. Array<float, 3>{+1.0f, +0.3963377774f, +0.2158037573f},
  99. Array<float, 3>{+1.0f, -0.1055613458f, -0.0638541728f},
  100. Array<float, 3>{+1.0f, -0.0894841775f, -1.2914855480f},
  101. };
  102. float lightness = values[0];
  103. float a_axis = values[1];
  104. float b_axis = values[2];
  105. float l_prime = oklab_to_lms_prime_matrix[0][0] * lightness + oklab_to_lms_prime_matrix[0][1] * a_axis + oklab_to_lms_prime_matrix[0][2] * b_axis;
  106. float m_prime = oklab_to_lms_prime_matrix[1][0] * lightness + oklab_to_lms_prime_matrix[1][1] * a_axis + oklab_to_lms_prime_matrix[1][2] * b_axis;
  107. float s_prime = oklab_to_lms_prime_matrix[2][0] * lightness + oklab_to_lms_prime_matrix[2][1] * a_axis + oklab_to_lms_prime_matrix[2][2] * b_axis;
  108. float l = l_prime * l_prime * l_prime;
  109. float m = m_prime * m_prime * m_prime;
  110. float s = s_prime * s_prime * s_prime;
  111. static constexpr Array<Array<float, 3>, 3> lms_to_srgb_matrix{
  112. Array<float, 3>{+4.0767416621f, -3.3077115913f, +0.2309699292f},
  113. Array<float, 3>{-1.2684380046f, +2.6097574011f, -0.3413193965f},
  114. Array<float, 3>{-0.0041960863f, -0.7034186147f, +1.7076147010f},
  115. };
  116. float r = lms_to_srgb_matrix[0][0] * l + lms_to_srgb_matrix[0][1] * m + lms_to_srgb_matrix[0][2] * s;
  117. float g = lms_to_srgb_matrix[1][0] * l + lms_to_srgb_matrix[1][1] * m + lms_to_srgb_matrix[1][2] * s;
  118. float b = lms_to_srgb_matrix[2][0] * l + lms_to_srgb_matrix[2][1] * m + lms_to_srgb_matrix[2][2] * s;
  119. values[0] = Math::Clamp(InverseSRGBNonlinearTransfer(r), 0.0f, 1.0f);
  120. values[1] = Math::Clamp(InverseSRGBNonlinearTransfer(g), 0.0f, 1.0f);
  121. values[2] = Math::Clamp(InverseSRGBNonlinearTransfer(b), 0.0f, 1.0f);
  122. }
  123. struct PropertyParserColourData {
  124. const UnorderedMap<String, Colourb> html_colours = {
  125. {"black", Colourb(0, 0, 0)},
  126. {"silver", Colourb(192, 192, 192)},
  127. {"gray", Colourb(128, 128, 128)},
  128. {"grey", Colourb(128, 128, 128)},
  129. {"white", Colourb(255, 255, 255)},
  130. {"maroon", Colourb(128, 0, 0)},
  131. {"red", Colourb(255, 0, 0)},
  132. {"orange", Colourb(255, 165, 0)},
  133. {"purple", Colourb(128, 0, 128)},
  134. {"fuchsia", Colourb(255, 0, 255)},
  135. {"green", Colourb(0, 128, 0)},
  136. {"lime", Colourb(0, 255, 0)},
  137. {"olive", Colourb(128, 128, 0)},
  138. {"yellow", Colourb(255, 255, 0)},
  139. {"navy", Colourb(0, 0, 128)},
  140. {"blue", Colourb(0, 0, 255)},
  141. {"teal", Colourb(0, 128, 128)},
  142. {"aqua", Colourb(0, 255, 255)},
  143. {"transparent", Colourb(0, 0, 0, 0)},
  144. };
  145. };
  146. ControlledLifetimeResource<PropertyParserColourData> PropertyParserColour::parser_data;
  147. void PropertyParserColour::Initialize()
  148. {
  149. parser_data.Initialize();
  150. }
  151. void PropertyParserColour::Shutdown()
  152. {
  153. parser_data.Shutdown();
  154. }
  155. PropertyParserColour::PropertyParserColour() {}
  156. PropertyParserColour::~PropertyParserColour() {}
  157. bool PropertyParserColour::ParseValue(Property& property, const String& value, const ParameterMap& /*parameters*/) const
  158. {
  159. Colourb colour;
  160. if (!ParseColour(colour, value))
  161. return false;
  162. property.value = Variant(colour);
  163. property.unit = Unit::COLOUR;
  164. return true;
  165. }
  166. bool PropertyParserColour::ParseColour(Colourb& colour, const String& value)
  167. {
  168. if (value.empty())
  169. return false;
  170. colour = {};
  171. if (value[0] == '#')
  172. {
  173. if (!ParseHexColour(colour, value))
  174. return false;
  175. }
  176. else if (value.substr(0, 3) == "rgb")
  177. {
  178. if (!ParseRGBColour(colour, value))
  179. return false;
  180. }
  181. else if (value.substr(0, 3) == "hsl")
  182. {
  183. if (!ParseHSLColour(colour, value))
  184. return false;
  185. }
  186. else if (value.substr(0, 3) == "lab" || value.substr(0, 3) == "lch")
  187. {
  188. if (!ParseCIELABColour(colour, value))
  189. return false;
  190. }
  191. else if (value.substr(0, 5) == "oklab" || value.substr(0, 5) == "oklch")
  192. {
  193. if (!ParseOklabColour(colour, value))
  194. return false;
  195. }
  196. else
  197. {
  198. // Check for the specification of an HTML colour.
  199. auto it = parser_data->html_colours.find(StringUtilities::ToLower(value));
  200. if (it == parser_data->html_colours.end())
  201. return false;
  202. else
  203. colour = it->second;
  204. }
  205. return true;
  206. }
  207. bool PropertyParserColour::ParseHexColour(Colourb& colour, const String& value)
  208. {
  209. char hex_values[4][2] = {{'f', 'f'}, {'f', 'f'}, {'f', 'f'}, {'f', 'f'}};
  210. switch (value.size())
  211. {
  212. // Single hex digit per channel, RGB and alpha.
  213. case 5:
  214. hex_values[3][0] = hex_values[3][1] = value[4];
  215. //-fallthrough
  216. // Single hex digit per channel, RGB only.
  217. case 4:
  218. hex_values[0][0] = hex_values[0][1] = value[1];
  219. hex_values[1][0] = hex_values[1][1] = value[2];
  220. hex_values[2][0] = hex_values[2][1] = value[3];
  221. break;
  222. // Two hex digits per channel, RGB and alpha.
  223. case 9:
  224. hex_values[3][0] = value[7];
  225. hex_values[3][1] = value[8];
  226. //-fallthrough
  227. // Two hex digits per channel, RGB only.
  228. case 7: memcpy(hex_values, &value.c_str()[1], sizeof(char) * 6); break;
  229. default: return false;
  230. }
  231. // Parse each of the colour elements.
  232. for (int i = 0; i < 4; i++)
  233. {
  234. int tens = Math::HexToDecimal(hex_values[i][0]);
  235. int ones = Math::HexToDecimal(hex_values[i][1]);
  236. if (tens == -1 || ones == -1)
  237. return false;
  238. colour[i] = (byte)(tens * 16 + ones);
  239. }
  240. return true;
  241. }
  242. bool PropertyParserColour::ParseRGBColour(Colourb& colour, const String& value)
  243. {
  244. StringList values;
  245. values.reserve(4);
  246. if (!GetColourFunctionValues(values, value, true))
  247. return false;
  248. // Check if we're parsing an 'rgba' or 'rgb' colour declaration.
  249. if (value.size() > 3 && value[3] == 'a')
  250. {
  251. if (values.size() != 4)
  252. return false;
  253. }
  254. else
  255. {
  256. if (values.size() != 3)
  257. return false;
  258. values.push_back("255");
  259. }
  260. // Parse the RGBA values.
  261. for (int i = 0; i < 4; ++i)
  262. {
  263. int component;
  264. // We're parsing a percentage value.
  265. if (values[i].size() > 0 && values[i][values[i].size() - 1] == '%')
  266. component = int((float)atof(values[i].substr(0, values[i].size() - 1).c_str()) * (255.0f / 100.0f));
  267. // We're parsing a 0 -> 255 integer value.
  268. else
  269. component = atoi(values[i].c_str());
  270. colour[i] = (byte)(Math::Clamp(component, 0, 255));
  271. }
  272. return true;
  273. }
  274. bool PropertyParserColour::ParseHSLColour(Colourb& colour, const String& value)
  275. {
  276. StringList values;
  277. values.reserve(4);
  278. if (!GetColourFunctionValues(values, value, true))
  279. return false;
  280. // Check if we're parsing an 'hsla' or 'hsl' colour declaration.
  281. if (value.size() > 3 && value[3] == 'a')
  282. {
  283. if (values.size() != 4)
  284. return false;
  285. }
  286. else
  287. {
  288. if (values.size() != 3)
  289. return false;
  290. values.push_back("1.0");
  291. }
  292. // Parse the HSLA values.
  293. Array<float, 4> vals;
  294. // H is a number in degrees, A is a number between 0.0 and 1.0.
  295. for (int i : {0, 3})
  296. vals[i] = (float)atof(values[i].c_str());
  297. // S and L are percentage values.
  298. for (int i : {1, 2})
  299. if (values[i].size() > 0 && values[i][values[i].size() - 1] == '%')
  300. vals[i] = (float)atof(values[i].substr(0, values[i].size() - 1).c_str()) * (1.0f / 100.0f);
  301. else
  302. return false;
  303. HSLAToRGBA(vals);
  304. for (int i = 0; i < 4; ++i)
  305. colour[i] = (byte)(Math::Clamp((int)(vals[i] * 255.0f), 0, 255));
  306. return true;
  307. }
  308. bool PropertyParserColour::ParseCIELABColour(Colourb& colour, const String& value)
  309. {
  310. StringList values;
  311. values.reserve(5);
  312. if (!GetColourFunctionValues(values, value, false))
  313. return false;
  314. // Check if we have an alpha component.
  315. if (values.size() == 5)
  316. {
  317. if (values[3] != "/")
  318. return false;
  319. values[3] = std::move(values[4]);
  320. values.pop_back();
  321. }
  322. else
  323. {
  324. if (values.size() != 3)
  325. return false;
  326. values.push_back("1.0");
  327. }
  328. Array<float, 4> lab_values;
  329. // Parse lightness and alpha (same for both lab and lch).
  330. for (int i : {0, 3})
  331. {
  332. // Value can either be 'none' (representing 0.0), a percentage between 0% and 100%, or a number (between 0.0 and 100.0 for lightness and between 0.0 and 1.0 for alpha).
  333. if (values[i] == "none")
  334. lab_values[i] = 0.0f;
  335. else if (values[i][values[i].size() - 1] == '%')
  336. {
  337. lab_values[i] = (float)atof(values[i].substr(0, values[i].size() - 1).c_str());
  338. if (i == 3)
  339. lab_values[i] /= 100.0f;
  340. }
  341. else
  342. lab_values[i] = (float)atof(values[i].c_str());
  343. lab_values[i] = Math::Clamp(lab_values[i], 0.0f, i == 0 ? 100.0f : 1.0f);
  344. }
  345. // Determine if colour is in CIELAB or CIELCh space.
  346. if (value.substr(0, 3) == "lab")
  347. {
  348. // Parse A-axis (green-to-red) and B-axis (blue-to-yellow).
  349. for (int i : {1, 2})
  350. {
  351. // Value can either be 'none' (representing 0.0), a percentage between -100% and +100% (representing -125.0 to +125.0), or a number.
  352. if (values[i] == "none")
  353. lab_values[i] = 0.0f;
  354. else if (values[i][values[i].size() - 1] == '%')
  355. {
  356. static constexpr float cielab_axis_percentage_bound = 125.0f;
  357. lab_values[i] = (float)atof(values[i].substr(0, values[i].size() - 1).c_str()) / 100.0f * cielab_axis_percentage_bound;
  358. }
  359. else
  360. lab_values[i] = (float)atof(values[i].c_str());
  361. // Whilst the axis values are theoretically unbounded, in practice, they only exist between -160.0 and +160.0.
  362. static constexpr float cielab_axis_bound_limit = 160.0f;
  363. lab_values[i] = Math::Clamp(lab_values[i], -cielab_axis_bound_limit, +cielab_axis_bound_limit);
  364. }
  365. }
  366. else
  367. {
  368. // Parse chroma; value can either be 'none' (representing 0.0), a percentage between 0% and 100% (representing 0.0 to 150.0), or a number.
  369. float chroma = 0.0f;
  370. if (values[1] == "none")
  371. chroma = 0.0f;
  372. else if (values[1][values[1].size() - 1] == '%')
  373. {
  374. static constexpr float cielch_maximum_percentage_chroma = 150.0f;
  375. chroma = (float)atof(values[1].substr(0, values[1].size() - 1).c_str()) / 100.0f * cielch_maximum_percentage_chroma;
  376. }
  377. else
  378. chroma = (float)atof(values[1].c_str());
  379. // Whilst the chroma is theoretically unbounded, in practice, it does not exceed 230.0.
  380. static constexpr float cielch_maximum_chroma = 230.0f;
  381. chroma = Math::Clamp(chroma, 0.0f, cielch_maximum_chroma);
  382. // Parse hue; value can either be 'none' (representing 0.0), or an angle.
  383. float hue = 0.0f;
  384. if (values[2] == "none")
  385. hue = 0.0f;
  386. else
  387. hue = (float)atof(values[2].c_str());
  388. // Convert LCh polar coordinates to LAB Cartesian coordinates.
  389. lab_values[1] = chroma * Math::Cos(Math::DegreesToRadians(hue));
  390. lab_values[2] = chroma * Math::Sin(Math::DegreesToRadians(hue));
  391. }
  392. CIELABToRGBA(lab_values);
  393. for (int i = 0; i < 4; ++i)
  394. colour[i] = (byte)(Math::Clamp((int)(lab_values[i] * 255.0f), 0, 255));
  395. return true;
  396. }
  397. bool PropertyParserColour::ParseOklabColour(Colourb& colour, const String& value)
  398. {
  399. StringList values;
  400. values.reserve(5);
  401. if (!GetColourFunctionValues(values, value, false))
  402. return false;
  403. // Check if we have an alpha component.
  404. if (values.size() == 5)
  405. {
  406. if (values[3] != "/")
  407. return false;
  408. values[3] = std::move(values[4]);
  409. values.pop_back();
  410. }
  411. else
  412. {
  413. if (values.size() != 3)
  414. return false;
  415. values.push_back("1.0");
  416. }
  417. Array<float, 4> oklab_values;
  418. // Parse lightness and alpha (same for both Oklab and Oklch).
  419. for (int i : {0, 3})
  420. {
  421. // Value can either be 'none' (representing 0.0), a percentage between 0% and 100%, or a number between 0.0 and 1.0.
  422. if (values[i] == "none")
  423. oklab_values[i] = 0.0f;
  424. else if (values[i][values[i].size() - 1] == '%')
  425. oklab_values[i] = (float)atof(values[i].substr(0, values[i].size() - 1).c_str()) / 100.0f;
  426. else
  427. oklab_values[i] = (float)atof(values[i].c_str());
  428. oklab_values[i] = Math::Clamp(oklab_values[i], 0.0f, 1.0f);
  429. }
  430. // Determine if colour is in Oklab or Oklch space.
  431. if (value.substr(0, 5) == "oklab")
  432. {
  433. // Parse A-axis (green-to-red) and B-axis (blue-to-yellow).
  434. for (int i : {1, 2})
  435. {
  436. // Value can either be 'none' (representing 0.0), a percentage between -100% and +100% (representing -0.4 to +0.4), or a number.
  437. if (values[i] == "none")
  438. oklab_values[i] = 0.0f;
  439. else if (values[i][values[i].size() - 1] == '%')
  440. {
  441. static constexpr float oklab_axis_percentage_bound = 0.4f;
  442. oklab_values[i] = (float)atof(values[i].substr(0, values[i].size() - 1).c_str()) / 100.0f * oklab_axis_percentage_bound;
  443. }
  444. else
  445. oklab_values[i] = (float)atof(values[i].c_str());
  446. // Whilst the axis values are theoretically unbounded, in practice, they only exist between -0.5 and +0.5.
  447. static constexpr float oklab_axis_bound_limit = 0.5f;
  448. oklab_values[i] = Math::Clamp(oklab_values[i], -oklab_axis_bound_limit, +oklab_axis_bound_limit);
  449. }
  450. }
  451. else
  452. {
  453. // Parse chroma; value can either be 'none' (representing 0.0), a percentage between 0% and 100% (representing 0.0 to 0.4), or a number.
  454. float chroma = 0.0f;
  455. if (values[1] == "none")
  456. chroma = 0.0f;
  457. else if (values[1][values[1].size() - 1] == '%')
  458. {
  459. static constexpr float oklch_maximum_percentage_chroma = 0.4f;
  460. chroma = (float)atof(values[1].substr(0, values[1].size() - 1).c_str()) / 100.0f * oklch_maximum_percentage_chroma;
  461. }
  462. else
  463. chroma = (float)atof(values[1].c_str());
  464. // Whilst the chroma is theoretically unbounded, in practice, it does not exceed 0.5.
  465. static constexpr float oklch_maximum_chroma = 0.5f;
  466. chroma = Math::Clamp(chroma, 0.0f, oklch_maximum_chroma);
  467. // Parse hue; value can either be 'none' (representing 0.0), or an angle.
  468. float hue = 0.0f;
  469. if (values[2] == "none")
  470. hue = 0.0f;
  471. else
  472. hue = (float)atof(values[2].c_str());
  473. // Convert Oklch polar coordinates to Oklab Cartesian coordinates.
  474. oklab_values[1] = chroma * Math::Cos(Math::DegreesToRadians(hue));
  475. oklab_values[2] = chroma * Math::Sin(Math::DegreesToRadians(hue));
  476. }
  477. OklabToRGBA(oklab_values);
  478. for (int i = 0; i < 4; ++i)
  479. colour[i] = (byte)(Math::Clamp((int)(oklab_values[i] * 255.0f), 0, 255));
  480. return true;
  481. }
  482. bool PropertyParserColour::GetColourFunctionValues(StringList& values, const String& value, bool is_comma_separated)
  483. {
  484. size_t find = value.find('(');
  485. if (find == String::npos)
  486. return false;
  487. size_t begin_values = find + 1;
  488. StringUtilities::ExpandString(values, value.substr(begin_values, value.rfind(')') - begin_values), is_comma_separated ? ',' : ' ',
  489. !is_comma_separated);
  490. return true;
  491. }
  492. } // namespace Rml