2018-08-16 06:05:28 +00:00
|
|
|
import numpy as np
|
2018-07-16 06:38:57 +00:00
|
|
|
import pytest
|
2018-08-16 06:05:28 +00:00
|
|
|
from matplotlib.colors import to_rgb
|
2018-07-16 06:38:57 +00:00
|
|
|
|
2018-12-20 10:31:55 +00:00
|
|
|
from corrscope.channel import ChannelConfig
|
|
|
|
from corrscope.outputs import RGB_DEPTH
|
|
|
|
from corrscope.renderer import RendererConfig, MatplotlibRenderer
|
|
|
|
from corrscope.layout import LayoutConfig
|
2018-07-16 06:38:57 +00:00
|
|
|
|
|
|
|
WIDTH = 640
|
|
|
|
HEIGHT = 360
|
|
|
|
|
2018-08-16 06:05:28 +00:00
|
|
|
ALL_ZEROS = np.array([0,0])
|
|
|
|
|
2018-08-17 07:00:36 +00:00
|
|
|
all_colors = pytest.mark.parametrize('bg_str,fg_str', [
|
2018-08-16 06:05:28 +00:00
|
|
|
('#000000', '#ffffff'),
|
|
|
|
('#ffffff', '#000000'),
|
|
|
|
('#0000aa', '#aaaa00'),
|
|
|
|
('#aaaa00', '#0000aa'),
|
|
|
|
])
|
2018-08-17 07:00:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
@all_colors
|
|
|
|
def test_default_colors(bg_str, fg_str):
|
|
|
|
""" Test the default background/foreground colors. """
|
2018-08-16 06:05:28 +00:00
|
|
|
cfg = RendererConfig(
|
|
|
|
WIDTH,
|
|
|
|
HEIGHT,
|
|
|
|
bg_color=bg_str,
|
|
|
|
init_line_color=fg_str,
|
|
|
|
)
|
|
|
|
lcfg = LayoutConfig()
|
|
|
|
nplots = 1
|
|
|
|
|
2018-11-05 23:45:38 +00:00
|
|
|
r = MatplotlibRenderer(cfg, lcfg, nplots, None)
|
2018-08-17 07:00:36 +00:00
|
|
|
verify(r, bg_str, fg_str)
|
|
|
|
|
|
|
|
# Ensure default ChannelConfig(line_color=None) does not override line color
|
|
|
|
chan = ChannelConfig(wav_path='')
|
2018-11-05 23:45:38 +00:00
|
|
|
channels = [chan] * nplots
|
|
|
|
r = MatplotlibRenderer(cfg, lcfg, nplots, channels)
|
2018-08-17 07:00:36 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
chan = ChannelConfig(wav_path='', line_color=fg_str)
|
2018-11-05 23:45:38 +00:00
|
|
|
channels = [chan] * nplots
|
|
|
|
r = MatplotlibRenderer(cfg, lcfg, nplots, channels)
|
2018-08-17 07:00:36 +00:00
|
|
|
verify(r, bg_str, fg_str)
|
|
|
|
|
|
|
|
|
|
|
|
def verify(r: MatplotlibRenderer, bg_str, fg_str):
|
2018-08-16 06:05:28 +00:00
|
|
|
r.render_frame([ALL_ZEROS])
|
|
|
|
frame_colors: np.ndarray = \
|
|
|
|
np.frombuffer(r.get_frame(), dtype=np.uint8).reshape((-1, RGB_DEPTH))
|
|
|
|
|
|
|
|
bg_u8 = [round(c*255) for c in to_rgb(bg_str)]
|
|
|
|
fg_u8 = [round(c*255) for c in to_rgb(fg_str)]
|
|
|
|
|
|
|
|
# Ensure background is correct
|
|
|
|
assert (frame_colors[0] == bg_u8).all()
|
|
|
|
|
|
|
|
# Ensure foreground is present
|
2018-11-05 23:30:06 +00:00
|
|
|
assert np.prod(frame_colors == fg_u8, axis=-1).any(), \
|
|
|
|
'incorrect foreground, it might be 136 = #888888'
|
2018-08-16 06:05:28 +00:00
|
|
|
|
|
|
|
assert (np.amax(frame_colors, axis=0) == np.maximum(bg_u8, fg_u8)).all()
|
|
|
|
assert (np.amin(frame_colors, axis=0) == np.minimum(bg_u8, fg_u8)).all()
|