User:Kim Bruning/colorspace.py

> example run

#!/usr/bin/python
# colorspace.py
# calculates RGB, CMYK and HSV values from a hex triplet


# Diclaimer: 
# 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) :
        # 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/wiki/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), [C,M,Y,K]))

# from http://en.wikipedia.org/wiki/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 we want encyclopedic style output
        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(round(h))
        S=s
        if (s!= "undefined") : S=int(round(s*100))
        
        V=int(round(v*100))
        
        return (H,S,V)

while True:
        print "\n\n"
        print "== Color Coordinates =="
        hextriplet = raw_input(" \'\'\'[[Hex triplet]]\'\'\' = #")
        rgb=hextriplet2rgb(hextriplet)
        print " \'\'\'[[RGB]]\'\'\'    (r, g, b)    = ",rgb
        print " \'\'\'[[CMYK]]\'\'\'   (c, m, y, k) = ",rgb2cmyk(rgb)
        print " \'\'\'[[HSV color space|HSV]]\'\'\'    (h, s, v)    = ",rgb2hsv(rgb)


Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools