[Tutor] Help with % conversion to output colour

bob gailer bgailer at gmail.com
Sat Jul 18 15:50:44 CEST 2009


Cassandra Peachey wrote:
>
> Hi,
>
>  
>
> I'm working on an assignment that requires the user RGB requested 
> output to create a colour block in fractional steps.  I'm pretty much 
> down to the last bit, however, I've hit a road block in that my 
> percentages are not showing as a colour block but as the <div> tag 
> percentages.
>
>  
>
> Any suggestions on what I'm missing to output the %'s into the colour 
> block showing colour?
>
>  
>
> Thanks for any help.
>
> Cassandra
>
>  
>
> import cgi
> form = cgi.FieldStorage()
>
> # print HTTP/HTML header stuff
> print "Content-type: text/html"
> print
> print"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>
> <html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en">
> <head>
> <title>Colour Blend</title>
> <style type="text/css">
> #blend {
> width: 10em;
> padding: 0;
> border: 1px solid black;
> margin-left: 3em;
> }
> .colourblock {
> width: 10em;
> height: 1em;
> }
> </style>
> </head>
> <body>
> <h1>Colour Blend</h1>
> <p>Here is a mix of the colours you specified:</p>
> <div id="blend">"""
>
> red1 = int(form["red1"].value)
> green1 = int(form["green1"].value)
> blue1 = int(form["blue1"].value)
>
> red2 = int(form["red2"].value)
> green2 = int(form["green2"].value)
> blue2 = int(form["blue2"].value)
>
> nsteps = int(form["steps"].value)
>
> if nsteps > 150:
>     nsteps = 150
>
> for i in range(0,nsteps-1):
>     fraction = (i+1.0)/nsteps
>     r = (1-fraction)*red1 + fraction*red2
>     g = (1-fraction)*green1 + fraction*green2
>     b = (1-fraction)*blue1 + fraction*blue2
>     print '<div class="colourblock" style="background-color: rgb(' + 
> str(r) + '%,' + str(g) + '%,' + str(b) + '%'"> </div>"
>

The output of that is (e.g.)
<div class="colourblock" style="background-color: rgb(30%,30%,40%> </div>
Can you see what it is missing?

> print "</body></html>"
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   


-- 
Bob Gailer
Chapel Hill NC
919-636-4239


More information about the Tutor mailing list