PropertyParserColour.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <string.h>
  30. namespace Rml {
  31. const PropertyParserColour::ColourMap PropertyParserColour::html_colours = {
  32. {"black", Colourb(0, 0, 0)},
  33. {"silver", Colourb(192, 192, 192)},
  34. {"gray", Colourb(128, 128, 128)},
  35. {"grey", Colourb(128, 128, 128)},
  36. {"white", Colourb(255, 255, 255)},
  37. {"maroon", Colourb(128, 0, 0)},
  38. {"red", Colourb(255, 0, 0)},
  39. {"orange", Colourb(255, 165, 0)},
  40. {"purple", Colourb(128, 0, 128)},
  41. {"fuchsia", Colourb(255, 0, 255)},
  42. {"green", Colourb(0, 128, 0)},
  43. {"lime", Colourb(0, 255, 0)},
  44. {"olive", Colourb(128, 128, 0)},
  45. {"yellow", Colourb(255, 255, 0)},
  46. {"navy", Colourb(0, 0, 128)},
  47. {"blue", Colourb(0, 0, 255)},
  48. {"teal", Colourb(0, 128, 128)},
  49. {"aqua", Colourb(0, 255, 255)},
  50. {"transparent", Colourb(0, 0, 0, 0)},
  51. };
  52. PropertyParserColour::PropertyParserColour() {}
  53. PropertyParserColour::~PropertyParserColour() {}
  54. bool PropertyParserColour::ParseValue(Property& property, const String& value, const ParameterMap& /*parameters*/) const
  55. {
  56. Colourb colour;
  57. if (!ParseColour(colour, value))
  58. return false;
  59. property.value = Variant(colour);
  60. property.unit = Unit::COLOUR;
  61. return true;
  62. }
  63. bool PropertyParserColour::ParseColour(Colourb& colour, const String& value)
  64. {
  65. if (value.empty())
  66. return false;
  67. colour = {};
  68. // Check for a hex colour.
  69. if (value[0] == '#')
  70. {
  71. char hex_values[4][2] = {{'f', 'f'}, {'f', 'f'}, {'f', 'f'}, {'f', 'f'}};
  72. switch (value.size())
  73. {
  74. // Single hex digit per channel, RGB and alpha.
  75. case 5:
  76. hex_values[3][0] = hex_values[3][1] = value[4];
  77. //-fallthrough
  78. // Single hex digit per channel, RGB only.
  79. case 4:
  80. hex_values[0][0] = hex_values[0][1] = value[1];
  81. hex_values[1][0] = hex_values[1][1] = value[2];
  82. hex_values[2][0] = hex_values[2][1] = value[3];
  83. break;
  84. // Two hex digits per channel, RGB and alpha.
  85. case 9:
  86. hex_values[3][0] = value[7];
  87. hex_values[3][1] = value[8];
  88. //-fallthrough
  89. // Two hex digits per channel, RGB only.
  90. case 7: memcpy(hex_values, &value.c_str()[1], sizeof(char) * 6); break;
  91. default: return false;
  92. }
  93. // Parse each of the colour elements.
  94. for (int i = 0; i < 4; i++)
  95. {
  96. int tens = Math::HexToDecimal(hex_values[i][0]);
  97. int ones = Math::HexToDecimal(hex_values[i][1]);
  98. if (tens == -1 || ones == -1)
  99. return false;
  100. colour[i] = (byte)(tens * 16 + ones);
  101. }
  102. }
  103. else if (value.substr(0, 3) == "rgb")
  104. {
  105. StringList values;
  106. values.reserve(4);
  107. size_t find = value.find('(');
  108. if (find == String::npos)
  109. return false;
  110. size_t begin_values = find + 1;
  111. StringUtilities::ExpandString(values, value.substr(begin_values, value.rfind(')') - begin_values), ',');
  112. // Check if we're parsing an 'rgba' or 'rgb' colour declaration.
  113. if (value.size() > 3 && value[3] == 'a')
  114. {
  115. if (values.size() != 4)
  116. return false;
  117. }
  118. else
  119. {
  120. if (values.size() != 3)
  121. return false;
  122. values.push_back("255");
  123. }
  124. // Parse the three RGB values.
  125. for (int i = 0; i < 4; ++i)
  126. {
  127. int component;
  128. // We're parsing a percentage value.
  129. if (values[i].size() > 0 && values[i][values[i].size() - 1] == '%')
  130. component = int((float)atof(values[i].substr(0, values[i].size() - 1).c_str()) * (255.0f / 100.0f));
  131. // We're parsing a 0 -> 255 integer value.
  132. else
  133. component = atoi(values[i].c_str());
  134. colour[i] = (byte)(Math::Clamp(component, 0, 255));
  135. }
  136. }
  137. else
  138. {
  139. // Check for the specification of an HTML colour.
  140. ColourMap::const_iterator iterator = html_colours.find(StringUtilities::ToLower(value));
  141. if (iterator == html_colours.end())
  142. return false;
  143. else
  144. colour = (*iterator).second;
  145. }
  146. return true;
  147. }
  148. } // namespace Rml