2019-01-12 10:45:48 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
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
|
2019-01-12 10:45:48 +00:00
|
|
|
from corrscope.layout import LayoutConfig
|
2018-12-20 10:31:55 +00:00
|
|
|
from corrscope.outputs import RGB_DEPTH
|
|
|
|
from corrscope.renderer import RendererConfig, MatplotlibRenderer
|
2018-07-16 06:38:57 +00:00
|
|
|
|
|
|
|
WIDTH = 640
|
|
|
|
HEIGHT = 360
|
|
|
|
|
2019-01-03 08:57:30 +00:00
|
|
|
ALL_ZEROS = np.array([0, 0])
|
2018-08-16 06:05:28 +00:00
|
|
|
|
2019-01-03 08:57:30 +00:00
|
|
|
all_colors = pytest.mark.parametrize(
|
2019-01-12 10:45:48 +00:00
|
|
|
"bg_str,fg_str,grid_str",
|
2019-01-03 08:57:30 +00:00
|
|
|
[
|
2019-01-12 10:45:48 +00:00
|
|
|
("#000000", "#ffffff", None),
|
|
|
|
("#ffffff", "#000000", None),
|
|
|
|
("#0000aa", "#aaaa00", None),
|
|
|
|
("#aaaa00", "#0000aa", None),
|
|
|
|
# Enabling gridlines enables Axes rectangles.
|
|
|
|
# Make sure they don't draw *over* the global figure background.
|
|
|
|
("#0000aa", "#aaaa00", "#ff00ff"), # beautiful magenta gridlines
|
|
|
|
("#aaaa00", "#0000aa", "#ff00ff"),
|
2019-01-03 08:57:30 +00:00
|
|
|
],
|
|
|
|
)
|
2018-08-17 07:00:36 +00:00
|
|
|
|
2019-01-12 10:45:48 +00:00
|
|
|
nplots = 2
|
|
|
|
|
2018-08-17 07:00:36 +00:00
|
|
|
|
|
|
|
@all_colors
|
2019-01-12 10:45:48 +00:00
|
|
|
def test_default_colors(bg_str, fg_str, grid_str):
|
2018-08-17 07:00:36 +00:00
|
|
|
""" Test the default background/foreground colors. """
|
2019-01-12 10:45:48 +00:00
|
|
|
cfg = RendererConfig(
|
2019-02-06 05:57:22 +00:00
|
|
|
WIDTH,
|
|
|
|
HEIGHT,
|
|
|
|
bg_color=bg_str,
|
|
|
|
init_line_color=fg_str,
|
|
|
|
grid_color=grid_str,
|
|
|
|
line_width=2.0,
|
2019-02-16 15:50:38 +00:00
|
|
|
antialiasing=False,
|
2019-01-12 10:45:48 +00:00
|
|
|
)
|
2018-08-16 06:05:28 +00:00
|
|
|
lcfg = LayoutConfig()
|
|
|
|
|
2018-11-05 23:45:38 +00:00
|
|
|
r = MatplotlibRenderer(cfg, lcfg, nplots, None)
|
2019-01-12 10:45:48 +00:00
|
|
|
verify(r, bg_str, fg_str, grid_str)
|
2018-08-17 07:00:36 +00:00
|
|
|
|
|
|
|
# Ensure default ChannelConfig(line_color=None) does not override line color
|
2019-01-03 08:57:30 +00:00
|
|
|
chan = ChannelConfig(wav_path="")
|
2018-11-05 23:45:38 +00:00
|
|
|
channels = [chan] * nplots
|
|
|
|
r = MatplotlibRenderer(cfg, lcfg, nplots, channels)
|
2019-01-12 10:45:48 +00:00
|
|
|
verify(r, bg_str, fg_str, grid_str)
|
2018-08-17 07:00:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
@all_colors
|
2019-01-12 10:45:48 +00:00
|
|
|
def test_line_colors(bg_str, fg_str, grid_str):
|
2018-08-17 07:00:36 +00:00
|
|
|
""" Test channel-specific line color overrides """
|
2019-01-12 10:45:48 +00:00
|
|
|
cfg = RendererConfig(
|
2019-02-06 05:57:22 +00:00
|
|
|
WIDTH,
|
|
|
|
HEIGHT,
|
|
|
|
bg_color=bg_str,
|
|
|
|
init_line_color="#888888",
|
|
|
|
grid_color=grid_str,
|
|
|
|
line_width=2.0,
|
2019-02-16 15:50:38 +00:00
|
|
|
antialiasing=False,
|
2019-01-12 10:45:48 +00:00
|
|
|
)
|
2018-08-17 07:00:36 +00:00
|
|
|
lcfg = LayoutConfig()
|
|
|
|
|
2019-01-03 08:57:30 +00:00
|
|
|
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)
|
2019-01-12 10:45:48 +00:00
|
|
|
verify(r, bg_str, fg_str, grid_str)
|
2018-08-17 07:00:36 +00:00
|
|
|
|
|
|
|
|
2019-01-12 10:45:48 +00:00
|
|
|
def verify(r: MatplotlibRenderer, bg_str, fg_str, grid_str: Optional[str]):
|
|
|
|
r.render_frame([ALL_ZEROS] * nplots)
|
2019-01-03 08:57:30 +00:00
|
|
|
frame_colors: np.ndarray = np.frombuffer(r.get_frame(), dtype=np.uint8).reshape(
|
|
|
|
(-1, RGB_DEPTH)
|
|
|
|
)
|
2018-08-16 06:05:28 +00:00
|
|
|
|
2019-01-03 08:57:30 +00:00
|
|
|
bg_u8 = [round(c * 255) for c in to_rgb(bg_str)]
|
|
|
|
fg_u8 = [round(c * 255) for c in to_rgb(fg_str)]
|
2019-01-12 10:45:48 +00:00
|
|
|
all_colors = [bg_u8, fg_u8]
|
|
|
|
|
|
|
|
if grid_str:
|
|
|
|
grid_u8 = [round(c * 255) for c in to_rgb(grid_str)]
|
|
|
|
all_colors.append(grid_u8)
|
2018-08-16 06:05:28 +00:00
|
|
|
|
|
|
|
# Ensure background is correct
|
2019-01-12 10:45:48 +00:00
|
|
|
bg_frame = frame_colors[0]
|
|
|
|
assert (
|
|
|
|
bg_frame == bg_u8
|
|
|
|
).all(), f"incorrect background, it might be grid_str={grid_str}"
|
2018-08-16 06:05:28 +00:00
|
|
|
|
|
|
|
# Ensure foreground is present
|
2019-01-03 08:57:30 +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
|
|
|
|
2019-01-12 10:45:48 +00:00
|
|
|
# Ensure grid color is present
|
|
|
|
if grid_str:
|
|
|
|
assert np.prod(frame_colors == grid_u8, axis=-1).any(), "Missing grid_str"
|
|
|
|
|
|
|
|
assert (np.amax(frame_colors, axis=0) == np.amax(all_colors, axis=0)).all()
|
|
|
|
assert (np.amin(frame_colors, axis=0) == np.amin(all_colors, axis=0)).all()
|