Created
November 16, 2010 10:48
-
-
Save fiee/701683 to your computer and use it in GitHub Desktop.
Color conversion helpers, see also fiee/fiee-coloree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
def HtmlColorCode_to_strings(value): | |
"Convert a HTML color code like '#FFCC00' or 'FFCC00' to a list of strings ['FF', 'CC', '00']" | |
x = 1 * value.startswith('#') | |
return [value[x:x+2],value[x+2:x+4],value[x+4:x+6]] | |
def HtmlColorCode_to_ints(value): | |
"Convert a HTML color code like '#FFCC00' or 'FFCC00' to a list of integers [255, 204, 0]" | |
return [int(x, 16) for x in HtmlColorCode_to_strings(value)] | |
def HtmlColorCode_to_floats(value): | |
"Convert a HTML color code like '#FFCC00' or 'FFCC00' to a list of floats [1.0, 0.8, 0.0]" | |
#return (float.fromhex('0x'+x)/255 for x in HtmlColorCode_to_strings(value)) | |
# fromhex is python 2.6, rest works with 2.5! | |
return [float(int(x, 16))/255 for x in HtmlColorCode_to_strings(value)] | |
def RGB_to_CMYK(r, g, b, ucr=1.0): | |
""" | |
Take r,g,b float values (0.0 to 1.0), | |
invert to get c,m,y, | |
apply UCR (0.0 to 1.0; 0 means no UCR = CMY separation), | |
return c,m,y,k as integers (percent values) | |
This is a simple number conversion that ignores the basics of color management (no profiles involved)! | |
""" | |
c,m,y = (1.0 - (float(x)/2) for x in (g+b, r+b, r+g)) | |
k = min(c,m,y)*ucr | |
c,m,y = c-k, m-k, y-k | |
return [int(round(x*100)) for x in (c,m,y,k)] | |
def HtmlColorCode_to_CMYK(value, ucr=1.0): | |
""" | |
Convert a HTML color code like '#FFCC00' or 'FFCC00' to a list of integers (CMYK percent values) | |
""" | |
(r,g,b) = HtmlColorCode_to_floats(value) | |
return RGB_to_CMYK(r,g,b,ucr) | |
def CMYK_to_RGB(cmyk): | |
""" | |
Convert a comma separated integer list into RGB float values, i.e. | |
'100,80,0,0' to (0.0, 51.0, 255.0) | |
Returns None for invalid input. | |
This is a simple number conversion that ignores the basics of color management (no profiles involved)! | |
""" | |
c,m,y,k = (float(x) for x in cmyk.split(',')) | |
if max(c+k, m+k, y+k) > 100: | |
k = 100 - max(c,m,y) | |
return [(1 - (x+k)/100)*255 for x in (c,m,y)] | |
def CMYK_to_HtmlColorCode(cmyk): | |
""" | |
Convert a comma separated integer list into a HTML hex color code, i.e. | |
'100,80,0,0' to '#0032FF' | |
Returns empty string for invalid input. | |
This is a simple number conversion that ignores the basics of color management (no profiles involved)! | |
""" | |
t = CMYK_to_RGB(cmyk) | |
if t: return '#%02X%02X%02X' % (t[0], t[1], t[2]) | |
return '' | |
def color_spot(htmlcolorcode, text=None): | |
""" | |
HTML snippet: span with class 'colorspot' and `htmlcolorcode` as background color | |
""" | |
if text is None: | |
text = htmlcolorcode | |
return u'<span class="colorspot" style="background-color:%s;"> </span> %s' % (htmlcolorcode, text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment