From aef5c8a9475faf61d565c2af1ee3f7e241e7815f Mon Sep 17 00:00:00 2001 From: MinchinWeb Date: Sun, 19 Jul 2015 23:14:28 -0600 Subject: [PATCH] [Palette] allow addition Closes #8 --- .travis.yml | 6 +++--- colourettu/_colour.py | 4 ++-- colourettu/_palette.py | 35 ++++++++++++++++++++++++++++++++++ docs/changelog.rst | 1 + docs/requirements.rst | 2 +- test/test_colour.py | 5 ++--- test/test_palette.py | 43 +++++++++++++++++++++++++++++++++++++++--- 7 files changed, 84 insertions(+), 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5d81a3ca..a3e42695 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ -sudo: false +sudo: false # run on new infrastructure language: python python: # - "2.6" # green doesn't run on Python 2.6 @@ -19,6 +19,6 @@ install: - pip install -r test/requirements.txt - pip install coveralls # command to run tests -script: green -vvr +script: green test -vvr after_success: - coveralls + - coveralls diff --git a/colourettu/_colour.py b/colourettu/_colour.py index de7ac90b..9a6cda4a 100644 --- a/colourettu/_colour.py +++ b/colourettu/_colour.py @@ -97,8 +97,8 @@ def __init__(self, mycolour="#FFF", normalized_rgb=False): (type(mycolour[1]) in (float, int)) and (type(mycolour[2]) in (float, int))): if((0 <= mycolour[0] <= 1) and - (0 <= mycolour[1] <= 1) and - (0 <= mycolour[2] <= 1)): + (0 <= mycolour[1] <= 1) and + (0 <= mycolour[2] <= 1)): self._r = int(mycolour[0]*255) self._g = int(mycolour[1]*255) self._b = int(mycolour[2]*255) diff --git a/colourettu/_palette.py b/colourettu/_palette.py index ef5ce795..ccaac52e 100644 --- a/colourettu/_palette.py +++ b/colourettu/_palette.py @@ -78,6 +78,41 @@ def __str__(self): def __len__(self): return(len(self._colours)) + def __add__(self, other): + """ + - adding two `colourettu.palette`s will concatenate the two together + - if two `colourettu.palette`s are added, and the last colour of the + first palette is the same as the first colour of the second palette, + that colour will only appear once in the new palette + - `colourettu.palette`s and `colourettu.colour`s can be added to + create new `colourettu.palette`s + """ + colour_for_type = colour() + palette_for_type = palette() + if type(other) is type(colour_for_type): + self._colours.append(other) + self._end = other + return self + elif type(other) is type(palette_for_type): + if (self._end == other._start): + self._colours.extend(other._colours[1:]) + else: + self._colours.extend(other._colours) + self._end = other._end + return self + else: + raise TypeError('unsupported opperand type(s) for +: {} and {}'.format(type(self), type(other))) + + def __radd__(self, other): + # used for `colour + palette` + colour_for_type = colour() + if type(other) is type(colour_for_type): + self._colours.insert(0, other) + self._start = other + return self + else: + raise TypeError('unsupported opperand type(s) for +: {} and {}'.format(type(self), type(other))) + # TO-DO def __init__(self) #this allows : for colour in palette: def from_list(self, list_of_colours, normalized_rgb=False): diff --git a/docs/changelog.rst b/docs/changelog.rst index c54a2358..716a359a 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,7 @@ Changelog ========= +- :feature:'8' allow addition of palettes and palettes and colours - :support:`-` manage changelog with `Releases `_ - :feature:`-` add `palette` class - :feature:`-` allow creation of colours from normalized rgb values diff --git a/docs/requirements.rst b/docs/requirements.rst index 5dd1b721..f6260011 100644 --- a/docs/requirements.rst +++ b/docs/requirements.rst @@ -16,7 +16,7 @@ Testing *colourettu* requires: Then run (from the base directory):: - green tests + green test -vv Documentation Generation diff --git a/test/test_colour.py b/test/test_colour.py index 70014a23..d08824bf 100644 --- a/test/test_colour.py +++ b/test/test_colour.py @@ -207,15 +207,14 @@ def test_eq(self): c5 = colourettu.colour('#123456') c6 = c5 c7 = colourettu.colour('#000000') - c8 = colourettu.colour([0,0,0]) - + c8 = colourettu.colour([0, 0, 0]) + self.assertEqual(c1, c2) self.assertEqual(c3, c4) self.assertEqual(c5, c6) self.assertEqual(c7, c8) - class Test_Contrast(unittest.TestCase): def test_white_white(self): diff --git a/test/test_palette.py b/test/test_palette.py index aeb9b415..11806eaf 100644 --- a/test/test_palette.py +++ b/test/test_palette.py @@ -38,11 +38,11 @@ def test_from_list_tuple(self): self.assertEqual(p1._end, colourettu.colour('#aabcde')) def test_convert_start_colour(self): - p1 = colourettu.palette(start_colour = '#124') + p1 = colourettu.palette(start_colour='#124') self.assertEqual(p1._start, colourettu.colour('#112244')) def test_convert_end_colour(self): - p1 = colourettu.palette(end_colour = '#a7f') + p1 = colourettu.palette(end_colour='#a7f') self.assertEqual(p1._end, colourettu.colour('#aa77ff')) def test_invalid_start_colour(self): @@ -50,7 +50,7 @@ def test_invalid_start_colour(self): p1 = colourettu.palette(start_colour="more") def test_invalid_end_colour(self): - with self.assertRaises(ValueError): + with self.assertRaises(ValueError): p1 = colourettu.palette(end_colour=[1, 2, 3, 4]) def test_from_list_convert_list_colour(self): @@ -63,6 +63,39 @@ def test_from_list_convert_list_colour(self): self.assertEqual(p1._start, colourettu.colour('#c2e5ae')) self.assertEqual(p1._end, colourettu.colour('#9295c0')) + def test_palette_plus_colour(self): + p1 = colourettu.palette() + c2 = colourettu.colour('#123') + p2 = p1 + c2 + self.assertEqual(len(p2), 3) + self.assertEqual(p2._start, colourettu.colour('#fff')) + self.assertEqual(p2._end, colourettu.colour('#112233')) + + def test_colour_plus_palette(self): + p1 = colourettu.palette() + c2 = colourettu.colour('#abc') + p2 = c2 + p1 + self.assertEqual(len(p2), 3) + self.assertEqual(p2._start, colourettu.colour('#aabbcc')) + self.assertEqual(p2._end, colourettu.colour('#000')) + + def test_palette_plus_palette(self): + p1 = colourettu.palette() + p2 = colourettu.palette('#abc', '#345') + p3 = p1 + p2 + self.assertEqual(len(p3), 4) + self.assertEqual(p3._start, colourettu.colour('#fff')) + self.assertEqual(p3._end, colourettu.colour('#345')) + + def test_palette_plus_palette_overlapping_colour(self): + p1 = colourettu.palette('#123', '#456') + p2 = colourettu.palette('#456', '#789') + p3 = p1 + p2 + self.assertEqual(len(p3), 3) + self.assertEqual(p3._start, colourettu.colour('#123')) + self.assertEqual(p3._end, colourettu.colour('#789')) + self.assertEqual(p3._colours[1], colourettu.colour('#456')) + class Test_Palette_with_Images(unittest.TestCase): @@ -77,6 +110,10 @@ def test_to_image(self): p1.to_image() self.assertTrue(os.path.isfile('palette.png')) + @skip('not yet defined') + def test_to_image_alpha_channel(self): + pass + @skip('not yet defined') def test_to_image_band_width(self): pass