Add support for shorthand hex colors
This commit is contained in:
@@ -12,7 +12,9 @@ class Color(NamedTuple):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def parse(cls, s: str) -> 'Color':
|
def parse(cls, s: str) -> 'Color':
|
||||||
if s.startswith('#'):
|
if s.startswith('#') and len(s) >= 7:
|
||||||
return cls(r=int(s[1:3], 16), g=int(s[3:5], 16), b=int(s[5:7], 16))
|
return cls(r=int(s[1:3], 16), g=int(s[3:5], 16), b=int(s[5:7], 16))
|
||||||
|
elif s.startswith('#'):
|
||||||
|
return cls.parse(f'#{s[1] * 2}{s[2] * 2}{s[3] * 2}')
|
||||||
else:
|
else:
|
||||||
return cls.parse(NAMED_COLORS[s])
|
return cls.parse(NAMED_COLORS[s])
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from babi.color import Color
|
|||||||
('#1e77d3', Color(0x1e, 0x77, 0xd3)),
|
('#1e77d3', Color(0x1e, 0x77, 0xd3)),
|
||||||
('white', Color(0xff, 0xff, 0xff)),
|
('white', Color(0xff, 0xff, 0xff)),
|
||||||
('black', Color(0x00, 0x00, 0x00)),
|
('black', Color(0x00, 0x00, 0x00)),
|
||||||
|
('#ccc', Color(0xcc, 0xcc, 0xcc)),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
def test_color_parse(s, expected):
|
def test_color_parse(s, expected):
|
||||||
|
|||||||
Reference in New Issue
Block a user