colorchooser.py

Download: project files

This Python CGI script allows the user to select RGB colors, and displays them by updating the HTML form itself.

Try it: colorchooser.html


01: #!/usr/bin/python
02: import cgi, cgitb, os, sys
03: cgitb.enable(); # formats errors in HTML
04: 
05: sys.stderr = sys.stdout
06: print "Content-type: text/html"
07: print
08: print '''<html>
09: <head>
10: <title>Color Chooser</title>
11: </head>
12: <body>
13: <h1>Color Chooser</h1>'''
14: 
15: form = cgi.FieldStorage()
16: 
17: 
18: if not (form.has_key("red") and form.has_key("green") and form.has_key("blue") ):
19:     print "<b>Error</b>: request did not provide color specifications."
20:     red = 0
21:     green = 255
22:     blue = 0
23: else:
24:     red = int(form["red"].value);
25:     green = int(form["green"].value);
26:     blue = int(form["blue"].value);
27: 
28: if (red>255): red=255
29: if (green>255): green=255
30: if (blue>255): blue=255
31: 
32: print '''<form action="colorchooser.py" method=GET>
33: 
34: <table>
35: <tr><td>red<td><input type="text" name="red" value="%d" size=3>
36: <td rowspan=3 width=16><br>''' % red
37: 
38: print '<td rowspan=3 width=96 bgcolor="%02X%02X%02X"><br>'% (red, green,blue)
39: print '<tr><td>green<td> <input type="text" name="green" value="%d" size=3>' % green
40: print '<tr><td>blue<td> <input type="text" name="blue" value="%d" size=3>' % blue
41: 
42: 
43: print '''</table>
44: 
45: <p><input type="submit" value="Update">
46: 
47: </form>
48: 
49: <p><hr>
50: 
51: <p>Maintained by <a href="https://johnloomis.org"> John Loomis</a>, 
52: last updated <i>29 February 2008 </i></p>
53: 
54: </body>
55: </html>'''


Results

Try it: colorchooser.html


Maintained by John Loomis, updated Thu Feb 28 22:32:29 2008