Misplaced Pages

User:Kim Bruning/colorspace.py: Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
< User:Kim Bruning Browse history interactively← Previous editNext edit →Content deleted Content addedVisualWikitext
Revision as of 04:32, 7 April 2004 editChinasaur (talk | contribs)Extended confirmed users3,353 editsm Changed all my added comments to "# -->" so you can find them← Previous edit Revision as of 09:03, 7 April 2004 edit undoKim Bruning (talk | contribs)Autopatrolled, Extended confirmed users, Pending changes reviewers, Rollbackers20,995 edits thanks for fixing the code, modified slightly for encyclopedic answers :)Next edit →
Line 1: Line 1:
<pre> <pre>

#!/usr/bin/python #!/usr/bin/python


Line 30: Line 29:
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
# because: human readable input
# --> languages, you'd want to use a special value like "undef" or
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
Line 47: Line 38:
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
# Format
# --> 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=h
if ( h != "undefined" ):

H=int(h) if h < 0: h+=360
S=int(s*100) H=int(h)
S=s
if (s!= "undefined") : S=int(s*100)
V=int(v*100) V=int(v*100)
# --> H will intentionally come out < 0 for purples; have to wrap around
# if H<0: H="undefined"
if H < 0: H += 360

# --> Shouldn't be possible for S < 0 anymore
# if S<0: S="undefined"

return (H,S,V) return (H,S,V)



Revision as of 09:03, 7 April 2004

#!/usr/bin/python
# Diclaimer: 
# I only just picked up python. No apologies are made for this code.
def hextriplet2rgb(triplet) :
        # assuming the triplet *isn't* prefixed with 0x
        value=eval("0x"+triplet)
        r=(value & 0xFF0000) / 0x10000
        g=(value & 0x00FF00) /   0x100
        b=(value & 0x0000FF) 
        return (r,g,b)
# from http://en.wikipedia.org/CMYK
def rgb2cmyk (rgb) :
        r,g,b=rgb
        c,m,y=(1 - r / 255.0,1 - g / 255.0,1 - b / 255.0)
        C,M,Y,K= (c-min(c,m,y),m-min(c,m,y),y-min(c,m,y),min(c,m,y))
        # and additionally, *255 to convert proportion up to 8bits
        return tuple(map (lambda proportion: int(proportion*255), ))
# from http://en.wikipedia.org/HSV_color_space
def rgb2hsv (rgb) :
        # formula takes fp, bother!
        r,g,b = map (lambda x : x/255.0, rgb)
        MAX=max(r,g,b)
        MIN=min(r,g,b)
        # Using a "undefined" instead of None 
        # because: human readable input
        h="undefined" #default
        s="undefined" #default
        v=MAX
        if (v > 0) :
                s = (MAX - MIN) / MAX 
        if (s > 0 and s != "undefined" ):
                if r == MAX: h=(0.0+( (g-b)/(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
        # Format
        H=h
        if ( h != "undefined" ):
                if h < 0: h+=360
                H=int(h)
        S=s
        if (s!= "undefined") : S=int(s*100)
        V=int(v*100)
        return (H,S,V)
while True:
        print "\n\n"
        print "== Color Coordinates =="
        hextriplet = raw_input(" \'\'\']\'\'\' = #")
        rgb=hextriplet2rgb(hextriplet)
        print " \'\'\']\'\'\'    (r, g, b)    = ",rgb
        print " \'\'\']\'\'\'   (c, m, y, k) = ",rgb2cmyk(rgb)
        print " \'\'\']\'\'\'    (h, s, v)    = ",rgb2hsv(rgb)