Total Complexity | 4 |
Total Lines | 50 |
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 in the format ``#NNNNNN`` or an int with the RGB values. |
||
12 | Attributes |
||
13 | r : :class:`int` |
||
14 | The red value for this color. |
||
15 | g : :class:`int` |
||
16 | The green value for this color. |
||
17 | b : :class:`int` |
||
18 | The blue value for this color. |
||
19 | """ |
||
20 | |||
21 | def __init__(self, c: Union[str, int]) -> None: |
||
22 | if isinstance(c, int): |
||
23 | self.r = c >> 16 & 255 |
||
24 | self.g = c >> 8 & 255 |
||
25 | self.b = c & 255 |
||
26 | return |
||
27 | |||
28 | # Conversion modified from this answer |
||
29 | # https://stackoverflow.com/a/29643643 |
||
30 | self.r, self.g, self.b = (int(c[n + 1:n + 3], 16) for n in (0, 2, 4)) |
||
31 | |||
32 | @property |
||
33 | def rbg(self): |
||
34 | """ |
||
35 | Returns |
||
36 | ------- |
||
37 | Tuple[:class:`int`, :class:`int` :class:`int`] |
||
38 | Tuple value for rgb. |
||
39 | """ |
||
40 | return self.r, self.g, self.b |
||
41 | |||
42 | @property |
||
43 | def hex(self): |
||
44 | """ |
||
45 | Returns |
||
46 | str |
||
47 | Str value for hex. |
||
48 | """ |
||
49 | return f"{hex(self.r)[2:]}{hex(self.g)[2:]}{hex(self.b)[2:]}" |
||
50 |