From f9d69ab1b2b90ac5e6941a925ad5d2870839e206 Mon Sep 17 00:00:00 2001 From: Ander <9and3r@gmail.com> Date: Mon, 22 Dec 2014 18:04:33 +0100 Subject: [PATCH] Dynamic background again --- mopidy_touchscreen/dynamic_background.py | 39 ++++++++++++++++++++++++ mopidy_touchscreen/screen_manager.py | 5 +-- 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 mopidy_touchscreen/dynamic_background.py diff --git a/mopidy_touchscreen/dynamic_background.py b/mopidy_touchscreen/dynamic_background.py new file mode 100644 index 0000000..250e99f --- /dev/null +++ b/mopidy_touchscreen/dynamic_background.py @@ -0,0 +1,39 @@ +import random + + +class DynamicBackground(): + def __init__(self): + self.current = get_valid_color() + self.target = get_valid_color() + + def draw_background(self, surface): + same = True + for x in range(0, 3): + if self.current[x] > self.target[x]: + self.current[x] -= 1 + elif self.current[x] < self.target[x]: + self.current[x] += 1 + if self.current != self.target: + same = False + if same: + self.target = get_valid_color() + surface.fill(self.current) + + +# Returns an array with 3 integers in range of 0-255 +# The sum of the three integers will be lower than 255*2 +# (510) to avoid very bright colors +# White text should be seen ok with this background color + + +def get_valid_color(): + color = [0, 0, 0] + total = 0 + for i in range(0, 3): + color[i] = random.randint(0, 255) + total += color[i] + extra = total - 510 + if extra > 0: + i = random.randint(0, 2) + color[i] -= extra + return color \ No newline at end of file diff --git a/mopidy_touchscreen/screen_manager.py b/mopidy_touchscreen/screen_manager.py index 7f5f1b9..e3c13f5 100644 --- a/mopidy_touchscreen/screen_manager.py +++ b/mopidy_touchscreen/screen_manager.py @@ -12,7 +12,7 @@ from .screen_objects import ScreenObjectsManager, \ TouchAndTextItem from .input_manager import InputManager from .tracklist import Tracklist - +from .dynamic_background import DynamicBackground logger = logging.getLogger(__name__) @@ -29,6 +29,7 @@ class ScreenManager(): self.size = size self.core = core self.fonts = {} + self.background = DynamicBackground() self.current_screen = 0 self.base_size = self.size[1] / 8 font = resource_filename( @@ -112,7 +113,7 @@ class ScreenManager(): def update(self): surface = pygame.Surface(self.size) - surface.fill([200, 200, 200]) + self.background.draw_background(surface) self.screens[self.current_screen].update(surface, self.screen_changed) surface.blit(self.down_bar, (0, self.base_size * 7))