Skip to content

Commit b2d558a

Browse files
committed
Bug/typo fix + test tweaks
1 parent aa5b5b0 commit b2d558a

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

arcade/sprite/base.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -352,17 +352,21 @@ def rgb(self) -> Tuple[int, int, int]:
352352
@rgb.setter
353353
def rgb(self, color: RGBOrA255):
354354

355-
# Fast validation by unpacking into channel values + unused
355+
# Fast validation of size by unpacking channel values
356356
try:
357-
r, g, b, *_ = color
358-
except ValueError as _:
359-
raise ValueError(
360-
f"{self.__class__.__name__},rgb takes 3 or 4 channels")
357+
r, g, b, *_a = color
358+
if len(_a) > 1: # Alpha's only used to validate here
359+
raise ValueError()
360+
361+
except ValueError as _: # It's always a length issue
362+
raise ValueError((
363+
f"{self.__class__.__name__},rgb takes 3 or 4 channel"
364+
f" colors, but got {len(color)} channels"))
361365

362366
# Unpack to avoid index / . overhead & prep for repack
363-
current_r, current_g, current_g, a = self._color
367+
current_r, current_b, current_g, a = self._color
364368

365-
# Early exit if equivalent
369+
# Do nothing if equivalent to current color
366370
if current_r == r and current_g == g and current_b == b:
367371
return
368372

tests/unit/sprite/test_sprite.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,22 @@ def test_visible():
336336
assert sprite.alpha == 100
337337

338338

339-
def test_sprite_rgb_property():
339+
def test_sprite_rgb_property_basics():
340340
sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png")
341341

342342
# Initial multiply tint is white
343343
assert sprite.rgb == (255, 255, 255)
344344

345+
# Values which are too short are not allowed
346+
with pytest.raises(ValueError):
347+
sprite.rgb = (1,2)
348+
with pytest.raises(ValueError):
349+
sprite.rgb = (0,)
350+
351+
# Nor are values which are too long
352+
with pytest.raises(ValueError):
353+
sprite.rgb = (100,100,100,100,100)
354+
345355
# Test color setting + .rgb report when .visible == True
346356
sprite.rgb = (1, 3, 5, 7)
347357
assert sprite.color.r == 1

0 commit comments

Comments
 (0)