Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New legacy_palette handls existing palettes in BPA
- Loading branch information
Tom Ritchford
committed
Oct 14, 2018
1 parent
bca1322
commit e6d1352
Showing
6 changed files
with
106 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters