Converting from integers to output

Peter Maas peter.maas at mplusr.de
Thu Jul 15 05:04:00 EDT 2004


Peter Maas schrieb:
> import cgi
> 
> def longtorgb(c):
>     '''long to (r,g,b) tuple assuming that the
>     r bits are high and the b bits are low'''
[...]

Sorry, serious error. longtorgb is nonlinear so the _tuples_
have to be mixed, not the integers. You probably want to work
with tuples only so you can drop the conversion crap. Correct
code:

import cgi

def rgbtolong(rgb):
     '''(r,g,b) tuple to long assuming that the
     r bits are high and the b bits are low'''

     return (rgb[0] << 16) + (rgb[1] << 8) + rgb[2]

def longtorgb(c):
     '''long to (r,g,b) tuple assuming that the
     r bits are high and the b bits are low'''

     return ((c & 0xff0000) >> 16, (c & 0x00ff00) >> 8, c & 0xff)

if __name__ == '__main__':

     form = cgi.FieldStorage()

     # fetch input
     #col1 = int(form['color1'].value)
     #col2 = int(form['color2'].value)
     col1 = 0xff      # blue
     col2 = 0xff0000  # red
     rcol1 = longtorgb(col1)
     rcol2 = longtorgb(col2)
     nsteps = 20

     # print header
     print 'Content-type: text/html\n'

     # print static part 1
     print '''
         <html>
         <head>
         <title>
         Blending colors
         </title>
         </head>
         <body>
         Here is a mix of the two colours you specified:
     '''

     # print blend fields
     for i in range(nsteps+1):
         fraction = float(i)/nsteps
         mix = (int((1.0-fraction)*rcol1[0] + fraction*rcol2[0]),
                int((1.0-fraction)*rcol1[1] + fraction*rcol2[1]),
                int((1.0-fraction)*rcol1[2] + fraction*rcol2[2]))
         print '''
             <div style="width: 100px; height: 5px; background: rgb(%d,%d,%d)">
                 step %d
             </div>
         ''' % (mix+(i,))

     # print static part 2
     print '''
         </body>
         </html>
     '''

Mit freundlichen Gruessen,

Peter Maas

-- 
-------------------------------------------------------------------
Peter Maas,  M+R Infosysteme,  D-52070 Aachen,  Tel +49-241-93878-0
E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
-------------------------------------------------------------------



More information about the Python-list mailing list