PropertyParserColour.cpp 4.9 KB

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