Skip to content

Commit b004e52

Browse files
authored
Improve SpriteList annotations + color_normalized validation (#2067)
* Annotate SpriteList.color_normalized setter * Annotate a few setters as returning None * Annotate SpriteList.__getitem__ * Use RGBANormalized instead of long Tuple return for SpriteList.color_normalized * Annotate and line-split SpriteList.draw_hit_boxes singature * Add None return * Make line_thickness a proper float instead of just an int * Split the line * Add color_normalized unpack validation * Remove no-op non-assert from SpriteList alpha_normalized tests * Add color_normalized setting tests
1 parent 8cc7450 commit b004e52

2 files changed

Lines changed: 30 additions & 11 deletions

File tree

arcade/sprite_list/sprite_list.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
gl,
3535
)
3636
from arcade.gl import Texture2D, Program
37-
from arcade.types import Color, RGBA255
37+
from arcade.types import Color, RGBA255, RGBOrANormalized, RGBANormalized
3838
from arcade.gl.types import OpenGlFilter, BlendFunction, PyGLenum
3939
from arcade.gl.buffer import Buffer
4040
from arcade.gl.vertex_array import Geometry
@@ -252,7 +252,7 @@ def __iter__(self) -> Iterator[SpriteType]:
252252
"""Return an iterable object of sprites."""
253253
return iter(self.sprite_list)
254254

255-
def __getitem__(self, i):
255+
def __getitem__(self, i: int) -> SpriteType:
256256
return self.sprite_list[i]
257257

258258
def __setitem__(self, index: int, sprite: SpriteType) -> None:
@@ -294,7 +294,7 @@ def visible(self) -> bool:
294294
return self._visible
295295

296296
@visible.setter
297-
def visible(self, value: bool):
297+
def visible(self, value: bool) -> None:
298298
self._visible = value
299299

300300
@property
@@ -325,20 +325,28 @@ def color(self) -> Color:
325325
return Color.from_normalized(self._color)
326326

327327
@color.setter
328-
def color(self, color: RGBA255):
328+
def color(self, color: RGBA255) -> None:
329329
self._color = Color.from_iterable(color).normalized
330330

331331
@property
332-
def color_normalized(self) -> Tuple[float, float, float, float]:
332+
def color_normalized(self) -> RGBANormalized:
333333
"""
334334
Get or set the spritelist color in normalized form (0.0 -> 1.0 floats).
335335
This property works the same as :py:attr:`~arcade.SpriteList.color`.
336336
"""
337337
return self._color
338338

339339
@color_normalized.setter
340-
def color_normalized(self, value):
341-
self._color = value
340+
def color_normalized(self, value: RGBOrANormalized) -> None:
341+
try:
342+
r, g, b, *_a = value
343+
assert len(_a) <= 1
344+
except (ValueError, AssertionError) as e:
345+
raise ValueError(
346+
"color_normalized must unpack as 3 or 4 float values"
347+
) from e
348+
349+
self._color = r, g, b, _a[0] if _a else 1.0
342350

343351
@property
344352
def alpha(self) -> int:
@@ -350,7 +358,7 @@ def alpha(self) -> int:
350358
return int(self._color[3] * 255)
351359

352360
@alpha.setter
353-
def alpha(self, value: int):
361+
def alpha(self, value: int) -> None:
354362
# value = clamp(value, 0, 255)
355363
self._color = self._color[0], self._color[1], self._color[2], value / 255
356364

@@ -367,7 +375,7 @@ def alpha_normalized(self) -> float:
367375
return self._color[3]
368376

369377
@alpha_normalized.setter
370-
def alpha_normalized(self, value: float):
378+
def alpha_normalized(self, value: float) -> None:
371379
# value = clamp(value, 0.0, 1.0)
372380
self._color = self._color[0], self._color[1], self._color[2], value
373381

@@ -1039,7 +1047,11 @@ def draw(
10391047
vertices=self._sprite_index_slots,
10401048
)
10411049

1042-
def draw_hit_boxes(self, color: RGBA255 = (0, 0, 0, 255), line_thickness: float = 1):
1050+
def draw_hit_boxes(
1051+
self,
1052+
color: RGBA255 = (0, 0, 0, 255),
1053+
line_thickness: float = 1.0
1054+
) -> None:
10431055
"""Draw all the hit boxes in this list"""
10441056
# NOTE: Find a way to efficiently draw this
10451057
for sprite in self.sprite_list:

tests/unit/spritelist/test_spritelist.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,14 @@ def test_color():
203203
sp.alpha = 172
204204
assert sp.alpha == 172
205205
assert sp.alpha_normalized == pytest.approx(172/255, rel=0.01)
206-
sp.alpha_normalized == 1.0
206+
207+
# Setting float RGBA works
208+
sp.color_normalized = 0.1, 0.2, 0.3, 0.4
209+
assert sp.color_normalized == pytest.approx((0.1, 0.2, 0.3, 0.4), rel=0.1)
210+
211+
# Setting float RGB works
212+
sp.color_normalized = 0.5, 0.6, 0.7
213+
assert sp.color_normalized == pytest.approx((0.5, 0.6, 0.7, 1.0), rel=0.1)
207214

208215
# Alpha Normalized
209216
sp.alpha_normalized = 0.5

0 commit comments

Comments
 (0)