dynamic_background.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import logging
  2. import random
  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
  22. # (510) to avoid very bright colors
  23. # White text should be seen ok with this background color
  24. def get_valid_color():
  25. color = [0, 0, 0]
  26. total = 0
  27. for i in range(0, 3):
  28. color[i] = random.randint(0, 255)
  29. total += color[i]
  30. extra = total - 510
  31. if extra > 0:
  32. i = random.randint(0, 2)
  33. color[i] -= extra
  34. return color