PropertyParserColour.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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(parameters)) const
  58. {
  59. if (value.Empty())
  60. return false;
  61. Colourb colour;
  62. // Check for a hex colour.
  63. if (value[0] == '#')
  64. {
  65. char hex_values[4][2] = { {'f', 'f'},
  66. {'f', 'f'},
  67. {'f', 'f'},
  68. {'f', 'f'} };
  69. switch (value.Length())
  70. {
  71. // Single hex digit per channel, RGB and alpha.
  72. case 5: hex_values[3][0] = hex_values[3][1] = value[4];
  73. // Single hex digit per channel, RGB only.
  74. case 4: hex_values[0][0] = hex_values[0][1] = value[1];
  75. hex_values[1][0] = hex_values[1][1] = value[2];
  76. hex_values[2][0] = hex_values[2][1] = value[3];
  77. break;
  78. // Two hex digits per channel, RGB and alpha.
  79. case 9: hex_values[3][0] = value[7];
  80. hex_values[3][1] = value[8];
  81. // Two hex digits per channel, RGB only.
  82. case 7: memcpy(hex_values, &value.CString()[1], sizeof(char) * 6);
  83. break;
  84. default:
  85. return false;
  86. }
  87. // Parse each of the colour elements.
  88. for (int i = 0; i < 4; i++)
  89. {
  90. int tens = Math::HexToDecimal(hex_values[i][0]);
  91. int ones = Math::HexToDecimal(hex_values[i][1]);
  92. if (tens == -1 ||
  93. ones == -1)
  94. return false;
  95. colour[i] = (byte) (tens * 16 + ones);
  96. }
  97. }
  98. else if (value.Substring(0, 3) == "rgb")
  99. {
  100. StringList values;
  101. int find = (int)value.Find("(") + 1;
  102. StringUtilities::ExpandString(values, value.Substring(find, value.RFind(")") - find), ',');
  103. // Check if we're parsing an 'rgba' or 'rgb' colour declaration.
  104. if (value.Length() > 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].Length() > 0 && values[i][values[i].Length() - 1] == '%')
  121. component = Math::RealToInteger((float) (atof(values[i].Substring(0, values[i].Length() - 1).CString()) / 100.0f) * 255.0f);
  122. // We're parsing a 0 -> 255 integer value.
  123. else
  124. component = atoi(values[i].CString());
  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(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. // Destroys the parser.
  142. void PropertyParserColour::Release()
  143. {
  144. delete this;
  145. }
  146. }
  147. }