Skip to content
This repository has been archived by the owner on Jun 30, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #33 from homm/master
Some colors improvements
  • Loading branch information
ojii committed May 23, 2013
2 parents 0b79cf0 + 0160ace commit c1c575d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
21 changes: 9 additions & 12 deletions pymaging/colors.py
Expand Up @@ -23,16 +23,18 @@
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from __future__ import division
from collections import namedtuple
from pymaging.utils import fdiv


def _mixin_alpha(colors, alpha):
ratio = fdiv(alpha, 255)
ratio = alpha / 255
return [int(round(color * ratio)) for color in colors]

class Color(object):
def __init__(self, red, green, blue, alpha):
__slots__ = 'red', 'green', 'blue', 'alpha'

def __init__(self, red, green, blue, alpha=255):
self.red = red
self.green = green
self.blue = blue
Expand All @@ -45,7 +47,7 @@ def __repr__(self):
return '<%s>' % self

def __hash__(self):
return hash(self.to_hexcode())
return hash((self.red, self.green, self.blue, self.alpha))

def __eq__(self, other):
return (
Expand All @@ -61,10 +63,7 @@ def from_pixel(cls, pixel):
Convert a pixel (list of 3-4 values) to a Color instance.
"""
assert len(pixel) in (3,4), "Color.from_pixel only supports 3 and 4 value pixels"
pixel = list(pixel)
if len(pixel) == 3:
pixel.append(255)
return cls(*map(int,pixel))
return cls(*map(int, list(pixel)))

@classmethod
def from_hexcode(cls, hexcode):
Expand All @@ -75,8 +74,6 @@ def from_hexcode(cls, hexcode):
assert len(hexcode) in (3,4,6,8), "Hex codes must be 3, 4, 6 or 8 characters long"
if len(hexcode) in (3,4):
hexcode = ''.join(x*2 for x in hexcode)
if len(hexcode) == 6:
hexcode += 'ff'
return cls(*[int(''.join(x), 16) for x in zip(hexcode[::2], hexcode[1::2])])

def get_for_brightness(self, brightness):
Expand All @@ -96,8 +93,8 @@ def cover_with(self, cover_color):
if cover_color.alpha == 255:
return Color(cover_color.red, cover_color.green, cover_color.blue, cover_color.alpha)

srca = fdiv(cover_color.alpha, 255)
dsta = fdiv(self.alpha, 255)
srca = cover_color.alpha / 255
dsta = self.alpha / 255
outa = srca + dsta * (1 - srca)

srcr, srcg, srcb = cover_color.red, cover_color.green, cover_color.blue
Expand Down
27 changes: 27 additions & 0 deletions pymaging/tests/test_colors.py
@@ -0,0 +1,27 @@
import unittest

from pymaging.colors import Color

def ctuple(c):
return c.red, c.green, c.blue, c.alpha

class TestColor(unittest.TestCase):
## constructors
def test_constructor(self):
c = Color(10, 20, 30)
self.assertEqual(c.red, 10)
self.assertEqual(c.alpha, 255)

def test_from_pixel(self):
c = Color.from_pixel([10, 20, 30])
self.assertEqual(ctuple(c), (10, 20, 30, 255))

c = Color.from_pixel([10, 20, 30, 40])
self.assertEqual(ctuple(c), (10, 20, 30, 40))

def test_from_hexcode(self):
c = Color.from_hexcode('feef1510')
self.assertEqual(ctuple(c), (254, 239, 21, 16))

c = Color.from_hexcode('123')
self.assertEqual(ctuple(c), (17, 34, 51, 255))

0 comments on commit c1c575d

Please sign in to comment.