Author: JonNeal Date: 2008-02-17 03:03:04 -0500 (Sun, 17 Feb 2008) New Revision: 917
Modified: trunk/pysoy/src/colors/Color.pxi Log: Colors addition and subtraction, proper negative output also Modified: trunk/pysoy/src/colors/Color.pxi =================================================================== --- trunk/pysoy/src/colors/Color.pxi 2008-02-17 05:39:50 UTC (rev 916) +++ trunk/pysoy/src/colors/Color.pxi 2008-02-17 08:03:04 UTC (rev 917) @@ -54,6 +54,30 @@ else : raise TypeError('can only multiply Color by a Color, int, or float') + def __add__(self, value) : + if type(value) == int or type(value) == float : + return Color(( (<Color> self)._r + value, (<Color> self)._g + value, + (<Color> self)._b + value, (<Color> self)._a + value )) + elif isinstance(value, Color) : + return Color(( (<Color> self)._r + (<Color> value)._r, + (<Color> self)._g + (<Color> value)._g, + (<Color> self)._b + (<Color> value)._b, + (<Color> self)._a + (<Color> value)._a )) + else : + raise TypeError('can only add Color with a Color, int, or float') + + def __sub__(self, value) : + if type(value) == int or type(value) == float : + return Color(( (<Color> self)._r - value, (<Color> self)._g - value, + (<Color> self)._b - value, (<Color> self)._a - value )) + elif isinstance(value, Color) : + return Color(( (<Color> self)._r - (<Color> value)._r, + (<Color> self)._g - (<Color> value)._g, + (<Color> self)._b - (<Color> value)._b, + (<Color> self)._a - (<Color> value)._a )) + else : + raise TypeError('can only subtract Color by a Color, int, or float') + def __repr__(self) : return '<Color %s>' % (self.hex) @@ -68,6 +92,8 @@ property hex : def __get__(self) : cdef float _top + cdef float _bottom + _top = self._r if self._g > _top : _top = self._g @@ -75,6 +101,25 @@ _top = self._b if self._a > _top : _top = self._a + + _bottom = self._r + if self._g < _bottom : + _bottom = self._g + if self._b < _bottom : + _bottom = self._b + if self._a < _bottom : + _bottom = self._a + + if _top < 0 : + return '#%.2x%.2x%.2x%.2x * %.2f' % (self._a*255*_bottom, + self._r*255*_bottom, + self._g*255*_bottom, + self._b*255*_bottom, + _bottom*-1) + if _bottom < 0 : + return '#%.2x%.2x%.2x%.2x' % (self._a*255-_bottom,self._r*255-_bottom, + self._g*255-_bottom,self._b*255-_bottom) + if _top > 1.0 : return '#%.2x%.2x%.2x%.2x * %.2f' % (self._a*255/_top,self._r*255/_top, self._g*255/_top,self._b*255/_top, _______________________________________________ PySoy-SVN mailing list PySoy-SVN@pysoy.org http://www.pysoy.org/mailman/listinfo/pysoy-svn