PropertyParserColour.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "PropertyParserColour.h"
  29. namespace Rocket {
  30. namespace Core {
  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["fuschia"] = 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(255, 255, 255, 0);
  52. }
  53. PropertyParserColour::~PropertyParserColour()
  54. {
  55. }
  56. // Called to parse a RCSS colour declaration.
  57. bool PropertyParserColour::ParseValue(Property& property, const String& value, const ParameterMap& ROCKET_UNUSED_PARAMETER(parameters)) const
  58. {
  59. ROCKET_UNUSED(parameters);
  60. if (value.Empty())
  61. return false;
  62. Colourb colour;
  63. // Check for a hex colour.
  64. if (value[0] == '#')
  65. {
  66. char hex_values[4][2] = { {'f', 'f'},
  67. {'f', 'f'},
  68. {'f', 'f'},
  69. {'f', 'f'} };
  70. switch (value.Length())
  71. {
  72. // Single hex digit per channel, RGB and alpha.
  73. case 5: hex_values[3][0] = hex_values[3][1] = value[4];
  74. // Single hex digit per channel, RGB only.
  75. case 4: hex_values[0][0] = hex_values[0][1] = value[1];
  76. hex_values[1][0] = hex_values[1][1] = value[2];
  77. hex_values[2][0] = hex_values[2][1] = value[3];
  78. break;
  79. // Two hex digits per channel, RGB and alpha.
  80. case 9: hex_values[3][0] = value[7];
  81. hex_values[3][1] = value[8];
  82. // Two hex digits per channel, RGB only.
  83. case 7: memcpy(hex_values, &value.CString()[1], sizeof(char) * 6);
  84. break;
  85. default:
  86. return false;
  87. }
  88. // Parse each of the colour elements.
  89. for (int i = 0; i < 4; i++)
  90. {
  91. int tens = Math::HexToDecimal(hex_values[i][0]);
  92. int ones = Math::HexToDecimal(hex_values[i][1]);
  93. if (tens == -1 ||
  94. ones == -1)
  95. return false;
  96. colour[i] = (byte) (tens * 16 + ones);
  97. }
  98. }
  99. else if (value.Substring(0, 3) == "rgb")
  100. {
  101. StringList values;
  102. int find = (int)value.Find("(") + 1;
  103. StringUtilities::ExpandString(values, value.Substring(find, value.RFind(")") - find), ',');
  104. // Check if we're parsing an 'rgba' or 'rgb' colour declaration.
  105. if (value.Length() > 3 && value[3] == 'a')
  106. {
  107. if (values.size() != 4)
  108. return false;
  109. }
  110. else
  111. {
  112. if (values.size() != 3)
  113. return false;
  114. values.push_back("255");
  115. }
  116. // Parse the three RGB values.
  117. for (int i = 0; i < 4; ++i)
  118. {
  119. int component;
  120. // We're parsing a percentage value.
  121. if (values[i].Length() > 0 && values[i][values[i].Length() - 1] == '%')
  122. component = Math::RealToInteger((float) (atof(values[i].Substring(0, values[i].Length() - 1).CString()) / 100.0f) * 255.0f);
  123. // We're parsing a 0 -> 255 integer value.
  124. else
  125. component = atoi(values[i].CString());
  126. colour[i] = (byte) (Math::Clamp(component, 0, 255));
  127. }
  128. }
  129. else
  130. {
  131. // Check for the specification of an HTML colour.
  132. ColourMap::const_iterator iterator = html_colours.find(value);
  133. if (iterator == html_colours.end())
  134. return false;
  135. else
  136. colour = (*iterator).second;
  137. }
  138. property.value = Variant(colour);
  139. property.unit = Property::COLOUR;
  140. return true;
  141. }
  142. // Destroys the parser.
  143. void PropertyParserColour::Release()
  144. {
  145. delete this;
  146. }
  147. }
  148. }