Skip to content

Commit

Permalink
New List pattern and some bugfixes and tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
rec committed Dec 27, 2014
1 parent 19fb8e5 commit 0aad1d0
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 11 deletions.
15 changes: 11 additions & 4 deletions code/cpp/echomesh/color/ColorList.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
9 changes: 7 additions & 2 deletions code/cpp/echomesh/color/FColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion code/cpp/echomesh/color/HSB.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,3 @@ class HSB : public ColorModel {

} // namespace color
} // namespace echomesh

2 changes: 0 additions & 2 deletions code/python/echomesh/base/MergeSettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion code/python/echomesh/color/Scroll_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion code/python/echomesh/pattern/Column.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand Down
56 changes: 56 additions & 0 deletions code/python/echomesh/pattern/List.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions code/python/echomesh/pattern/Registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'Image',
'Inject',
'Insert',
'List',
'Mirror',
'Scroll',
'Spread',
Expand Down

0 comments on commit 0aad1d0

Please sign in to comment.