PropertyParserColour.cpp 5.0 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 "precompiled.h"
  29. #include "PropertyParserColour.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.Length())
  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.CString()[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.Substring(0, 3) == "rgb")
  101. {
  102. StringList values;
  103. int find = (int)value.Find("(") + 1;
  104. StringUtilities::ExpandString(values, value.Substring(find, value.RFind(")") - find), ',');
  105. // Check if we're parsing an 'rgba' or 'rgb' colour declaration.
  106. if (value.Length() > 3 && value[3] == 'a')
  107. {
  108. if (values.size() != 4)
  109. return false;
  110. }
  111. else
  112. {
  113. if (values.size() != 3)
  114. return false;
  115. values.push_back("255");
  116. }
  117. // Parse the three RGB values.
  118. for (int i = 0; i < 4; ++i)
  119. {
  120. int component;
  121. // We're parsing a percentage value.
  122. if (values[i].Length() > 0 && values[i][values[i].Length() - 1] == '%')
  123. component = Math::RealToInteger((float) (atof(values[i].Substring(0, values[i].Length() - 1).CString()) / 100.0f) * 255.0f);
  124. // We're parsing a 0 -> 255 integer value.
  125. else
  126. component = atoi(values[i].CString());
  127. colour[i] = (byte) (Math::Clamp(component, 0, 255));
  128. }
  129. }
  130. else
  131. {
  132. // Check for the specification of an HTML colour.
  133. ColourMap::const_iterator iterator = html_colours.find(value);
  134. if (iterator == html_colours.end())
  135. return false;
  136. else
  137. colour = (*iterator).second;
  138. }
  139. property.value = Variant(colour);
  140. property.unit = Property::COLOUR;
  141. return true;
  142. }
  143. // Destroys the parser.
  144. void PropertyParserColour::Release()
  145. {
  146. delete this;
  147. }
  148. }
  149. }