1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
"""Python Cookbook 2nd ed.
Chapter 4, recipe 5, Writing list-related type hints
"""
from typing import List, Tuple, Union
scheme = [
('Brick_Red', (198, 45, 66)),
('color1', (198.00, 100.50, 45.00)),
('color2', (198.00, 45.00, 142.50)),
]
RGB_I = Tuple[str, Tuple[int, int, int]]
RGB_F = Tuple[str, Tuple[float, float, float]]
ColorList = List[Union[RGB_I, RGB_F]]
def hexify(r: float, g: float, b: float) -> str:
"""
>>> hexify(198, 45, 66)
'#C62D42'
"""
return f'#{int(r)<<16 | int(g)<<8 | int(b):X}'
def source_to_hex(src: ColorList) -> List[Tuple[str, str]]:
return [
(n, hexify(*color)) for n, color in src
]
test_list = """
>>> scheme
[('Brick_Red', (198, 45, 66)), ('color1', (198.0, 100.5, 45.0)), ('color2', (198.0, 45.0, 142.5))]
>>> source_to_hex(scheme)
[('Brick_Red', '#C62D42'), ('color1', '#C6642D'), ('color2', '#C62D8E')]
"""
__test__ = {n: v for n, v in locals().items() if n.startswith("test_")}
|