Skip to content

Commit

Permalink
[Palette] allow addition
Browse files Browse the repository at this point in the history
Closes #8
  • Loading branch information
MinchinWeb committed Jul 20, 2015
1 parent 0377e23 commit aef5c8a
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 12 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
4 changes: 2 additions & 2 deletions colourettu/_colour.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions colourettu/_palette.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Changelog
=========

- :feature:'8' allow addition of palettes and palettes and colours
- :support:`-` manage changelog with `Releases <https://github.com/bitprophet/releases>`_
- :feature:`-` add `palette` class
- :feature:`-` allow creation of colours from normalized rgb values
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Testing *colourettu* requires:

Then run (from the base directory)::

green tests
green test -vv


Documentation Generation
Expand Down
5 changes: 2 additions & 3 deletions test/test_colour.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
43 changes: 40 additions & 3 deletions test/test_palette.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ 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):
with self.assertRaises(ValueError):
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):
Expand All @@ -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):

Expand All @@ -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
Expand Down

0 comments on commit aef5c8a

Please sign in to comment.