Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added arithmetic and blend modes

  • Loading branch information...
commit 6407855c7ad574cb174899e1b0a50d68e52b34bc 1 parent d8d9b1a
@mattrobenolt authored
Showing with 73 additions and 3 deletions.
  1. +73 −3 colors.py
View
76 colors.py
@@ -17,7 +17,6 @@ class _ColorMetaClass(type):
>>> RGBColor(r=150, g=0, b=100).red
150
-
"""
def __new__(cls, name, bases, attrs):
# Check for internal Meta class providing a property map
@@ -45,6 +44,76 @@ def rgb(self):
def hsv(self):
raise NotImplemented
+ def multiply(self, other):
+ self_rgb = self.rgb
+ other_rgb = other.rgb
+ return RGBColor(
+ self_rgb.red * other_rgb.red / 255.0,
+ self_rgb.green * other_rgb.green / 255.0,
+ self_rgb.blue * other_rgb.blue / 255.0
+ )
+
+ __mul__ = multiply
+
+ def add(self, other):
+ self_rgb = self.rgb
+ other_rgb = other.rgb
+ return RGBColor(
+ min(255, self_rgb.red + other_rgb.red),
+ min(255, self_rgb.green + other_rgb.green),
+ min(255, self_rgb.blue + other_rgb.blue),
+ )
+
+ __add__ = add
+
+ def divide(self, other):
+ self_rgb = self.rgb
+ other_rgb = other.rgb
+ if 0 in other_rgb:
+ raise ZeroDivisionError
+ return RGBColor(
+ self_rgb.red / float(other_rgb.red),
+ self_rgb.green / float(other_rgb.green),
+ self_rgb.blue / float(other_rgb.blue),
+ )
+
+ __div__ = divide
+
+ def subtract(self, other):
+ self_rgb = self.rgb
+ other_rgb = other.rgb
+ return RGBColor(
+ max(0, (self_rgb.red - other_rgb.red)),
+ max(0, (self_rgb.green - other_rgb.green)),
+ max(0, (self_rgb.blue - other_rgb.blue)),
+ )
+
+ __sub__ = subtract
+
+ def screen(self, other):
+ self_rgb = self.rgb
+ other_rgb = other.rgb
+ return RGBColor(
+ 255 - (((255 - self_rgb.red) * (255 - other_rgb.red)) / 255.0),
+ 255 - (((255 - self_rgb.green) * (255 - other_rgb.green)) / 255.0),
+ 255 - (((255 - self_rgb.blue) * (255 - other_rgb.blue)) / 255.0),
+ )
+
+ def difference(self, other):
+ self_rgb = self.rgb
+ other_rgb = other.rgb
+ return RGBColor(
+ abs(self_rgb.red - other_rgb.red),
+ abs(self_rgb.green - other_rgb.green),
+ abs(self_rgb.blue - other_rgb.blue),
+ )
+
+ def overlay(self, other):
+ return self.screen(self.multiply(other))
+
+ def invert(self):
+ return self.difference(RGBColor(255, 255, 255))
+
def __eq__(self, other):
self_rgb = self.rgb
other_rgb = other.rgb
@@ -52,6 +121,9 @@ def __eq__(self, other):
and self_rgb.green == other_rgb.green \
and self_rgb.blue == other_rgb.blue
+ def __contains__(self, item):
+ return item in self._color
+
def __ne__(self, other):
return not self.__eq__(other)
@@ -65,7 +137,6 @@ def __iter__(self):
100
50
0
-
"""
return iter(self._color)
@@ -178,7 +249,6 @@ class ColorWheel(object):
#00cc26
>>> print '#%s' % wheel.next().hex
#009ecc
-
"""
def __init__(self, start=0):
# A 1.1 shift is identical to 0.1
Please sign in to comment.
Something went wrong with that request. Please try again.