Total Complexity | 5 |
Total Lines | 55 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | from typing import Union |
||
|
|||
2 | |||
3 | |||
4 | class Color: |
||
5 | """ |
||
6 | A color in RGB. |
||
7 | |||
8 | Parameters |
||
9 | --------- |
||
10 | c : Union[:class:`str`,:class:`int`] |
||
11 | The hex color can be a string that optionally starts with an ``#`` or |
||
12 | an int with the RGB values. |
||
13 | Attributes |
||
14 | --------- |
||
15 | r : :class:`int` |
||
16 | The red value for this color. |
||
17 | g : :class:`int` |
||
18 | The green value for this color. |
||
19 | b : :class:`int` |
||
20 | The blue value for this color. |
||
21 | """ |
||
22 | |||
23 | def __init__(self, c: Union[str, int]) -> None: |
||
24 | if isinstance(c, int): |
||
25 | self.r = c >> 16 & 255 |
||
26 | self.g = c >> 8 & 255 |
||
27 | self.b = c & 255 |
||
28 | return |
||
29 | |||
30 | # Conversion modified from this answer |
||
31 | # https://stackoverflow.com/a/29643643 |
||
32 | if c.startswith("#"): |
||
33 | c = c[1:] |
||
34 | self.r, self.g, self.b = (int(c[n:n + 2], 16) for n in (0, 2, 4)) |
||
35 | |||
36 | @property |
||
37 | def rbg(self): |
||
38 | """ |
||
39 | Returns |
||
40 | ------- |
||
41 | Tuple[:class:`int`, :class:`int` :class:`int`] |
||
42 | Tuple value for rgb. |
||
43 | """ |
||
44 | return self.r, self.g, self.b |
||
45 | |||
46 | @property |
||
47 | def hex(self): |
||
48 | """ |
||
49 | Returns |
||
50 | --------- |
||
51 | str |
||
52 | Str value for hex. |
||
53 | """ |
||
54 | return f"{self.r:02x}{self.g:02x}{self.b:02x}" |
||
55 |