Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions arcade/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
MAX_UINT24 = 0xFFFFFF
MAX_UINT32 = 0xFFFFFFFF

MASK_RGBA_R = 0xFF000000
MASK_RGBA_G = 0x00FF0000
MASK_RGBA_B = 0x0000FF00
MASK_RGBA_A = 0x000000FF

MASK_RGB_R = 0xFF0000
MASK_RGB_G = 0x00FF00
MASK_RGB_B = 0x0000FF

ChannelType = TypeVar('ChannelType')

RGB = Tuple[ChannelType, ChannelType, ChannelType]
Expand Down Expand Up @@ -408,14 +417,15 @@ def random(
:param b: Fixed value for blue channel
:param a: Fixed value for alpha channel
"""
rand = random.randint(0, MAX_UINT32)
if r is None:
r = random.randint(0, 255)
r = (rand & MASK_RGBA_R) >> 24
if g is None:
g = random.randint(0, 255)
g = (rand & MASK_RGBA_G) >> 16
if b is None:
b = random.randint(0, 255)
b = (rand & MASK_RGBA_B) >> 8
if a is None:
a = random.randint(0, 255)
a = (rand & MASK_RGBA_A)

return cls(r, g, b, a)

Expand Down
9 changes: 8 additions & 1 deletion tests/unit/color/test_color_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,14 @@ def test_deepcopy_color_inheritance():

@pytest.fixture
def randint_is_constant(monkeypatch):
monkeypatch.setattr('random.randint', Mock(return_value=RANDINT_RETURN_RESULT))
"""
Replace the randomized color uint32 with a known signal value.

Since the color randomization masks out channels, we use a known
repeated value (128, or 0x80 in hex) to represent a channel fetched
from random rather than taken from user input.
"""
monkeypatch.setattr('random.randint', Mock(return_value=0x80808080))


def test_color_random(randint_is_constant):
Expand Down