Skip to content

Color

Color

Represents a color in RGB format.

This class provides methods to work with colors, including creating colors from hex codes, converting colors to hex strings, RGB tuples, and BGR tuples.

Attributes:

Name Type Description
r int

Red channel value (0-255).

g int

Green channel value (0-255).

b int

Blue channel value (0-255).

Example
import supervision as sv

sv.Color.WHITE
# Color(r=255, g=255, b=255)
Constant Hex Code RGB
WHITE #FFFFFF (255, 255, 255)
BLACK #000000 (0, 0, 0)
RED #FF0000 (255, 0, 0)
GREEN #00FF00 (0, 255, 0)
BLUE #0000FF (0, 0, 255)
YELLOW #FFFF00 (255, 255, 0)
ROBOFLOW #A351FB (163, 81, 251)
Source code in supervision/draw/color.py
 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
123
124
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
@dataclass
class Color:
    """
    Represents a color in RGB format.

    This class provides methods to work with colors, including creating colors from hex
    codes, converting colors to hex strings, RGB tuples, and BGR tuples.

    Attributes:
        r (int): Red channel value (0-255).
        g (int): Green channel value (0-255).
        b (int): Blue channel value (0-255).

    Example:
        ```python
        import supervision as sv

        sv.Color.WHITE
        # Color(r=255, g=255, b=255)
        ```

    | Constant   | Hex Code   | RGB              |
    |------------|------------|------------------|
    | `WHITE`    | `#FFFFFF`  | `(255, 255, 255)`|
    | `BLACK`    | `#000000`  | `(0, 0, 0)`      |
    | `RED`      | `#FF0000`  | `(255, 0, 0)`    |
    | `GREEN`    | `#00FF00`  | `(0, 255, 0)`    |
    | `BLUE`     | `#0000FF`  | `(0, 0, 255)`    |
    | `YELLOW`   | `#FFFF00`  | `(255, 255, 0)`  |
    | `ROBOFLOW` | `#A351FB`  | `(163, 81, 251)` |
    """

    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:
            ```python
            import supervision as sv

            sv.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_hex(self) -> str:
        """
        Converts the Color instance to a hex string.

        Returns:
            str: The hexadecimal color string.

        Example:
            ```python
            import supervision as sv

            sv.Color(r=255, g=255, b=0).as_hex()
            # '#ffff00'
            ```
        """
        return f"#{self.r:02x}{self.g:02x}{self.b:02x}"

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

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

        Example:
            ```python
            import supervision as sv

            sv.Color(r=255, g=255, b=0).as_rgb()
            # (255, 255, 0)
            ```
        """
        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:
            ```python
            import supervision as sv

            sv.Color(r=255, g=255, b=0).as_bgr()
            # (0, 255, 255)
            ```
        """
        return self.b, self.g, self.r

    @classproperty
    def WHITE(cls):
        return Color.from_hex("#FFFFFF")

    @classproperty
    def BLACK(cls):
        return Color.from_hex("#000000")

    @classproperty
    def RED(cls):
        return Color.from_hex("#FF0000")

    @classproperty
    def GREEN(cls):
        return Color.from_hex("#00FF00")

    @classproperty
    def BLUE(cls):
        return Color.from_hex("#0000FF")

    @classproperty
    def YELLOW(cls):
        return Color.from_hex("#FFFF00")

    @classproperty
    def ROBOFLOW(cls):
        return Color.from_hex("#A351FB")

    @classmethod
    @deprecated(
        "`Color.white()` is deprecated and will be removed in "
        "`supervision-0.20.0`. Use `Color.WHITE` instead."
    )
    def white(cls) -> Color:
        return Color.from_hex(color_hex="#ffffff")

    @classmethod
    @deprecated(
        "`Color.black()` is deprecated and will be removed in "
        "`supervision-0.20.0`. Use `Color.BLACK` instead."
    )
    def black(cls) -> Color:
        return Color.from_hex(color_hex="#000000")

    @classmethod
    @deprecated(
        "`Color.red()` is deprecated and will be removed in "
        "`supervision-0.20.0`. Use `Color.RED` instead."
    )
    def red(cls) -> Color:
        return Color.from_hex(color_hex="#ff0000")

    @classmethod
    @deprecated(
        "`Color.green()` is deprecated and will be removed in "
        "`supervision-0.20.0`. Use `Color.GREEN` instead."
    )
    def green(cls) -> Color:
        return Color.from_hex(color_hex="#00ff00")

    @classmethod
    @deprecated(
        "`Color.blue()` is deprecated and will be removed in "
        "`supervision-0.20.0`. Use `Color.BLUE` instead."
    )
    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
import supervision as sv

sv.Color(r=255, g=255, b=0).as_bgr()
# (0, 255, 255)
Source code in supervision/draw/color.py
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
def as_bgr(self) -> Tuple[int, int, int]:
    """
    Returns the color as a BGR tuple.

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

    Example:
        ```python
        import supervision as sv

        sv.Color(r=255, g=255, b=0).as_bgr()
        # (0, 255, 255)
        ```
    """
    return self.b, self.g, self.r

as_hex()

Converts the Color instance to a hex string.

Returns:

Name Type Description
str str

The hexadecimal color string.

Example
import supervision as sv

sv.Color(r=255, g=255, b=0).as_hex()
# '#ffff00'
Source code in supervision/draw/color.py
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def as_hex(self) -> str:
    """
    Converts the Color instance to a hex string.

    Returns:
        str: The hexadecimal color string.

    Example:
        ```python
        import supervision as sv

        sv.Color(r=255, g=255, b=0).as_hex()
        # '#ffff00'
        ```
    """
    return f"#{self.r:02x}{self.g:02x}{self.b:02x}"

as_rgb()

Returns the color as an RGB tuple.

Returns:

Type Description
Tuple[int, int, int]

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

Example
import supervision as sv

sv.Color(r=255, g=255, b=0).as_rgb()
# (255, 255, 0)
Source code in supervision/draw/color.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def as_rgb(self) -> Tuple[int, int, int]:
    """
    Returns the color as an RGB tuple.

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

    Example:
        ```python
        import supervision as sv

        sv.Color(r=255, g=255, b=0).as_rgb()
        # (255, 255, 0)
        ```
    """
    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
import supervision as sv

sv.Color.from_hex('#ff00ff')
# Color(r=255, g=0, b=255)
Source code in supervision/draw/color.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
@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:
        ```python
        import supervision as sv

        sv.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
@dataclass
class ColorPalette:
    colors: List[Color]

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

        Returns:
            ColorPalette: A ColorPalette instance with default colors.

        Example:
            ```python
            import supervision as sv

            sv.ColorPalette.DEFAULT
            # ColorPalette(colors=[Color(r=255, g=64, b=64), Color(r=255, g=161, b=160), ...])
            ```

        ![default-color-palette](https://media.roboflow.com/
        supervision-annotator-examples/default-color-palette.png)
        """  # noqa: E501 // docs
        return ColorPalette.from_hex(color_hex_list=DEFAULT_COLOR_PALETTE)

    @classproperty
    def ROBOFLOW(cls) -> ColorPalette:
        """
        Returns a Roboflow color palette.

        Returns:
            ColorPalette: A ColorPalette instance with Roboflow colors.

        Example:
            ```python
            import supervision as sv

            sv.ColorPalette.ROBOFLOW
            # ColorPalette(colors=[Color(r=194, g=141, b=252), Color(r=163, g=81, b=251), ...])
            ```

        ![roboflow-color-palette](https://media.roboflow.com/
        supervision-annotator-examples/roboflow-color-palette.png)
        """  # noqa: E501 // docs
        return ColorPalette.from_hex(color_hex_list=ROBOFLOW_COLOR_PALETTE)

    @classproperty
    def LEGACY(cls) -> ColorPalette:
        return ColorPalette.from_hex(color_hex_list=LEGACY_COLOR_PALETTE)

    @classmethod
    @deprecated(
        "`ColorPalette.default()` is deprecated and will be removed in "
        "`supervision-0.20.0`. Use `Color.DEFAULT` instead."
    )
    def default(cls) -> ColorPalette:
        """
        Returns a default color palette.

        Returns:
            ColorPalette: A ColorPalette instance with default colors.

        Example:
            ```python
            import supervision as sv

            sv.ColorPalette.default()
            # ColorPalette(colors=[Color(r=255, g=64, b=64), Color(r=255, g=161, b=160), ...])
            ```
        """  # noqa: E501 // docs
        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:
            ```python
            import supervision as sv

            sv.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)

    @classmethod
    def from_matplotlib(cls, palette_name: str, color_count: int) -> ColorPalette:
        """
        Create a ColorPalette instance from a Matplotlib color palette.

        Args:
            palette_name (str): Name of the Matplotlib palette.
            color_count (int): Number of colors to sample from the palette.

        Returns:
            ColorPalette: A ColorPalette instance.

        Example:
            ```python
            import supervision as sv

            sv.ColorPalette.from_matplotlib('viridis', 5)
            # ColorPalette(colors=[Color(r=68, g=1, b=84), Color(r=59, g=82, b=139), ...])
            ```

        ![visualized_color_palette](https://media.roboflow.com/
        supervision-annotator-examples/visualized_color_palette.png)
        """  # noqa: E501 // docs
        mpl_palette = plt.get_cmap(palette_name, color_count)
        colors = [
            Color(int(r * 255), int(g * 255), int(b * 255))
            for r, g, b, _ in mpl_palette.colors
        ]
        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:
            ```python
            import supervision as sv

            color_palette = sv.ColorPalette.from_hex(['#ff0000', '#00ff00', '#0000ff'])
            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()

Returns a default color palette.

Returns:

Name Type Description
ColorPalette ColorPalette

A ColorPalette instance with default colors.

Example
import supervision as sv

sv.ColorPalette.DEFAULT
# ColorPalette(colors=[Color(r=255, g=64, b=64), Color(r=255, g=161, b=160), ...])

default-color-palette

Source code in supervision/draw/color.py
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
@classproperty
def DEFAULT(cls) -> ColorPalette:
    """
    Returns a default color palette.

    Returns:
        ColorPalette: A ColorPalette instance with default colors.

    Example:
        ```python
        import supervision as sv

        sv.ColorPalette.DEFAULT
        # ColorPalette(colors=[Color(r=255, g=64, b=64), Color(r=255, g=161, b=160), ...])
        ```

    ![default-color-palette](https://media.roboflow.com/
    supervision-annotator-examples/default-color-palette.png)
    """  # noqa: E501 // docs
    return ColorPalette.from_hex(color_hex_list=DEFAULT_COLOR_PALETTE)

ROBOFLOW()

Returns a Roboflow color palette.

Returns:

Name Type Description
ColorPalette ColorPalette

A ColorPalette instance with Roboflow colors.

Example
import supervision as sv

sv.ColorPalette.ROBOFLOW
# ColorPalette(colors=[Color(r=194, g=141, b=252), Color(r=163, g=81, b=251), ...])

roboflow-color-palette

Source code in supervision/draw/color.py
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
@classproperty
def ROBOFLOW(cls) -> ColorPalette:
    """
    Returns a Roboflow color palette.

    Returns:
        ColorPalette: A ColorPalette instance with Roboflow colors.

    Example:
        ```python
        import supervision as sv

        sv.ColorPalette.ROBOFLOW
        # ColorPalette(colors=[Color(r=194, g=141, b=252), Color(r=163, g=81, b=251), ...])
        ```

    ![roboflow-color-palette](https://media.roboflow.com/
    supervision-annotator-examples/roboflow-color-palette.png)
    """  # noqa: E501 // docs
    return ColorPalette.from_hex(color_hex_list=ROBOFLOW_COLOR_PALETTE)

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
import supervision as sv

color_palette = sv.ColorPalette.from_hex(['#ff0000', '#00ff00', '#0000ff'])
color_palette.by_idx(1)
# Color(r=0, g=255, b=0)
Source code in supervision/draw/color.py
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
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:
        ```python
        import supervision as sv

        color_palette = sv.ColorPalette.from_hex(['#ff0000', '#00ff00', '#0000ff'])
        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
import supervision as sv

sv.ColorPalette.default()
# ColorPalette(colors=[Color(r=255, g=64, b=64), Color(r=255, g=161, b=160), ...])
Source code in supervision/draw/color.py
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
@classmethod
@deprecated(
    "`ColorPalette.default()` is deprecated and will be removed in "
    "`supervision-0.20.0`. Use `Color.DEFAULT` instead."
)
def default(cls) -> ColorPalette:
    """
    Returns a default color palette.

    Returns:
        ColorPalette: A ColorPalette instance with default colors.

    Example:
        ```python
        import supervision as sv

        sv.ColorPalette.default()
        # ColorPalette(colors=[Color(r=255, g=64, b=64), Color(r=255, g=161, b=160), ...])
        ```
    """  # noqa: E501 // docs
    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
import supervision as sv

sv.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
@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:
        ```python
        import supervision as sv

        sv.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)

from_matplotlib(palette_name, color_count) classmethod

Create a ColorPalette instance from a Matplotlib color palette.

Parameters:

Name Type Description Default
palette_name str

Name of the Matplotlib palette.

required
color_count int

Number of colors to sample from the palette.

required

Returns:

Name Type Description
ColorPalette ColorPalette

A ColorPalette instance.

Example
import supervision as sv

sv.ColorPalette.from_matplotlib('viridis', 5)
# ColorPalette(colors=[Color(r=68, g=1, b=84), Color(r=59, g=82, b=139), ...])

visualized_color_palette

Source code in supervision/draw/color.py
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
@classmethod
def from_matplotlib(cls, palette_name: str, color_count: int) -> ColorPalette:
    """
    Create a ColorPalette instance from a Matplotlib color palette.

    Args:
        palette_name (str): Name of the Matplotlib palette.
        color_count (int): Number of colors to sample from the palette.

    Returns:
        ColorPalette: A ColorPalette instance.

    Example:
        ```python
        import supervision as sv

        sv.ColorPalette.from_matplotlib('viridis', 5)
        # ColorPalette(colors=[Color(r=68, g=1, b=84), Color(r=59, g=82, b=139), ...])
        ```

    ![visualized_color_palette](https://media.roboflow.com/
    supervision-annotator-examples/visualized_color_palette.png)
    """  # noqa: E501 // docs
    mpl_palette = plt.get_cmap(palette_name, color_count)
    colors = [
        Color(int(r * 255), int(g * 255), int(b * 255))
        for r, g, b, _ in mpl_palette.colors
    ]
    return cls(colors)

Comments