From 0aad1d0f9f1dc4ddbcff5cea2378bb4c29a20f86 Mon Sep 17 00:00:00 2001 From: Tom Ritchford Date: Fri, 26 Dec 2014 23:17:38 -0500 Subject: [PATCH] New List pattern and some bugfixes and tweaks. --- code/cpp/echomesh/color/ColorList.pyx | 15 ++++-- code/cpp/echomesh/color/FColor.h | 9 +++- code/cpp/echomesh/color/HSB.h | 1 - code/python/echomesh/base/MergeSettings.py | 2 - code/python/echomesh/color/Scroll_test.py | 2 +- code/python/echomesh/pattern/Column.py | 2 +- code/python/echomesh/pattern/List.py | 56 ++++++++++++++++++++++ code/python/echomesh/pattern/Registry.py | 1 + 8 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 code/python/echomesh/pattern/List.py diff --git a/code/cpp/echomesh/color/ColorList.pyx b/code/cpp/echomesh/color/ColorList.pyx index b1430aef..938114e5 100644 --- a/code/cpp/echomesh/color/ColorList.pyx +++ b/code/cpp/echomesh/color/ColorList.pyx @@ -40,6 +40,13 @@ cdef class ColorList: recolumn(self.thisptr, self._columns, cols) self._columns = cols + property size: + def __get__(self): + return len(self) + + def __set__(self, unsigned int new_size): + self.thisptr.resize(new_size) + def append(self, object item): cdef FColor c if fill_color(item, &c): @@ -86,8 +93,7 @@ cdef class ColorList: original_length = len(self) colors = list(colors) new_length = len(colors) - if return_errors: - error_colors = [] + error_colors = [] self.thisptr.reserve(original_length + new_length) for color in colors: @@ -119,8 +125,9 @@ cdef class ColorList: def interpolate(self, color_list, float fader, unsigned int smooth=0): cdef ColorList cl = toColorList(color_list) - cdef ColorList result = ColorList(columns=self.columns) - result.thisptr[0] = self.thisptr.interpolate(cl.thisptr[0], fader, smooth) + cdef ColorList result = ColorList(columns=self.column or cl.columns) + result.thisptr[0] = self.thisptr.interpolate( + cl.thisptr[0], fader, smooth) return result def pop(self, int index=-1): diff --git a/code/cpp/echomesh/color/FColor.h b/code/cpp/echomesh/color/FColor.h index 4e058ecb..f12fd9d3 100644 --- a/code/cpp/echomesh/color/FColor.h +++ b/code/cpp/echomesh/color/FColor.h @@ -134,7 +134,9 @@ class FColor { } struct Comparer { - bool operator()(const FColor& x, const FColor& y) { return x.compare(y) < 0; } + bool operator()(const FColor& x, const FColor& y) { + return x.compare(y) < 0; + } }; void combine(const FColor& x) { @@ -149,7 +151,10 @@ class FColor { } private: - float red_, green_, blue_, alpha_; + float red_ = 0.0; + float green_ = 0.0; + float blue_ = 0.0; + float alpha_ = 1.0; }; } // namespace color diff --git a/code/cpp/echomesh/color/HSB.h b/code/cpp/echomesh/color/HSB.h index df641319..5d2694ce 100644 --- a/code/cpp/echomesh/color/HSB.h +++ b/code/cpp/echomesh/color/HSB.h @@ -62,4 +62,3 @@ class HSB : public ColorModel { } // namespace color } // namespace echomesh - diff --git a/code/python/echomesh/base/MergeSettings.py b/code/python/echomesh/base/MergeSettings.py index 86094854..3b298f25 100644 --- a/code/python/echomesh/base/MergeSettings.py +++ b/code/python/echomesh/base/MergeSettings.py @@ -93,8 +93,6 @@ def save(self): if not os.path.exists(parent): print('Creating directory', parent) os.makedirs(parent) - else: - print('Directory exists', parent) with open(f, 'wb') as fw: if data: diff --git a/code/python/echomesh/color/Scroll_test.py b/code/python/echomesh/color/Scroll_test.py index 51d2cc2f..b3656149 100644 --- a/code/python/echomesh/color/Scroll_test.py +++ b/code/python/echomesh/color/Scroll_test.py @@ -12,7 +12,7 @@ def setUp(self): 'plum', 'teal', 'wheat', 'orchid', ]) def doTest(self, dx, dy, expected): - result = cechomesh.scroll_color_list(self.data, dx, dy, 4) + result = cechomesh.scroll_color_list(self.data, dx, dy, columns=4) expected = cechomesh.ColorList(expected) self.assertEquals(result, cechomesh.ColorList(expected)) diff --git a/code/python/echomesh/pattern/Column.py b/code/python/echomesh/pattern/Column.py index ffcaedcd..ab76cf04 100644 --- a/code/python/echomesh/pattern/Column.py +++ b/code/python/echomesh/pattern/Column.py @@ -6,7 +6,7 @@ from echomesh.pattern.Pattern import Pattern class Column(Pattern): - HELP = 'Set or rest the number of columns in an x, y pattern.' + HELP = 'Set or reset the number of columns in an x, y pattern.' SETTINGS = { 'columns': { diff --git a/code/python/echomesh/pattern/List.py b/code/python/echomesh/pattern/List.py new file mode 100644 index 00000000..22233e16 --- /dev/null +++ b/code/python/echomesh/pattern/List.py @@ -0,0 +1,56 @@ +from __future__ import absolute_import, division, print_function, unicode_literals + +from echomesh.Cechomesh import cechomesh + +import itertools + +from echomesh.pattern.Pattern import Pattern +from echomesh.util.string.Plural import plural + +class List(Pattern): + HELP = 'Display a spread of colors between two or more color endpoints.' + CONSTANT = True + PATTERN_COUNT = 0 + + SETTINGS = { + 'colors': { + 'default': [], + 'help': 'A list of colors to be displayed.', + }, + 'columns': { + 'default': 0, + 'help': 'The number of columns in the result', + }, + } + + PATTERN_COUNT = 0 + + def _evaluate(self): + colors = self.get_raw('colors') + has_lists, has_scalars = False, False + for c in colors: + is_list = isinstance(c, list) + has_lists = has_lists or is_list + has_scalars = has_scalars or not is_list + + if has_scalars: + if has_lists: + raise Exception('Can\'t mix lists and scalars in pattern.List') + colors = [colors] + max_column = max(len(c) for c in colors) + columns = self.get('columns') or max_column + if not max_column: + # Empty list. + return cechomesh.Colorlist(columns=columns) + + ce = (cechomesh.color_list_with_errors(c) for c in colors) + color_lists, errors = zip(*ce) + errors = list(itertools.chain(*errors)) + if errors: + raise Exception('\nCan\'t understand %s: %s.' % ( + plural(len(errors), 'color'), ', '.join(errors))) + result = cechomesh.ColorList(columns=columns) + for i, cl in enumerate(color_lists): + cl.size = columns + result.extend(cl) + return result diff --git a/code/python/echomesh/pattern/Registry.py b/code/python/echomesh/pattern/Registry.py index f818123d..6d5d8fc4 100755 --- a/code/python/echomesh/pattern/Registry.py +++ b/code/python/echomesh/pattern/Registry.py @@ -11,6 +11,7 @@ 'Image', 'Inject', 'Insert', + 'List', 'Mirror', 'Scroll', 'Spread',