MCPcopy Create free account
hub / github.com/ActiveState/code / Color

Class Color

recipes/Python/577674_Bitmap_Maker/recipe-577674.py:76–127  ·  view source on GitHub ↗

class for specifying colors while drawing BitMap elements

Source from the content-addressed store, hash-verified

74 return ord(input_string[offset+2]) << 16 | ord(input_string[offset+1]) << 8 | ord(input_string[offset])
75
76class Color(object):
77 """class for specifying colors while drawing BitMap elements"""
78 __slots__ = [ 'red', 'grn', 'blu' ]
79 __shade = 32
80
81 def __init__( self, r=0, g=0, b=0 ):
82 self.red = r
83 self.grn = g
84 self.blu = b
85
86 def __setattr__(self, name, value):
87 if hasattr(self, name):
88 raise AttributeError("Color is immutable")
89 else:
90 object.__setattr__(self, name, value)
91
92 def __str__( self ):
93 return "R:%d G:%d B:%d" % (self.red, self.grn, self.blu )
94
95 def __hash__( self ):
96 return ( ( int(self.blu) ) +
97 ( int(self.grn) << 8 ) +
98 ( int(self.red) << 16 ) )
99
100 def __eq__( self, other ):
101 return (self is other) or (self.toLong == other.toLong)
102
103 def lighten( self ):
104 return Color(
105 min( self.red + Color.__shade, 255),
106 min( self.grn + Color.__shade, 255),
107 min( self.blu + Color.__shade, 255)
108 )
109
110 def darken( self ):
111 return Color(
112 max( self.red - Color.__shade, 0),
113 max( self.grn - Color.__shade, 0),
114 max( self.blu - Color.__shade, 0)
115 )
116
117 def toLong( self ):
118 return self.__hash__()
119
120 def fromLong( l ):
121 b = l & 0xff
122 l = l >> 8
123 g = l & 0xff
124 l = l >> 8
125 r = l & 0xff
126 return Color( r, g, b )
127 fromLong = staticmethod(fromLong)
128
129# define class constants for common colors
130Color.BLACK = Color( 0, 0, 0 )

Callers 5

lightenMethod · 0.70
darkenMethod · 0.70
fromLongMethod · 0.70
recipe-577674.pyFile · 0.70
loadImageMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected