Skip to content

Commit

Permalink
New legacy_palette handls existing palettes in BPA
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Ritchford committed Oct 14, 2018
1 parent bca1322 commit e6d1352
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 3 deletions.
9 changes: 6 additions & 3 deletions bibliopixel/animation/animation.py
Expand Up @@ -3,6 +3,8 @@
from . runner import Runner, STATE
from .. util import deprecated, log
from .. util.colors import palettes
from .. util.colors.legacy_palette import pop_legacy_palette

from .. util.threads.animation_threading import AnimationThreading
from .. project import attributes, fields

Expand Down Expand Up @@ -35,7 +37,10 @@ def construct(cls, project, *, run=None, name=None, data=None, **desc):
a.data = data
return a

def __init__(self, layout, *, preclear=True, palette=None, **kwds):
def __init__(self, layout, *, preclear=True, **kwds):
self.palette = pop_legacy_palette(kwds)
self.palette.length = layout.numLEDs

attributes.set_reserved(self, 'animation', **kwds)
self.layout = layout
assert layout
Expand All @@ -47,8 +52,6 @@ def __init__(self, layout, *, preclear=True, palette=None, **kwds):
self.runner = None
self.time = time.time
self.preframe_callbacks = []
self.palette = palette or palettes.get()
self.palette.length = layout.numLEDs

def set_project(self, project):
self.project = project
Expand Down
4 changes: 4 additions & 0 deletions bibliopixel/project/fields.py
Expand Up @@ -8,6 +8,8 @@
'c_order': channel_order,
'channel_order': channel_order,
'color': color,
'color1': color,
'color2': color,
'colors': colors,
'count': int_name,
'duration': duration,
Expand All @@ -16,6 +18,8 @@
'i': int_name,
'ledtype': ledtype,
'num': int_name,
'offColor': color,
'onColor': color,
'palette': colors,
'spi_interface': spi_interface,
'time': duration,
Expand Down
43 changes: 43 additions & 0 deletions bibliopixel/util/colors/legacy_palette.py
@@ -0,0 +1,43 @@
from . import make, palettes
from . names import COLORS

LEGACY_FIELDS = (
('color',),
('color1', 'color2'),
('onColor', 'offColor'),
('colors',),
('palette',),
)


def pop_legacy_palette(kwds):
"""
Older animations in BPA and other areas use all sorts of different names for
what we are now representing with palettes.
This function mutates a kwds dictionary to remove these legacy fields and
extract a palette from it, which it returns.
"""

cases = []
for fields in LEGACY_FIELDS:
if any(f in kwds for f in fields):
cases.append(fields)

if not cases:
return palettes.get()

if len(cases) > 1:
raise ValueError('Cannot set all of ' + ', '.join(sum(cases, [])))

first, *rest = cases[0]

a = kwds.pop(first, COLORS.Red)
if first == 'color':
return make.colors((a,))

if not rest:
return a

b = kwds.pop(rest[0], COLORS.Black)
return make.colors((a, b))
6 changes: 6 additions & 0 deletions bibliopixel/util/colors/palette.py
Expand Up @@ -112,3 +112,9 @@ def get(self, position=0):
dr, dg, db = r2 - r1, g2 - g1, b2 - b1

return r1 + fade * dr, g1 + fade * dg, b1 + fade * db

def __eq__(self, other):
return super().__eq__(other) and vars(self) == vars(other)

def __ne__(self, other):
return not (self == other)
31 changes: 31 additions & 0 deletions test/bibliopixel/util/colors/legacy_palette_test.py
@@ -0,0 +1,31 @@
import collections, unittest
from bibliopixel.util.colors import palette, palettes
from bibliopixel.colors import COLORS
from bibliopixel.util.colors.legacy_palette import pop_legacy_palette


class LegacyPaletteTest(unittest.TestCase):
def test_simple(self):
kwds = {}
pal = pop_legacy_palette(kwds)
self.assertFalse(kwds)
self.assertEqual(pal, palettes.get())

def test_palette(self):
pal = 'wombat'
for k in 'palette', 'colors':
kwds = {k: pal, 'other': 'stuff'}
self.assertIs(pal, pop_legacy_palette(kwds))
self.assertEqual(kwds, {'other': 'stuff'})

def test_color(self):
kwds = {'color': COLORS.yellow, 'other': 'stuff'}
pal = pop_legacy_palette(kwds)
self.assertEqual(kwds, {'other': 'stuff'})
self.assertEqual(pal, palette.Palette([COLORS.yellow]))

def test_color(self):
kwds = {'offColor': 'lime', 'other': 'stuff'}
pal = pop_legacy_palette(kwds)
self.assertEqual(kwds, {'other': 'stuff'})
self.assertEqual(pal, palette.Palette([COLORS.red, COLORS.lime]))
16 changes: 16 additions & 0 deletions test/bibliopixel/util/colors/palette_test.py
Expand Up @@ -203,3 +203,19 @@ def test_autoscale(self):
p.length = 256
result = [p.get(32 * i) for i in range(10)]
self.assertEqual(expected, result)

def test_equality(self):
colors = [Red, Green, Blue, White]
p = Palette(colors, autoscale=True)
q = Palette(colors, autoscale=True)

self.assertEqual(p, q)
self.assertFalse(p != q)

r = Palette(colors)
self.assertNotEqual(p, r)
self.assertFalse(p == r)

s = Palette(colors, serpentine=True)
t = Palette(colors[:3], serpentine=True)
self.assertNotEqual(s, t)

0 comments on commit e6d1352

Please sign in to comment.