Example #1
0
def translate(filename: str,
              *,
              sleep_secs: float = .5,
              in_terminal=False,
              size=RC(16, 16),
              split=True,
              dir: Dir = Dir.right,
              n_iterations: int = None):
    """move right"""
    cm = ColorMatrix.from_filename(filename)
    color_map = _get_color_replacements(filename)
    if split:
        cm = cm.split()[0]

    mult = 1 if dir is Dir.right else -1

    def _gen_offset():
        its = count() if n_iterations is None else range(n_iterations)
        for _ in its:
            for _c_offset in range(cm.width - size.c):
                yield mult * (cm.width - _c_offset - 1)

    for c_offset in _gen_offset():
        cm.replace(color_map)
        cm.wrap = True
        set_cm(cm, offset=RC(0, c_offset), size=size, in_terminal=in_terminal)
        sleep(sleep_secs)
Example #2
0
 def to_color_matrix(self,
                     text: str,
                     color: Color = Colors.YELLOW,
                     height=16) -> ColorMatrix:
     im = self.to_image(text, color)
     width = int(im.width * height / im.height)
     im = im.resize((width, height), Image.ANTIALIAS)
     return ColorMatrix.from_image(
         im.resize((width, height), Image.ANTIALIAS))
Example #3
0
def _init_cm(
    cm_or_fn_or_bytes_or_image: CFBI
) -> Tuple[ColorMatrix, Dict[Color, Color]]:
    color_map = {}
    if isinstance(cm_or_fn_or_bytes_or_image, str):
        color_map = _get_color_replacements(cm_or_fn_or_bytes_or_image)
        cm_or_fn_or_bytes_or_image = Images.get_image(
            cm_or_fn_or_bytes_or_image)

    if isinstance(cm_or_fn_or_bytes_or_image, BytesIO):
        im = ColorMatrix.from_bytes(cm_or_fn_or_bytes_or_image)
    elif isinstance(cm_or_fn_or_bytes_or_image, Image.Image):
        im = ColorMatrix.from_image(cm_or_fn_or_bytes_or_image)
    elif isinstance(cm_or_fn_or_bytes_or_image, ColorMatrix):
        im = cm_or_fn_or_bytes_or_image
    else:
        raise TypeError(
            f'got {type(cm_or_fn_or_bytes_or_image)}, must be of type {CFBI}')

    return im, color_map
Example #4
0
def set_cm(cm: ColorMatrix,
           offset=RC(0, 0),
           size=RC(16, 16),
           *,
           in_terminal=False,
           with_mini=True,
           strip=True,
           verbose=True,
           duration_msec=0):
    """set color matrix either in terminal or on lights"""
    if strip:
        cm = cm.strip()

    cm = cm.get_range(RC(0, 0) + offset, size + offset)

    if in_terminal:
        print(cm.color_str)
        if verbose:
            print(cm.describe)
            print(cm.resize().color_str)
            print(cm.resize((4, 4)).color_str)
        return

    cm.set_max_brightness_pct(60)
    cm.replace({default_color: Color(1, 1, 100, 9000)})
    tiles = cm.to_tiles()

    idx_colors_map = {}
    for t_idx, t_cm in tiles.items():
        t_info = tile_map[t_idx]
        idx_colors_map[t_info.idx] = t_cm.flattened

    if with_mini:
        ti = tile_map[RC(2, -1)]
        idx_colors_map[ti.idx] = cm.resize(
            (8, 8)).rotate_from_origin(ti.origin).flattened

    tc = get_tile_chain()
    tc.set_tilechain_colors(idx_colors_map, duration=duration_msec)
Example #5
0
def animate(filename: str,
            *,
            center: bool = False,
            sleep_secs: float = .75,
            in_terminal=False,
            size=RC(16, 16),
            strip=True):
    """split color matrix and change images every `sleep_secs` seconds"""
    cm = ColorMatrix.from_filename(filename)
    color_map = _get_color_replacements(filename)
    for i, cm in enumerate(cycle(cm.split())):
        log.info('.')
        c_offset = 0 if not center else max(0, ceil(cm.width / 2 - 8))
        cm.replace(color_map)
        set_cm(cm,
               offset=RC(0, c_offset),
               size=size,
               in_terminal=in_terminal,
               strip=strip)
        sleep(sleep_secs)
Example #6
0
def _cm_test(c: Color) -> ColorMatrix:
    cm = ColorMatrix.from_shape(default_shape)
    cm[0, 0] = cm[0, 1] = cm[1, 0] = cm[1, 1] = c
    return cm
Example #7
0
 def cm(self) -> ColorMatrix:
     colors = [self._get_color(rc) for rc in RC(0, 0).to(self._shape)]
     return ColorMatrix.from_colors(colors, self._shape)