@@ -1,5 +1,9 @@
|
||||
from typing import NamedTuple
|
||||
|
||||
# TODO: find a standard which defines these
|
||||
# limited number of "named" colors
|
||||
NAMED_COLORS = {'white': '#ffffff', 'black': '#000000'}
|
||||
|
||||
|
||||
class Color(NamedTuple):
|
||||
r: int
|
||||
@@ -8,4 +12,7 @@ class Color(NamedTuple):
|
||||
|
||||
@classmethod
|
||||
def parse(cls, s: str) -> 'Color':
|
||||
return cls(r=int(s[1:3], 16), g=int(s[3:5], 16), b=int(s[5:7], 16))
|
||||
if s.startswith('#'):
|
||||
return cls(r=int(s[1:3], 16), g=int(s[3:5], 16), b=int(s[5:7], 16))
|
||||
else:
|
||||
return cls.parse(NAMED_COLORS[s])
|
||||
|
||||
15
tests/color_test.py
Normal file
15
tests/color_test.py
Normal file
@@ -0,0 +1,15 @@
|
||||
import pytest
|
||||
|
||||
from babi.color import Color
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
('s', 'expected'),
|
||||
(
|
||||
('#1e77d3', Color(0x1e, 0x77, 0xd3)),
|
||||
('white', Color(0xff, 0xff, 0xff)),
|
||||
('black', Color(0x00, 0x00, 0x00)),
|
||||
),
|
||||
)
|
||||
def test_color_parse(s, expected):
|
||||
assert Color.parse(s) == expected
|
||||
Reference in New Issue
Block a user