convertColor: Correctly shorten #FF0000 (upper case) to red

Signed-off-by: Niels Thykier <niels@thykier.net>
This commit is contained in:
Niels Thykier 2021-02-23 19:49:53 +00:00
parent ce92515c1c
commit a3c4aa86d9
No known key found for this signature in database
GPG key ID: A65B78DBE67C7AAC
2 changed files with 4 additions and 2 deletions

View file

@ -2245,6 +2245,9 @@ def convertColor(s):
""" """
Converts the input color string and returns it to the shortest known format for it. 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) color = colors.get(s)
# Short cut: if we know the color (either by name or hex code) then skip the # Short cut: if we know the color (either by name or hex code) then skip the
# parsing logic. This makes # parsing logic. This makes
@ -2255,7 +2258,6 @@ def convertColor(s):
# If it is in #RGB/#RRGGBB format already, then we can also avoid # 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, # the regex parsing code. Notably, if it is not a known hex code,
# then at best we can truncate #RRGGBB into #RGB. # 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]: 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] s = '#' + s[1] + s[3] + s[5]
return s return s

View file

@ -9,6 +9,6 @@
<rect id="rect" width="100" height="100" fill="rgb(15,16,17)" stroke="darkgrey" /> <rect id="rect" width="100" height="100" fill="rgb(15,16,17)" stroke="darkgrey" />
<circle id="circle" cx="100" cy="100" r="30" fill="url(#g1)" stroke="url(#c1)" /> <circle id="circle" cx="100" cy="100" r="30" fill="url(#g1)" stroke="url(#c1)" />
<ellipse id="ellipse" cx="100" cy="100" rx="30" ry="30" style="fill:#ffffff" fill="black" /> <ellipse id="ellipse" cx="100" cy="100" rx="30" ry="30" style="fill:#ffffff" fill="black" />
<rect id="short_color" width="100" height="100" fill="rgb(255,0, 0)" stroke="#ff0000" /> <rect id="short_color" width="100" height="100" fill="rgb(255,0, 0)" stroke="#FF0000" />
<rect id="tied_color" width="100" height="100" fill="blue" stroke="rgb(0, 0, 255)" /> <rect id="tied_color" width="100" height="100" fill="blue" stroke="rgb(0, 0, 255)" />
</svg> </svg>

Before

Width:  |  Height:  |  Size: 768 B

After

Width:  |  Height:  |  Size: 768 B

Before After
Before After