Passed
Pull Request — main (#235)
by
unknown
01:38
created

pincer.utils.color   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 19
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Color.rbg() 0 9 1
A Color.hex() 0 9 1
A Color.__init__() 0 12 3
1
from typing import Union
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
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))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable n does not seem to be defined.
Loading history...
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