Skip to content

Commit

Permalink
Merge pull request #149 from szabopeter/random-color
Browse files Browse the repository at this point in the history
Add the possibility to generate a random color in the color editor
  • Loading branch information
arnauddupuis committed Nov 1, 2020
2 parents 47a5449 + d012f27 commit 512a866
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pgl-editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def custom_color_picker():
print("5/2 - Increase/decrease the green value")
print("6/3 - Increase/decrease the blue value")
print(f"+/- - Increase/decrease the increment step (current: {step})")
print("r - Randomize values")
key = game.get_key()
if key == "1" and spr.bg_color.r >= step:
spr.bg_color.r -= step
Expand All @@ -154,6 +155,8 @@ def custom_color_picker():
step -= 1
elif key == "+":
step += 1
elif key == "r":
spr.bg_color.randomize()
return spr


Expand Down
16 changes: 16 additions & 0 deletions pygamelib/gfx/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pygamelib import constants
from pygamelib.assets import graphics
from pygamelib.functions import pgl_isinstance
import random
import time
from collections import UserDict
from uuid import uuid4
Expand Down Expand Up @@ -281,6 +282,21 @@ def load(cls, data):
)
return cls(data["red"], data["green"], data["blue"])

def randomize(self):
"""Set a random value for each component
:returns: None
:rtype: NoneType
Example::
color = Color()
color.randomize()
"""
self.r = random.randrange(256)
self.g = random.randrange(256)
self.b = random.randrange(256)


class Sprixel(object):

Expand Down
15 changes: 15 additions & 0 deletions tests/test_gfx_core_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ def test_color_load(self):
with self.assertRaises(gfx_core.base.PglInvalidTypeException):
gfx_core.Color.load({"red": 25, "green": "nope"})

def test_color_randomize(self):
# Let's keep this simple: most often than not,
# the randomized value should be different from the default one
# and the previous one
default_color = gfx_core.Color()
c = gfx_core.Color()
criteria_match_count = 0
total_rolls = 100
for i in range(total_rolls):
previous_color = gfx_core.Color(c.r, c.g, c.b)
c.randomize()
if c != default_color and c != previous_color:
criteria_match_count += 1
self.assertGreater(criteria_match_count, total_rolls // 2)


if __name__ == "__main__":
unittest.main()

0 comments on commit 512a866

Please sign in to comment.