From 3324ed15688386738490b15a46bb44f96c4557ff Mon Sep 17 00:00:00 2001 From: nyanpasu64 <nyanpasu64@tuta.io> Date: Fri, 17 Aug 2018 00:00:36 -0700 Subject: [PATCH] [renderer] Test default colors, and channel line-color overrides --- ovgenpy/renderer.py | 5 +++++ tests/test_renderer.py | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/ovgenpy/renderer.py b/ovgenpy/renderer.py index c89945e..2548384 100644 --- a/ovgenpy/renderer.py +++ b/ovgenpy/renderer.py @@ -119,6 +119,11 @@ class MatplotlibRenderer: raise ValueError( f"cannot assign {len(channel_cfgs)} colors to {self.nplots} plots" ) + + if self._lines is not None: + raise ValueError( + f'cannot set line colors after calling render_frame()' + ) self._line_colors = [cfg.line_color for cfg in channel_cfgs] def render_frame(self, datas: List[np.ndarray]) -> None: diff --git a/tests/test_renderer.py b/tests/test_renderer.py index b739a6f..a6f7b1a 100644 --- a/tests/test_renderer.py +++ b/tests/test_renderer.py @@ -2,6 +2,7 @@ import numpy as np import pytest from matplotlib.colors import to_rgb +from ovgenpy.channel import ChannelConfig from ovgenpy.outputs import RGB_DEPTH from ovgenpy.renderer import RendererConfig, MatplotlibRenderer, LayoutConfig, \ RendererLayout @@ -93,25 +94,55 @@ def test_renderer(): ALL_ZEROS = np.array([0,0]) -@pytest.mark.parametrize('bg_str,fg_str', [ +all_colors = pytest.mark.parametrize('bg_str,fg_str', [ ('#000000', '#ffffff'), ('#ffffff', '#000000'), ('#0000aa', '#aaaa00'), ('#aaaa00', '#0000aa'), ]) -def test_colors(bg_str, fg_str): - """ Ensure the rendered background/foreground colors are correct. """ + + +@all_colors +def test_default_colors(bg_str, fg_str): + """ Test the default background/foreground colors. """ cfg = RendererConfig( WIDTH, HEIGHT, bg_color=bg_str, init_line_color=fg_str, - # line_width=5, ) lcfg = LayoutConfig() nplots = 1 r = MatplotlibRenderer(cfg, lcfg, nplots) + verify(r, bg_str, fg_str) + + # Ensure default ChannelConfig(line_color=None) does not override line color + r = MatplotlibRenderer(cfg, lcfg, nplots) + chan = ChannelConfig(wav_path='') + r.set_colors([chan] * nplots) + verify(r, bg_str, fg_str) + + +@all_colors +def test_line_colors(bg_str, fg_str): + """ Test channel-specific line color overrides """ + cfg = RendererConfig( + WIDTH, + HEIGHT, + bg_color=bg_str, + init_line_color='#888888', + ) + lcfg = LayoutConfig() + nplots = 1 + + r = MatplotlibRenderer(cfg, lcfg, nplots) + chan = ChannelConfig(wav_path='', line_color=fg_str) + r.set_colors([chan] * nplots) + verify(r, bg_str, fg_str) + + +def verify(r: MatplotlibRenderer, bg_str, fg_str): r.render_frame([ALL_ZEROS]) frame_colors: np.ndarray = \ np.frombuffer(r.get_frame(), dtype=np.uint8).reshape((-1, RGB_DEPTH))