From a3c4aa86d93a3cd0830955661263feb3be352064 Mon Sep 17 00:00:00 2001 From: Niels Thykier Date: Tue, 23 Feb 2021 19:49:53 +0000 Subject: [PATCH] convertColor: Correctly shorten `#FF0000` (upper case) to `red` Signed-off-by: Niels Thykier --- scour/scour.py | 4 +++- unittests/color-formats.svg | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/scour/scour.py b/scour/scour.py index 362180e..1a75f25 100644 --- a/scour/scour.py +++ b/scour/scour.py @@ -2245,6 +2245,9 @@ def convertColor(s): """ Converts the input color string and returns it to the shortest known format for it. """ + if s[0] == '#': + # Normalize #RGB into lower case early as our lookup relies on the lower case name + s = s.lower() color = colors.get(s) # Short cut: if we know the color (either by name or hex code) then skip the # parsing logic. This makes @@ -2255,7 +2258,6 @@ def convertColor(s): # If it is in #RGB/#RRGGBB format already, then we can also avoid # the regex parsing code. Notably, if it is not a known hex code, # then at best we can truncate #RRGGBB into #RGB. - s = s.lower() if len(s) == 7 and s[1] == s[2] and s[3] == s[4] and s[5] == s[6]: s = '#' + s[1] + s[3] + s[5] return s diff --git a/unittests/color-formats.svg b/unittests/color-formats.svg index 7e28c7c..2889ea0 100644 --- a/unittests/color-formats.svg +++ b/unittests/color-formats.svg @@ -9,6 +9,6 @@ - +