dynamic_background.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import random
  2. import logging
  3. logger = logging.getLogger(__name__)
  4. class DynamicBackground():
  5. def __init__(self):
  6. self.current = get_valid_color()
  7. self.target = get_valid_color()
  8. def draw_background(self, surface):
  9. same = True
  10. for x in range(0, 3):
  11. if self.current[x] > self.target[x]:
  12. self.current[x] -= 1
  13. elif self.current[x] < self.target[x]:
  14. self.current[x] += 1
  15. if self.current != self.target:
  16. same = False
  17. if same:
  18. self.target = get_valid_color()
  19. surface.fill(self.current)
  20. # Returns an array with 3 integers in range of 0-255
  21. # The sum of the three integers will be lower than 255*2 (510) to avoid very bright colors
  22. # White text should be seen ok with this background color
  23. def get_valid_color():
  24. color = [0, 0, 0]
  25. total = 0
  26. for i in range(0, 3):
  27. color[i] = random.randint(0, 255)
  28. total += color[i]
  29. extra = total - 510
  30. if extra > 0:
  31. i = random.randint(0, 2)
  32. color[i] -= extra
  33. return color