From 49361b4775f3e2f4fbe4eb43b613e836a91a1573 Mon Sep 17 00:00:00 2001 From: ed Date: Mon, 20 Apr 2020 15:28:12 +0100 Subject: [PATCH] SVG: Added support for RGBA, HSL and HSLA colours --- .../drawables/juce_SVGParser.cpp | 69 +++++++++++++------ 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/modules/juce_gui_basics/drawables/juce_SVGParser.cpp b/modules/juce_gui_basics/drawables/juce_SVGParser.cpp index f4d64e482a13..31bc56e5cb2c 100644 --- a/modules/juce_gui_basics/drawables/juce_SVGParser.cpp +++ b/modules/juce_gui_basics/drawables/juce_SVGParser.cpp @@ -1518,16 +1518,18 @@ class SVGState if (text.startsWithChar ('#')) { - uint32 hex[6] = { 0 }; + uint32 hex[8] = { 0 }; + hex[6] = hex[7] = 15; + int numChars = 0; auto s = text.getCharPointer(); - while (numChars < 6) + while (numChars < 8) { auto hexValue = CharacterFunctions::getHexDigitValue (*++s); if (hexValue >= 0) - hex [numChars++] = (uint32) hexValue; + hex[numChars++] = (uint32) hexValue; else break; } @@ -1539,30 +1541,53 @@ class SVGState return Colour ((uint8) ((hex[0] << 4) + hex[1]), (uint8) ((hex[2] << 4) + hex[3]), - (uint8) ((hex[4] << 4) + hex[5])); + (uint8) ((hex[4] << 4) + hex[5]), + (uint8) ((hex[6] << 4) + hex[7])); } - if (text.startsWith ("rgb")) + if (text.startsWith ("rgb") || text.startsWith ("hsl")) { - auto openBracket = text.indexOfChar ('('); - auto closeBracket = text.indexOfChar (openBracket, ')'); + auto tokens = [&text] + { + auto openBracket = text.indexOfChar ('('); + auto closeBracket = text.indexOfChar (openBracket, ')'); + + StringArray arr; + + if (openBracket >= 3 && closeBracket > openBracket) + { + arr.addTokens (text.substring (openBracket + 1, closeBracket), ",", ""); + arr.trim(); + arr.removeEmptyStrings(); + } - if (openBracket >= 3 && closeBracket > openBracket) + return arr; + }(); + + auto alpha = [&tokens, &text] { - StringArray tokens; - tokens.addTokens (text.substring (openBracket + 1, closeBracket), ",", ""); - tokens.trim(); - tokens.removeEmptyStrings(); - - if (tokens[0].containsChar ('%')) - return Colour ((uint8) roundToInt (2.55 * tokens[0].getDoubleValue()), - (uint8) roundToInt (2.55 * tokens[1].getDoubleValue()), - (uint8) roundToInt (2.55 * tokens[2].getDoubleValue())); - - return Colour ((uint8) tokens[0].getIntValue(), - (uint8) tokens[1].getIntValue(), - (uint8) tokens[2].getIntValue()); - } + if ((text.startsWith ("rgba") || text.startsWith ("hsla")) && tokens.size() == 4) + return tokens[3].getFloatValue(); + + return 1.0f; + }(); + + if (text.startsWith ("hsl")) + return Colour ((float) (tokens[0].getDoubleValue() / 360.0), + (float) (tokens[1].getDoubleValue() / 100.0), + (float) (tokens[2].getDoubleValue() / 100.0), + alpha); + + if (tokens[0].containsChar ('%')) + return Colour ((uint8) roundToInt (2.55 * tokens[0].getDoubleValue()), + (uint8) roundToInt (2.55 * tokens[1].getDoubleValue()), + (uint8) roundToInt (2.55 * tokens[2].getDoubleValue()), + alpha); + + return Colour ((uint8) tokens[0].getIntValue(), + (uint8) tokens[1].getIntValue(), + (uint8) tokens[2].getIntValue(), + alpha); } if (text == "inherit")