Revision as of 04:28, 7 April 2004 editChinasaur (talk | contribs)Extended confirmed users3,353 edits Did my best to touch things up for HSV. Dunno Py, so may need tweaking to get it to run.← Previous edit |
Latest revision as of 15:49, 8 May 2022 edit undoQwerfjkl (bot) (talk | contribs)Bots, Mass message senders4,012,477 editsm →top: Replaced deprecated <source> tags with <syntaxhighlight>Tag: AWB |
(10 intermediate revisions by 3 users not shown) |
Line 1: |
Line 1: |
|
|
] |
|
<pre> |
|
|
|
|
|
|
|
<syntaxhighlight lang=python> |
|
#!/usr/bin/python |
|
#!/usr/bin/python |
|
|
# colorspace.py |
|
|
# calculates RGB, CMYK and HSV values from a hex triplet |
|
|
|
|
|
|
|
|
# Diclaimer: |
|
# Diclaimer: |
|
# I only just picked up python. No apologies are made for this code. |
|
# I only just picked up python. No apologies are made for this code. |
|
|
# Dysprosia and Chinasaur helped with formulas for CMYK and HSV respectively |
|
|
|
|
|
def hextriplet2rgb(triplet) : |
|
def hextriplet2rgb(triplet) : |
|
# assuming the triplet *isn't* prefixed with 0x |
|
value=int(triplet, 16) |
|
value=eval("0x"+triplet) |
|
|
r=(value & 0xFF0000) / 0x10000 |
|
r=(value & 0xFF0000) / 0x10000 |
|
g=(value & 0x00FF00) / 0x100 |
|
g=(value & 0x00FF00) / 0x100 |
Line 30: |
Line 34: |
|
MIN=min(r,g,b) |
|
MIN=min(r,g,b) |
|
|
|
|
|
# Using negatives for this generally not a good idea. In most |
|
# Using a "undefined" instead of None |
|
# languages, you'd want to use a special value like "undef" or |
|
# because we want encyclopedic style output |
|
|
h="undefined" #default |
|
# "null". I think Python has "none" but I don't know Python so |
|
|
|
s="undefined" #default |
|
# I'm not sure if that's what you'd use. |
|
|
# Especially not a good idea in this case since h is actually supposed |
|
|
# to be able to go negative (see below and my new revision to the |
|
|
# HSV color space article. |
|
|
# And anyway, using the else statement is a better way to arrange the |
|
|
# logic. Easier to read, and one less comparison so probably more |
|
⚫ |
# efficient. |
|
⚫ |
# h=-1 |
|
⚫ |
# s=-1 |
|
|
|
|
|
|
v=MAX |
|
v=MAX |
|
|
|
|
|
if (v > 0) : |
|
if v > 0 : |
|
s = (MAX - MIN) / MAX |
|
s = (MAX - MIN) / MAX |
|
else: s = 0 |
|
|
|
|
|
|
if (s > 0) : |
|
if s > 0 and s != "undefined" : |
|
if r == MAX: h=(0.0+( (g-b)/(MAX-MIN) ))*60 |
|
if r == MAX: h=(0.0+( (g-b)/(MAX-MIN) ))*60 |
|
|
|
|
# This was wrong in the article; reversed the minus |
|
|
if g == MAX: h=(2.0+( (b-r)/(MAX-MIN) ))*60 |
|
if g == MAX: h=(2.0+( (b-r)/(MAX-MIN) ))*60 |
|
if b == MAX: h=(4.0+( (r-g)/(MAX-MIN) ))*60 |
|
if b == MAX: h=(4.0+( (r-g)/(MAX-MIN) ))*60 |
|
# Actually h should be undefined in this case; you might want to |
|
|
# set it to "none" if that's the way Python works. In practice |
|
|
# there's nothing wrong with making it zero though. |
|
⚫ |
else: h = 0 |
|
|
|
|
⚫ |
H=int(h) |
|
⚫ |
S=int(s*100) |
|
|
V=int(v*100) |
|
|
|
|
|
|
⚫ |
# Format |
|
# H will intentionally come out < 0 for purples; have to wrap around |
|
|
# if H<0: H="undefined" |
|
|
|
if H < 0: H += 360 |
|
H=h |
|
⚫ |
if h != "undefined" : |
|
|
|
|
# Shouldn't be possible for S < 0; at most throw an error here but |
|
if h < 0: h+=360 |
|
# I'd just cut it. |
|
H=int(round(h)) |
|
# if S<0: S="undefined" |
|
S=s |
|
⚫ |
if s!= "undefined" : S=int(round(s*100)) |
|
|
|
|
⚫ |
|
|
⚫ |
V=int(round(v*100)) |
|
⚫ |
|
|
return (H,S,V) |
|
return (H,S,V) |
|
|
|
|
Line 84: |
Line 72: |
|
|
|
|
|
|
|
|
|
|
</syntaxhighlight> |
|
</pre> |
|