59 lines
2.4 KiB
Python
59 lines
2.4 KiB
Python
from openrgb_hue.color import Color
|
|||
|
|
from openrgb_hue.gradient import Gradient, Stop
|
||
|
|
|
||
|
|
|
||
|
|
def test_from_colors_even_spacing_single():
|
||
|
|
gradient = Gradient.from_colors([Color(10, 20, 30)])
|
||
|
|
assert [s.position for s in gradient.stops] == [0.0]
|
||
|
|
|
||
|
|
|
||
|
|
def test_from_colors_even_spacing_multiple():
|
||
|
|
colors = [Color(0, 0, 0), Color(50, 50, 50), Color(255, 255, 255)]
|
||
|
|
gradient = Gradient.from_colors(colors)
|
||
|
|
assert [s.position for s in gradient.stops] == [0.0, 0.5, 1.0]
|
||
|
|
|
||
|
|
|
||
|
|
def test_sample_at_exact_stop_positions():
|
||
|
|
colors = [Color(255, 0, 0), Color(0, 255, 0), Color(0, 0, 255)]
|
||
|
|
gradient = Gradient.from_colors(colors, interpolation="rgb")
|
||
|
|
assert gradient.sample(0.0) == colors[0]
|
||
|
|
assert gradient.sample(0.5) == colors[1]
|
||
|
|
assert gradient.sample(1.0) == colors[0] # cyclic: wraps back to the first stop
|
||
|
|
|
||
|
|
|
||
|
|
def test_sample_wraps_cyclically():
|
||
|
|
colors = [Color(255, 0, 0), Color(0, 0, 255)]
|
||
|
|
gradient = Gradient.from_colors(colors, interpolation="rgb")
|
||
|
|
# t=0.75 is halfway between the last stop (1.0 -> wraps to 0.0) and back
|
||
|
|
# to the first stop; verify negative/over-1 t wrap to the same result.
|
||
|
|
assert gradient.sample(-0.25) == gradient.sample(0.75)
|
||
|
|
assert gradient.sample(1.25) == gradient.sample(0.25)
|
||
|
|
|
||
|
|
|
||
|
|
def test_single_stop_gradient_is_constant():
|
||
|
|
gradient = Gradient([Stop(0.3, Color(1, 2, 3))])
|
||
|
|
assert gradient.sample(0.0) == Color(1, 2, 3)
|
||
|
|
assert gradient.sample(0.9) == Color(1, 2, 3)
|
||
|
|
|
||
|
|
|
||
|
|
def test_from_scene_dedupe_collapses_consecutive_duplicates(sample_scene):
|
||
|
|
with_dedupe = Gradient.from_scene(sample_scene, dedupe=True, use_brightness=False)
|
||
|
|
without_dedupe = Gradient.from_scene(sample_scene, dedupe=False, use_brightness=False)
|
||
|
|
# sample_scene has 4 lights but lights 2 and 3 share a color.
|
||
|
|
assert len(with_dedupe.stops) == 3
|
||
|
|
assert len(without_dedupe.stops) == 4
|
||
|
|
|
||
|
|
|
||
|
|
def test_from_scene_use_brightness_scales_colors(sample_scene):
|
||
|
|
gradient = Gradient.from_scene(sample_scene, dedupe=False, use_brightness=True)
|
||
|
|
# Fourth light: Color(0, 0, 255) at brightness 128/255.
|
||
|
|
last_stop_color = gradient.stops[-1].color
|
||
|
|
expected = Color(0, 0, 255).scale_brightness(128 / 255)
|
||
|
|
assert last_stop_color == expected
|
||
|
|
|
||
|
|
|
||
|
|
def test_preview_stops_length():
|
||
|
|
gradient = Gradient.from_colors([Color(255, 0, 0), Color(0, 255, 0)])
|
||
|
|
assert len(gradient.preview_stops(10)) == 10
|
||
|
|
assert gradient.preview_stops(0) == []
|