dynamic_background.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import random
  2. class DynamicBackground():
  3. def __init__(self):
  4. self.current = get_valid_color()
  5. self.target = get_valid_color()
  6. def draw_background(self, surface):
  7. same = True
  8. for x in range(0, 3):
  9. if self.current[x] > self.target[x]:
  10. self.current[x] -= 1
  11. elif self.current[x] < self.target[x]:
  12. self.current[x] += 1
  13. if self.current != self.target:
  14. same = False
  15. #if same:
  16. # self.target = get_valid_color()
  17. surface.fill(self.current)
  18. def set_target_color(self, color):
  19. self.target = [color[0], color[1], color[2]]
  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