async def set_led(self, rgb: RGB = RGB(0, 0, 0)): """ change color of torso's led default to turning led off """ rgb.validate() return await self._post('led', rgb.json)
async def flash(colors: Union[RGB, List[RGB]], images: Union[str, List[str]], on_time_secs=.1, off_time_secs=.02, flashlight=False): """rapidly change display image, led color, and flashlight""" async def _set(c, i, t, on_off, force=False): if t <= 0: return coros = [asyncio.sleep(t)] if c: print(c) coros.append(api.images.set_led(c)) if i and (on_off or force): coros.append(api.images.display(i)) if flashlight: coros.append(api.system.set_flashlight(on_off)) await asyncio.gather(*coros) try: for c, i in zip(cycle(always_iterable(colors, base_type=tuple)), cycle(always_iterable(images))): await _set(c, i, on_time_secs, True) await _set(RGB(0, 0, 0), i, off_time_secs, False) finally: await asyncio.shield(_set(RGB(0, 0, 0), 'e_DefaultContent.jpg', off_time_secs, False, True))
def resize(self, shape: Shape = (8, 8)) -> 'ColorMatrix': """resize image using pillow and return a new ColorMatrix""" if self.shape == shape: return self.copy() cm = self im = Image.new('RGB', cm.shape, 'black') pixels = im.load() for c, r in product(range(im.width), range(im.height)): with suppress(IndexError): pixels[c, r] = cm[r][c] y, x = shape im = im.resize((x, y), Image.ANTIALIAS) pixels = im.load() res = type(self).from_shape(shape) for c, r in product(range(im.width), range(im.height)): with suppress(IndexError): res[r][c] = pixels[c, r] return res.cast(lambda rgb: RGB(*rgb))
print(t) # print(json_obj()) # print(json_obj(dict(a=5), b=6)) # print(json_obj([4, 5])) print(HeadSettings(yaw=40).json) sys.exit api = MistyAPI('localhost:9999') uvloop.install() mws = MistyWS(api) mws2 = MistyWS(api) print(mws is mws2) print(RGB.from_hex(0xFFFFFF).hex) class C: def __init__(self): self.t = asyncio.run(self.atest()) print(self.t) async def atest(self): print('in') await asyncio.sleep(1) print('done') return 4 async def run():
class Colors(metaclass=ColorsMeta): RED = RGB(255, 0, 0) ORANGE = RGB.from_hex(0xffa500) YELLOW = RGB(255, 255, 0) GREEN = RGB(0, 255, 0) CYAN = RGB(0, 255, 255) BLUE = RGB(0, 0, 255) PURPLE = RGB(128, 0, 128) MAGENTA = RGB.from_hex(0xff00ff) PINK = RGB.from_hex(0xffc0cb) BROWN = RGB.from_hex(0xa0522d) COPILOT_BLUE = RGB.from_hex(0x00b4e3) COPILOT_BLUE_GREEN = RGB.from_hex(0x00827d) COPILOT_BLUE_GREY = RGB.from_hex(0x386e8f) COPILOT_DARK_BLUE = RGB.from_hex(0x193849) HANUKKAH_BLUE = RGB.from_hex(0x09239b) MARIO_BLUE = RGB.from_hex(0x049cd8) MARIO_YELLOW = RGB.from_hex(0xfbd000) MARIO_RED = RGB.from_hex(0xe52521) MARIO_GREEN = RGB.from_hex(0x43b047) PYTHON_LIGHT_BLUE = RGB.from_hex(0x4b8bbe) PYTHON_DARK_BLUE = RGB.from_hex(0x306998) PYTHON_LIGHT_YELLOW = RGB.from_hex(0xffe873) PYTHON_DARK_YELLOW = RGB.from_hex(0xffd43b) PYTHON_GREY = RGB.from_hex(0x646464) SNES_BLACK = RGB.from_hex(0x211a21) SNES_DARK_GREY = RGB.from_hex(0x908a99) SNES_DARK_PURPLE = RGB.from_hex(0x4f43ae) SNES_LIGHT_GREY = RGB.from_hex(0xcec9cc) SNES_LIGHT_PURPLE = RGB.from_hex(0xb5b6e4) STEELERS_BLACK = RGB.from_hex(0x101820) STEELERS_BLUE = RGB.from_hex(0x00539b) STEELERS_GOLD = RGB.from_hex(0xffb612) STEELERS_RED = RGB.from_hex(0xc60c30) STEELERS_SILVER = RGB.from_hex(0xa5acaf) XMAS_GOLD = RGB.from_hex(0xe5d08f) XMAS_GREEN = RGB.from_hex(0x18802b) XMAS_RED = RGB.from_hex(0xd42426) YALE_BLUE = RGB.from_hex(0xf4d92)
def from_filename(cls, fn) -> 'ColorMatrix': """read a png in using pillow and convert to ColorMatrix""" im = Image.open(fn).convert('RGB') px = im.load() return cls([RGB(*px[c, r]).color for c in range(im.width)] for r in range(im.height))
from contextlib import suppress from functools import lru_cache from itertools import islice, cycle, groupby, product, starmap from types import SimpleNamespace from typing import List, NamedTuple, Tuple, Dict, Optional, Callable, Iterable, Set, Union, Any from PIL import Image # from lifxlan import RGB, Color, Colors, init_log, timer # from routines import colors_to_theme, ColorTheme from misty_py.utils import RGB __author__ = 'acushner' default_shape = 8, 8 default_color = RGB(0, 0, 0) Shape = Tuple[int, int] class RC(NamedTuple): """represent row/column coords""" r: int c: int def to(self, other): """range from self to other""" yield from (RC(r, c) for r in range(self.r, other.r) for c in range(self.c, other.c)) def in_bounds(self, rc_ul, rc_lr) -> bool: """return True if self inside the bounds of [upper_left, lower_right)"""