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