tool to calculate color combination
Max M
maxm at mxm.dk
Thu May 1 14:15:55 EDT 2008
Astan Chee skrev:
> Hi,
> I was just wondering if there is a tool/script in python that allows me
> to do color calculations; specifically, when I add them.
There is the colorsys module which I have used in this class:
from colorsys import rgb_to_hls, hls_to_rgb
import binascii
from types import StringType, TupleType, ListType, IntType, FloatType
from math import modf
class RGB:
""" Makes it easier to work with rgb colors """
def __init__(self, color):
# set color value
self.color = self.any2color(color)
def any2color(self, color):
"""
Takes a number of color formats and returns a sequence of 3
floats:
(r,g,b)
Some legal formats for pure blue are:
'0x0000FF','#0000FF','0000FF',
[0.0, 0.0, 1.0], [0, 0, 255], (0, 0.0, 'FF') and ('0', '0', 'f').
Mixed types are allowed in sequences.
"""
# it must be a hex, so convert to sequence of hex values
if isinstance(color, StringType):
# handle hex value
if color[:2].lower() == '0x': color = color[2:]
elif color[0] == '#': color = color[1:]
color = (color[:2], color[2:4], color[4:])
# convert sequence to floats
color_result = []
a = color_result.append
for part in color:
# what type is the part?
if isinstance(part, StringType): # hex part
if len(part) == 1:
part = '0%s' % part
b = binascii.a2b_hex(part)
a(ord(b[0])/255.0)
elif isinstance(part, IntType): # int part
a(part/255.0)
elif isinstance(part, FloatType): # float part
a(part)
return color_result
def __str__(self):
"Returns string representation of color (same as html_hex)"
return self.html_hex()
def r(self):
return self.color[0]
def g(self):
return self.color[1]
def b(self):
return self.color[2]
def bytes(self):
"""
Takes a sequence of colors in floats, and returns a sequence of
int in
the range 0-255
"""
return map(lambda x: int(x*255), self.color)
def html_hex(self):
"""
Returns the color in a hex string representation of the form
'#0000FF'
"""
r,g,b = self.color
return '#%02X%02X%02X' % (int(r*255),int(g*255),int(b*255))
def _cAdd(self, x, y):
"Private method! Cirkular add x+y so value allways in 0.0-1.0
range"
fractional, integer = modf(x + y)
if not fractional and integer: # special case 1.0
return 1.0
return abs(fractional)
# wrong result for negative values!
def hls_delta(self, dh, dl, ds):
"""
Returns a Color object same as self, but adjusted by delta hls
values
"""
h,l,s = rgb_to_hls(*self.color)
nh = self._cAdd(h, dh)
nl = l + dl
if nl > 1.0: nl = 1.0
if nl < 0.0: nl = 0.0
ns = s + ds
if ns > 1.0: ns = 1.0
if ns < 0.0: ns = 0.0
return RGB(hls_to_rgb(nh, nl, ns))
def change_ls(self, new_l=None, new_s=None):
"""
Returns a Color object same as self, but with new lightness and
saturation levels
"""
h,l,s = rgb_to_hls(*self.color)
if new_l == None:
new_l = l
if new_s == None:
new_s = s
return RGB(hls_to_rgb(h, new_l, new_s))
def spacer(self, transparent=None):
"""
Creates a 1x1 GIF89a of color. If no color it returns a
transparent gif
Should probably not be in this module?
"""
template = [71, 73, 70, 56, 57, 97, 1, 0, 1, 0, 128, 0, 0, 255,
255,
255, 0, 0, 0, 33, 249, 4, 1, 0, 0, 0, 0, 44, 0, 0, 0, 0, 1, 0,
1, 0,
0, 2, 2, 68, 1, 0, 59]
if not transparent:
template[13:16] = self.bytes() # set rgb values
template[22] = 0 # remove transparency
return ''.join(map(chr, template))
if __name__=='__main__':
red = (255, 0, 0)
green = (0.0, 1.0, 0.0)
blue = (0.0, 0.0, 1.0)
yellow = '#ffff00'
col = RGB(blue)
print col.color
print col.bytes()
print col
brighter = col.change_ls(0.0, 0.0)
print 'brighter:',brighter
# complementary = col.hls_delta(0.50, 0.0, 0.0)
# print complementary
--
hilsen/regards Max M, Denmark
http://www.mxm.dk/
IT's Mad Science
More information about the Python-list
mailing list