Skip to content

Commit 4e4fad9

Browse files
authored
Merge pull request #756 from roboflow/q1-2024/color-properties
feat(color.py): ✨ add color constants/properties
2 parents d503e3a + 38bdeff commit 4e4fad9

File tree

7 files changed

+67
-18
lines changed

7 files changed

+67
-18
lines changed

supervision/annotators/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ class LabelAnnotator:
783783
def __init__(
784784
self,
785785
color: Union[Color, ColorPalette] = ColorPalette.default(),
786-
text_color: Color = Color.black(),
786+
text_color: Color = Color.BLACK,
787787
text_scale: float = 0.5,
788788
text_thickness: int = 1,
789789
text_padding: int = 10,

supervision/detection/annotate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(
2828
self,
2929
color: Union[Color, ColorPalette] = ColorPalette.default(),
3030
thickness: int = 2,
31-
text_color: Color = Color.black(),
31+
text_color: Color = Color.BLACK,
3232
text_scale: float = 0.5,
3333
text_thickness: int = 1,
3434
text_padding: int = 10,

supervision/detection/line_counter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ class LineZoneAnnotator:
166166
def __init__(
167167
self,
168168
thickness: float = 2,
169-
color: Color = Color.white(),
169+
color: Color = Color.WHITE,
170170
text_thickness: float = 2,
171-
text_color: Color = Color.black(),
171+
text_color: Color = Color.BLACK,
172172
text_scale: float = 0.5,
173173
text_offset: float = 1.5,
174174
text_padding: int = 10,

supervision/detection/tools/polygon_zone.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def __init__(
9090
zone: PolygonZone,
9191
color: Color,
9292
thickness: int = 2,
93-
text_color: Color = Color.black(),
93+
text_color: Color = Color.BLACK,
9494
text_scale: float = 0.5,
9595
text_thickness: int = 1,
9696
text_padding: int = 10,

supervision/draw/color.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from dataclasses import dataclass
44
from typing import List, Tuple
55

6+
from supervision.utils.internal import deprecated
7+
68
DEFAULT_COLOR_PALETTE = [
79
"#a351fb",
810
"#e6194b",
@@ -116,23 +118,63 @@ def as_bgr(self) -> Tuple[int, int, int]:
116118
"""
117119
return self.b, self.g, self.r
118120

121+
@property
122+
def WHITE(cls):
123+
return cls.from_hex("#ffffff")
124+
125+
@property
126+
def BLACK(cls):
127+
return cls.from_hex("#000000")
128+
129+
@property
130+
def RED(cls):
131+
return cls.from_hex("#ff0000")
132+
133+
@property
134+
def GREEN(cls):
135+
return cls.from_hex("#00ff00")
136+
137+
@property
138+
def BLUE(cls):
139+
return cls.from_hex("#0000ff")
140+
119141
@classmethod
142+
@deprecated(
143+
"`Color.white()` is deprecated and will be removed in "
144+
"`supervision-0.20.0`. Use `Color.WHITE` instead."
145+
)
120146
def white(cls) -> Color:
121147
return Color.from_hex(color_hex="#ffffff")
122148

123149
@classmethod
150+
@deprecated(
151+
"`Color.black()` is deprecated and will be removed in "
152+
"`supervision-0.20.0`. Use `Color.BLACK` instead."
153+
)
124154
def black(cls) -> Color:
125155
return Color.from_hex(color_hex="#000000")
126156

127157
@classmethod
158+
@deprecated(
159+
"`Color.red()` is deprecated and will be removed in "
160+
"`supervision-0.20.0`. Use `Color.RED` instead."
161+
)
128162
def red(cls) -> Color:
129163
return Color.from_hex(color_hex="#ff0000")
130164

131165
@classmethod
166+
@deprecated(
167+
"`Color.green()` is deprecated and will be removed in "
168+
"`supervision-0.20.0`. Use `Color.GREEN` instead."
169+
)
132170
def green(cls) -> Color:
133171
return Color.from_hex(color_hex="#00ff00")
134172

135173
@classmethod
174+
@deprecated(
175+
"`Color.blue()` is deprecated and will be removed in "
176+
"`supervision-0.20.0`. Use `Color.BLUE` instead."
177+
)
136178
def blue(cls) -> Color:
137179
return Color.from_hex(color_hex="#0000ff")
138180

@@ -197,3 +239,10 @@ def by_idx(self, idx: int) -> Color:
197239
raise ValueError("idx argument should not be negative")
198240
idx = idx % len(self.colors)
199241
return self.colors[idx]
242+
243+
244+
Color.WHITE = Color.from_hex("#ffffff")
245+
Color.BLACK = Color.from_hex("#000000")
246+
Color.RED = Color.from_hex("#ff0000")
247+
Color.GREEN = Color.from_hex("#00ff00")
248+
Color.BLUE = Color.from_hex("#0000ff")

supervision/draw/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def draw_text(
105105
scene: np.ndarray,
106106
text: str,
107107
text_anchor: Point,
108-
text_color: Color = Color.black(),
108+
text_color: Color = Color.BLACK,
109109
text_scale: float = 0.5,
110110
text_thickness: int = 1,
111111
text_padding: int = 10,

test/draw/test_color.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
@pytest.mark.parametrize(
1010
"color_hex, expected_result, exception",
1111
[
12-
("fff", Color.white(), DoesNotRaise()),
13-
("#fff", Color.white(), DoesNotRaise()),
14-
("ffffff", Color.white(), DoesNotRaise()),
15-
("#ffffff", Color.white(), DoesNotRaise()),
16-
("f00", Color.red(), DoesNotRaise()),
17-
("0f0", Color.green(), DoesNotRaise()),
18-
("00f", Color.blue(), DoesNotRaise()),
12+
("fff", Color.WHITE, DoesNotRaise()),
13+
("#fff", Color.WHITE, DoesNotRaise()),
14+
("ffffff", Color.WHITE, DoesNotRaise()),
15+
("#ffffff", Color.WHITE, DoesNotRaise()),
16+
("f00", Color.RED, DoesNotRaise()),
17+
("0f0", Color.GREEN, DoesNotRaise()),
18+
("00f", Color.BLUE, DoesNotRaise()),
1919
("#808000", Color(r=128, g=128, b=0), DoesNotRaise()),
2020
("", None, pytest.raises(ValueError)),
2121
("00", None, pytest.raises(ValueError)),
@@ -35,11 +35,11 @@ def test_color_from_hex(
3535
@pytest.mark.parametrize(
3636
"color, expected_result, exception",
3737
[
38-
(Color.white(), "#ffffff", DoesNotRaise()),
39-
(Color.black(), "#000000", DoesNotRaise()),
40-
(Color.red(), "#ff0000", DoesNotRaise()),
41-
(Color.green(), "#00ff00", DoesNotRaise()),
42-
(Color.blue(), "#0000ff", DoesNotRaise()),
38+
(Color.WHITE, "#ffffff", DoesNotRaise()),
39+
(Color.BLACK, "#000000", DoesNotRaise()),
40+
(Color.RED, "#ff0000", DoesNotRaise()),
41+
(Color.GREEN, "#00ff00", DoesNotRaise()),
42+
(Color.BLUE, "#0000ff", DoesNotRaise()),
4343
(Color(r=128, g=128, b=0), "#808000", DoesNotRaise()),
4444
],
4545
)

0 commit comments

Comments
 (0)