Skip to content

Color

Color

Represents a color in RGB format.

Attributes:

Name Type Description
r int

Red channel.

g int

Green channel.

b int

Blue channel.

Source code in supervision/draw/color.py
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
@dataclass
class Color:
    """
    Represents a color in RGB format.

    Attributes:
        r (int): Red channel.
        g (int): Green channel.
        b (int): Blue channel.
    """

    r: int
    g: int
    b: int

    @classmethod
    def from_hex(cls, color_hex: str) -> Color:
        """
        Create a Color instance from a hex string.

        Args:
            color_hex (str): Hex string of the color.

        Returns:
            Color: Instance representing the color.

        Example:
            ```
            >>> Color.from_hex('#ff00ff')
            Color(r=255, g=0, b=255)
            ```
        """
        _validate_color_hex(color_hex)
        color_hex = color_hex.lstrip("#")
        if len(color_hex) == 3:
            color_hex = "".join(c * 2 for c in color_hex)
        r, g, b = (int(color_hex[i : i + 2], 16) for i in range(0, 6, 2))
        return cls(r, g, b)

    def as_rgb(self) -> Tuple[int, int, int]:
        """
        Returns the color as an RGB tuple.

        Returns:
            Tuple[int, int, int]: RGB tuple.

        Example:
            ```
            >>> color.as_rgb()
            (255, 0, 255)
            ```
        """
        return self.r, self.g, self.b

    def as_bgr(self) -> Tuple[int, int, int]:
        """
        Returns the color as a BGR tuple.

        Returns:
            Tuple[int, int, int]: BGR tuple.

        Example:
            ```
            >>> color.as_bgr()
            (255, 0, 255)
            ```
        """
        return self.b, self.g, self.r

    @classmethod
    def white(cls) -> Color:
        return Color.from_hex(color_hex="#ffffff")

    @classmethod
    def black(cls) -> Color:
        return Color.from_hex(color_hex="#000000")

    @classmethod
    def red(cls) -> Color:
        return Color.from_hex(color_hex="#ff0000")

    @classmethod
    def green(cls) -> Color:
        return Color.from_hex(color_hex="#00ff00")

    @classmethod
    def blue(cls) -> Color:
        return Color.from_hex(color_hex="#0000ff")

as_bgr()

Returns the color as a BGR tuple.

Returns:

Type Description
Tuple[int, int, int]

Tuple[int, int, int]: BGR tuple.

Example
>>> color.as_bgr()
(255, 0, 255)
Source code in supervision/draw/color.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def as_bgr(self) -> Tuple[int, int, int]:
    """
    Returns the color as a BGR tuple.

    Returns:
        Tuple[int, int, int]: BGR tuple.

    Example:
        ```
        >>> color.as_bgr()
        (255, 0, 255)
        ```
    """
    return self.b, self.g, self.r

as_rgb()

Returns the color as an RGB tuple.

Returns:

Type Description
Tuple[int, int, int]

Tuple[int, int, int]: RGB tuple.

Example
>>> color.as_rgb()
(255, 0, 255)
Source code in supervision/draw/color.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def as_rgb(self) -> Tuple[int, int, int]:
    """
    Returns the color as an RGB tuple.

    Returns:
        Tuple[int, int, int]: RGB tuple.

    Example:
        ```
        >>> color.as_rgb()
        (255, 0, 255)
        ```
    """
    return self.r, self.g, self.b

from_hex(color_hex) classmethod

Create a Color instance from a hex string.

Parameters:

Name Type Description Default
color_hex str

Hex string of the color.

required

Returns:

Name Type Description
Color Color

Instance representing the color.

Example
>>> Color.from_hex('#ff00ff')
Color(r=255, g=0, b=255)
Source code in supervision/draw/color.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@classmethod
def from_hex(cls, color_hex: str) -> Color:
    """
    Create a Color instance from a hex string.

    Args:
        color_hex (str): Hex string of the color.

    Returns:
        Color: Instance representing the color.

    Example:
        ```
        >>> Color.from_hex('#ff00ff')
        Color(r=255, g=0, b=255)
        ```
    """
    _validate_color_hex(color_hex)
    color_hex = color_hex.lstrip("#")
    if len(color_hex) == 3:
        color_hex = "".join(c * 2 for c in color_hex)
    r, g, b = (int(color_hex[i : i + 2], 16) for i in range(0, 6, 2))
    return cls(r, g, b)

ColorPalette

Source code in supervision/draw/color.py
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
@dataclass
class ColorPalette:
    colors: List[Color]

    @classmethod
    def default(cls) -> ColorPalette:
        """
        Returns a default color palette.

        Returns:
            ColorPalette: A ColorPalette instance with default colors.

        Example:
            ```
            >>> ColorPalette.default()
            ColorPalette(colors=[Color(r=255, g=0, b=0), Color(r=0, g=255, b=0), ...])
            ```
        """
        return ColorPalette.from_hex(color_hex_list=DEFAULT_COLOR_PALETTE)

    @classmethod
    def from_hex(cls, color_hex_list: List[str]) -> ColorPalette:
        """
        Create a ColorPalette instance from a list of hex strings.

        Args:
            color_hex_list (List[str]): List of color hex strings.

        Returns:
            ColorPalette: A ColorPalette instance.

        Example:
            ```
            >>> ColorPalette.from_hex(['#ff0000', '#00ff00', '#0000ff'])
            ColorPalette(colors=[Color(r=255, g=0, b=0), Color(r=0, g=255, b=0), ...])
            ```
        """
        colors = [Color.from_hex(color_hex) for color_hex in color_hex_list]
        return cls(colors)

    def by_idx(self, idx: int) -> Color:
        """
        Return the color at a given index in the palette.

        Args:
            idx (int): Index of the color in the palette.

        Returns:
            Color: Color at the given index.

        Example:
            ```
            >>> color_palette.by_idx(1)
            Color(r=0, g=255, b=0)
            ```
        """
        if idx < 0:
            raise ValueError("idx argument should not be negative")
        idx = idx % len(self.colors)
        return self.colors[idx]

by_idx(idx)

Return the color at a given index in the palette.

Parameters:

Name Type Description Default
idx int

Index of the color in the palette.

required

Returns:

Name Type Description
Color Color

Color at the given index.

Example
>>> color_palette.by_idx(1)
Color(r=0, g=255, b=0)
Source code in supervision/draw/color.py
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
def by_idx(self, idx: int) -> Color:
    """
    Return the color at a given index in the palette.

    Args:
        idx (int): Index of the color in the palette.

    Returns:
        Color: Color at the given index.

    Example:
        ```
        >>> color_palette.by_idx(1)
        Color(r=0, g=255, b=0)
        ```
    """
    if idx < 0:
        raise ValueError("idx argument should not be negative")
    idx = idx % len(self.colors)
    return self.colors[idx]

default() classmethod

Returns a default color palette.

Returns:

Name Type Description
ColorPalette ColorPalette

A ColorPalette instance with default colors.

Example
>>> ColorPalette.default()
ColorPalette(colors=[Color(r=255, g=0, b=0), Color(r=0, g=255, b=0), ...])
Source code in supervision/draw/color.py
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
@classmethod
def default(cls) -> ColorPalette:
    """
    Returns a default color palette.

    Returns:
        ColorPalette: A ColorPalette instance with default colors.

    Example:
        ```
        >>> ColorPalette.default()
        ColorPalette(colors=[Color(r=255, g=0, b=0), Color(r=0, g=255, b=0), ...])
        ```
    """
    return ColorPalette.from_hex(color_hex_list=DEFAULT_COLOR_PALETTE)

from_hex(color_hex_list) classmethod

Create a ColorPalette instance from a list of hex strings.

Parameters:

Name Type Description Default
color_hex_list List[str]

List of color hex strings.

required

Returns:

Name Type Description
ColorPalette ColorPalette

A ColorPalette instance.

Example
>>> ColorPalette.from_hex(['#ff0000', '#00ff00', '#0000ff'])
ColorPalette(colors=[Color(r=255, g=0, b=0), Color(r=0, g=255, b=0), ...])
Source code in supervision/draw/color.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
@classmethod
def from_hex(cls, color_hex_list: List[str]) -> ColorPalette:
    """
    Create a ColorPalette instance from a list of hex strings.

    Args:
        color_hex_list (List[str]): List of color hex strings.

    Returns:
        ColorPalette: A ColorPalette instance.

    Example:
        ```
        >>> ColorPalette.from_hex(['#ff0000', '#00ff00', '#0000ff'])
        ColorPalette(colors=[Color(r=255, g=0, b=0), Color(r=0, g=255, b=0), ...])
        ```
    """
    colors = [Color.from_hex(color_hex) for color_hex in color_hex_list]
    return cls(colors)