def update(self, canvas): # count the number of neighbours canvas.clear() for x in range(0, canvas.width): for y in range(0, canvas.height): noise = int( snoise2((x + self.xoff) / self.freq, y / self.freq, self.octaves) * 127.0 + 128.0) if (noise > 200): canvas.draw_pixel(x, y, Color(1, 41, 95)) elif (noise > 170): canvas.draw_pixel(x, y, Color(67, 127, 151)) elif (noise > 140): canvas.draw_pixel(x, y, Color(132, 147, 36)) elif (noise > 110): canvas.draw_pixel(x, y, Color(255, 179, 15)) elif (noise > 80): canvas.draw_pixel(x, y, Color(253, 21, 27)) elif (noise > 50): canvas.draw_pixel(x, y, Color(181, 189, 104)) elif (noise > 20): canvas.draw_pixel(x, y, Color(245, 151, 78)) elif (noise > 0): canvas.draw_pixel(x, y, Color(241, 85, 65)) self.xoff += 1
def __init__(self, canvas, send_object, send_object_to_all, start_script, restart_self, set_frame_period, set_frame_rate, get_connected_clients): super().__init__(canvas, send_object, send_object_to_all, start_script, restart_self, set_frame_period, set_frame_rate, get_connected_clients) #self.set_frame_period(0.020) self.set_frame_rate(20) self.display_minute = True self.color_delta = None self.color = Color() self.last_time, self.text, self.x, self.y = None, None, None, None
class clock(CustomScript): def __init__(self, canvas, send_object, send_object_to_all, start_script, restart_self, set_frame_period, set_frame_rate, get_connected_clients): super().__init__(canvas, send_object, send_object_to_all, start_script, restart_self, set_frame_period, set_frame_rate, get_connected_clients) #self.set_frame_period(0.020) self.set_frame_rate(20) self.display_minute = True self.color_delta = None self.color = Color() self.last_time, self.text, self.x, self.y = None, None, None, None def draw(self, canvas: Canvas): canvas.draw_text(self.text, self.x, self.y, self.color) def __set_text(self, text, canvas): self.text = canvas.render_text(text, size=12) self.x = 0 # (canvas.width - self.text.width) // 2 self.y = 1 # (canvas.height - self.text.height) // 2 def __set_minute_or_hour(self, current_time, canvas): canvas.clear() if self.display_minute: self.__set_text("%02i" % current_time.minute, canvas) else: self.__set_text("%02i" % current_time.hour, canvas) #sleep(0.3) def update(self, canvas): time = datetime.datetime.now().time() if self.last_time is None or time.minute != self.last_time.minute: self.last_time = time self.__set_minute_or_hour(time, canvas) if self.color.is_black(): self.display_minute = not self.display_minute self.__set_minute_or_hour(time, canvas) self.color_delta = 0.02 elif self.color.is_white(): self.color_delta = -0.02 self.color.change_rgb(lambda r, g, b: ( r + self.color_delta, g + self.color_delta, b + self.color_delta))
def update(self, canvas): force_switch = self._read_force_switch() if force_switch != self._last_force_switch_status: self._light_percentage = -0.2 self.logger.info("Reset light percentage") self._last_force_switch_status = force_switch if self._light_percentage < 0: light_percentage = 0 elif self._light_percentage > 1: light_percentage = 1 else: light_percentage = self._light_percentage color_temp = 1400 + (5500 - 1400) * light_percentage color_value = Color.from_temperature(color_temp, light_percentage) self.logger.info("Color temp is {} from light percentage {}".format( color_temp, light_percentage)) self.current_color = color_value self._light_percentage += 0.005 if self._light_percentage > 1.2: self.logger.info("Reset light percentage, was above 1.5") self._light_percentage = -0.2
def update(self, canvas): # count the number of neighbours canvas.clear() for y in range(0, 25): canvas.draw_pixel( randint(0, 9), randint(0, 9), Color(randint(0, 255), randint(0, 255), randint(0, 255)))
def __write_color_at(self, x, y, color: Color): """ Write a color at a specified position in the matrix :param x: x position of pixel; counted from zero beginning on the left, must be smaller than the canvas width :param y: y position of pixel; y is zero for the top row of pixels, must be smaller than the canvas height :param color: the color to be written to the pixel :return: nothing """ red_index = self.get_red_index(x, y) # could be a one-liner, but i think avoiding the tuples involved is slightly faster #self.data_buffer[red_index:red_index + 3] = color.get_rgb() self.data_buffer[red_index] = color.get_red() self.data_buffer[red_index + 1] = color.get_green() self.data_buffer[red_index + 2] = color.get_blue()
def clear(self, color: Color = Color(0, 0, 0)): """ Set all pixels to some color :param color: the color that should be applied """ # convert from 0-1 normalized to 0-255 rgb_color = color.get_rgb() # should be faster than manually zeroing all entries if rgb_color == [0, 0, 0]: self.data_buffer = bytearray(self.buffer_length) # write the color code to all leds else: for i in range(self.led_count): self.data_buffer[3 * i:3 * i + 3] = rgb_color
def update(self, canvas): if ((self.count % 3) == 0): canvas.clear(Color(255, 255, 255)) self.count += 1 else: canvas.clear() self.count += 1
def randomize_colors(words: List[Word], config_file_path: str): # generate n = mWords.size() evenly spaced hue values available_hues = [] step_size = 1 / len(words) offset = random.random() * step_size for i in range(len(words)): available_hues.append(step_size * i + offset) # shuffle to avoid putting similar colors next to each other random.shuffle(available_hues) # random start offset to avoid always starting at blue i = random.randrange(len(words)) for word in words: # choose next hue value hue = available_hues[i % len(words)] # curse you, python, for not allowing ++ i += 1 # randomize value a little to further reduce similarity of output word.rectangle.color = Color.from_hsv(hue, 1, 0.5 + random.random() * 0.5) save_color_info(config_file_path, get_color_config(words))
def test_canvas_font(): print(inspect.currentframe().f_code.co_name) c = Canvas(10, 10, 0) c.set_font("helvetica.otf", 13) blue = Color(0, 0, 255) c.draw_text("h", 0, 0, blue) print(repr(c))
def draw(self, canvas: Canvas): if self.client_data is not None: for x in range(0, canvas.width): for y in range(0, canvas.height): color = Color(self.client_data[x][y][0], self.client_data[x][y][1], self.client_data[x][y][2]) canvas.draw_pixel(x, y, color)
def __parse_rect(rectangle_list: Dict): return Rect( rectangle_list.get("x", 0), rectangle_list["y"], rectangle_list["width"], rectangle_list.get("height", 1), Color(255, 255, 255) )
def draw(self, canvas): self.frame += 1 if self.frame == 3: self.frame = 0 for i in range(0, 2): canvas.draw_pixel( randint(0, canvas.width - 1), 0, Color(randint(0, 255), randint(0, 255), randint(0, 255)))
def read_color_config_file(config_file_path, words: List[Word]): logger = logging.getLogger("scripts:wordclock:colorlogic") try: with open(config_file_path, "r", encoding="utf-8") as in_file: color_config = json.load(in_file) update_color_info(color_config, words) except FileNotFoundError: logger.info( "no wordclock color config found at {}. first start?".format( config_file_path)) set_color(Color.from_rgb((255, 255, 255)), words) except JSONDecodeError: logger.error( "bad wordclock color config at {}! re-send from app...".format( config_file_path)) set_color(Color.from_rgb((255, 255, 255)), words)
def test_canvas_rect(): print(inspect.currentframe().f_code.co_name) c = Canvas(10, 10, 0) blue = Color(0, 0, 255) c.draw_rect(2, 2, 4, 4, blue) print(repr(c))
def draw(self, canvas: Canvas): for x in range(0, canvas.width): for y in range(0, canvas.height): if self.game_board[x][y] is True: c = Color(self.colorboard[x][y][0], self.colorboard[x][y][1], self.colorboard[x][y][2]) canvas.draw_pixel(x, y, c)
def test_canvas_pixel_line(): print(inspect.currentframe().f_code.co_name) c = Canvas(10, 10, 0) blue = Color(0, 0, 255) for i in range(c.width - 1, 0 - 1, -1): c.draw_pixel(9 - i, i, blue) print(repr(c))
def _clear_properties(self): self.current_color = Color(0, 0, 0) # type: Color self.timezone = None # pytz.timezone("Europe/Berlin") self.wake_time = None # datetime.now(tz=self.timezone).replace(hour=20, minute=10) # type: datetime self.blend_in_duration = None # timedelta(minutes=30) # type: timedelta self.time_delta = 0 # type: int self.lower_color_temperature = 1800 # type: int self.upper_color_temperature = 2800 # type: int self.test_color_temperature = None # type: Optional[int] self.enable_color_temp_test = False # type: bool
def test_canvas_draw_pixel_line(serial): print(inspect.currentframe().f_code.co_name) c = Canvas(156, 1, 0) blue = Color(0, 0, 255) while True: for i in range(c.width): c.draw_pixel(i, 0, blue) serial.update(c.get_buffer_for_arduino())
def update(self, canvas): if self.enable_color_temp_test: self.current_color = Color.from_temperature( self.test_color_temperature, 1) return force_switch = GPIO.input(17) == GPIO.LOW # self.logger.debug("Wakeuplight force-on is {}".format(force_switch)) if force_switch: self.current_color = Color.from_temperature(3000, .75) self._was_forced = True return elif self._was_forced and not force_switch: self._was_forced = False self.current_color = Color(0, 0, 0) return if self.wake_time is None or self.blend_in_duration is None: return now = datetime.now(tz=self.timezone) + timedelta( minutes=self.time_delta) # self.time_delta += 1 if self.wake_time - self.blend_in_duration <= now <= self.wake_time + self.blend_in_duration: # blend-in phase light_percentage = 1 if now < self.wake_time: light_percentage = 1 - ( (self.wake_time - now).total_seconds() / self.blend_in_duration.total_seconds()) # convert to uint8 color_temp = self.lower_color_temperature + ( self.upper_color_temperature - self.lower_color_temperature) * light_percentage color_value = Color.from_temperature(color_temp, light_percentage) # self.logger.info("{} at {}".format(color_value, now)) # apply self.current_color = color_value else: self.current_color = Color(0, 0, 0)
def get_color(self, x, y) -> Color: """ Get a Color instance describing the color of the led at x,y :param x: x position of pixel; counted from zero beginning on the left, must be smaller than the canvas width :param y: y position of pixel; y is zero for the top row of pixels, must be smaller than the canvas height :return: a Color instance """ red_index = self.get_red_index(x, y) return Color(self.data_buffer[red_index], self.data_buffer[red_index + 1], self.data_buffer[red_index + 2])
def on_data(self, data_dictionary, source_id): command = data_dictionary['command'] if command == "change_text": self.text_scroller.set_text(data_dictionary['text']) elif command == "change_color": self.text_scroller.set_color(Color.from_rgb(data_dictionary['color'])) elif command == "change_speed": speed = data_dictionary['speed'] self.set_frame_rate((speed + 1) * 2) elif command == "set_font_size": self.text_scroller.set_size(data_dictionary['size']) else: print("unknown command")
def __init__(self, canvas, text: str = None, font_path="Inconsolata.otf", font_size: int = 10): self.current_x = 0 self.current_y = 0 self.current_color = Color(255, 255, 255) self.canvas = canvas self.font_path = font_path self.font_size = font_size self.current_text_width = None self.rendered_text = None self.text = text self.re_render = text is not None
def test_gui_canvas_display_by_line(): print(inspect.currentframe().f_code.co_name) print("should print blue line bottom left to top right") try: from MatrixGraphicalDisplay import MatrixGraphicalDisplay c = Canvas(15, 10, 0) gui = MatrixGraphicalDisplay(15, 10, 0) blue = Color(0, 0, 255) for i in range(c.width - 1, 0 - 1 + (c.width - c.height), -1): c.draw_pixel(c.width - 1 - i, i - (c.width - c.height), blue) gui.update_with_canvas(c) time.sleep(.4) except ImportError: print("could not import tkinter, probably")
def test_gui_canvas_display_pixel_counter_up(): print(inspect.currentframe().f_code.co_name) print("should print blue line from pixel 0 to 41") try: from MatrixGraphicalDisplay import MatrixGraphicalDisplay c = Canvas(42, 1, 0) gui = MatrixGraphicalDisplay(42, 1, 0, test_gui_canvas_display_by_line) blue = Color(0, 0, 255) for i in range(42): c.draw_pixel(i, 0, blue) gui.update_with_canvas(c) time.sleep(0.6) except ImportError: print("could not import tkinter, probably")
def __get_repr_color(color: Color) -> str: """ Returns r(ed), g(reen), b(lue), .(lack), w(hite), matching the largest color component that the given color has. :param color: the color of which we want to get the largest component :return: r, g, b, . or w """ rgb = color.get_rgb() # white and black have their own representations if rgb == (0, 0, 0): return '.' if rgb == (1, 1, 1): return 'w' # return the color with the greatest part largest_color_index = rgb.index(max(rgb)) if largest_color_index == 0: return 'r' elif largest_color_index == 1: return 'g' return 'b'
def draw(self, canvas: Canvas): if self.enable: canvas.clear(Color(255, 255, 255)) else: canvas.clear()
def draw(self, canvas): for x in range(0, 10): canvas.draw_pixel(x, x, Color(255, 255, 255)) canvas.draw_pixel(4, 7, Color(255, 255, 255))
def test_canvas_line(): print(inspect.currentframe().f_code.co_name) c = Canvas(10, 10, 0) c.draw_line(0, 0, 9, 5, Color()) print(repr(c))
def update_color_info(color_array: Dict, words: List[Word]): for color_info in color_array: words[color_info["id"]].rectangle.color = Color.from_aarrggbb_int( color_info["clr"])