]>
Commit | Line | Data |
---|---|---|
e0df8241 JR |
1 | from typing import NamedTuple, Tuple |
2 | ||
3 | ||
4 | class ColorTriplet(NamedTuple): | |
5 | """The red, green, and blue components of a color.""" | |
6 | ||
7 | red: int | |
8 | """Red component in 0 to 255 range.""" | |
9 | green: int | |
10 | """Green component in 0 to 255 range.""" | |
11 | blue: int | |
12 | """Blue component in 0 to 255 range.""" | |
13 | ||
14 | @property | |
15 | def hex(self) -> str: | |
16 | """get the color triplet in CSS style.""" | |
17 | red, green, blue = self | |
18 | return f"#{red:02x}{green:02x}{blue:02x}" | |
19 | ||
20 | @property | |
21 | def rgb(self) -> str: | |
22 | """The color in RGB format. | |
23 | ||
24 | Returns: | |
25 | str: An rgb color, e.g. ``"rgb(100,23,255)"``. | |
26 | """ | |
27 | red, green, blue = self | |
28 | return f"rgb({red},{green},{blue})" | |
29 | ||
30 | @property | |
31 | def normalized(self) -> Tuple[float, float, float]: | |
32 | """Convert components into floats between 0 and 1. | |
33 | ||
34 | Returns: | |
35 | Tuple[float, float, float]: A tuple of three normalized colour components. | |
36 | """ | |
37 | red, green, blue = self | |
38 | return red / 255.0, green / 255.0, blue / 255.0 |