| 42 | PropertyParserColorHack::~PropertyParserColorHack() {} |
| 43 | |
| 44 | bool PropertyParserColorHack::ParseValue(Rml::Property& property, const Rml::String& value, const Rml::ParameterMap& /*parameters*/) const { |
| 45 | if (value.empty()) |
| 46 | return false; |
| 47 | |
| 48 | Rml::Colourb colour; |
| 49 | |
| 50 | // Check for a hex colour. |
| 51 | if (value[0] == '#') |
| 52 | { |
| 53 | char hex_values[4][2] = { {'f', 'f'}, {'f', 'f'}, {'f', 'f'}, {'f', 'f'} }; |
| 54 | |
| 55 | switch (value.size()) |
| 56 | { |
| 57 | // Single hex digit per channel, RGB and alpha. |
| 58 | case 5: |
| 59 | hex_values[3][0] = hex_values[3][1] = value[4]; |
| 60 | //-fallthrough |
| 61 | // Single hex digit per channel, RGB only. |
| 62 | case 4: |
| 63 | hex_values[0][0] = hex_values[0][1] = value[1]; |
| 64 | hex_values[1][0] = hex_values[1][1] = value[2]; |
| 65 | hex_values[2][0] = hex_values[2][1] = value[3]; |
| 66 | break; |
| 67 | |
| 68 | // Two hex digits per channel, RGB and alpha. |
| 69 | case 9: |
| 70 | hex_values[3][0] = value[7]; |
| 71 | hex_values[3][1] = value[8]; |
| 72 | //-fallthrough |
| 73 | // Two hex digits per channel, RGB only. |
| 74 | case 7: memcpy(hex_values, &value.c_str()[1], sizeof(char) * 6); break; |
| 75 | |
| 76 | default: return false; |
| 77 | } |
| 78 | |
| 79 | // Parse each of the colour elements. |
| 80 | for (size_t i = 0; i < 4; i++) |
| 81 | { |
| 82 | int tens = Rml::Math::HexToDecimal(hex_values[i][0]); |
| 83 | int ones = Rml::Math::HexToDecimal(hex_values[i][1]); |
| 84 | if (tens == -1 || ones == -1) |
| 85 | return false; |
| 86 | |
| 87 | colour[i] = (Rml::byte)(tens * 16 + ones); |
| 88 | } |
| 89 | } |
| 90 | else if (value.substr(0, 3) == "rgb") |
| 91 | { |
| 92 | Rml::StringList values; |
| 93 | values.reserve(4); |
| 94 | |
| 95 | size_t find = value.find('('); |
| 96 | if (find == Rml::String::npos) |
| 97 | return false; |
| 98 | |
| 99 | size_t begin_values = find + 1; |
| 100 | |
| 101 | Rml::StringUtilities::ExpandString(values, value.substr(begin_values, value.rfind(')') - begin_values), ','); |