473,385 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Converting from integers to output

I'm sure this is so easy that it will hurt for anyone to read it, but I
really need some direction. I'm trying to create a color chart (RGB) that
shows steps between two different colors as entered by the user. I know the
web script must convert the values into integers, store them as variables
and then output them, but I cannot seem to establish the calculation for
this. I'm attaching the html I've got, that works when the page is static,
and I know I need to create a fraction for each color by using the formula,
fraction = (i+1.0)/nsteps
r = (1-fraction)*red1 + fraction*red2

but the program won't recognize i... anyway, here is the html...if anyone
can give me any help I'd be so grateful!!

Thanks,
Krista

import cgi
form = cgi.FieldStorage()
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 two colours you specified:
</p>
<div id="blend">

<div class="colourblock" style="background-color: rgb(100.0%,10.0%,10.0%)">
</div>
<div class="colourblock" style="background-color: rgb(100.0%,20.0%,20.0%)">
</div>
<div class="colourblock" style="background-color: rgb(100.0%,30.0%,30.0%)">
</div>
<div class="colourblock" style="background-color: rgb(100.0%,40.0%,40.0%)">
</div>
<div class="colourblock" style="background-color: rgb(100.0%,50.0%,50.0%)">
</div>
<div class="colourblock" style="background-color: rgb(100.0%,60.0%,60.0%)">
</div>
<div class="colourblock" style="background-color: rgb(100.0%,70.0%,70.0%)">
</div>
<div class="colourblock" style="background-color: rgb(100.0%,80.0%,80.0%)">
</div>
<div class="colourblock" style="background-color: rgb(100.0%,90.0%,90.0%)">
</div>
<div class="colourblock" style="background-color:
rgb(100.0%,100.0%,100.0%)"> </div>

</div>

</body>
</html>"""


Jul 18 '05 #1
7 1793
On Wed, 14 Jul 2004 05:55:49 GMT, "Krista Bailie" <ba*****@telus.net>
declaimed the following in comp.lang.python:
I'm sure this is so easy that it will hurt for anyone to read it, but I
really need some direction. I'm trying to create a color chart (RGB) that
shows steps between two different colors as entered by the user. I know the
web script must convert the values into integers, store them as variables
and then output them, but I cannot seem to establish the calculation for
this. I'm attaching the html I've got, that works when the page is static,
and I know I need to create a fraction for each color by using the formula,
fraction = (i+1.0)/nsteps
r = (1-fraction)*red1 + fraction*red2

but the program won't recognize i... anyway, here is the html...if anyone
can give me any help I'd be so grateful!!
Where's the INPUT HTML? I presume you display something from
which the user is supposed to pick two colors. If "i" is one of the
fields being returned from the input HTML then...
Thanks,
Krista

import cgi
form = cgi.FieldStorage()
.... it would be accessed as
form["i"]
(unless I misread the help file for the cgi module).

Without knowing the input HTML, it is difficult to guess what
the data looks like. Does the user enter first RGB, then second RGB? Are
the RGB entries triplets in the range 0..255, or 0.0 .. 1.0, or
singletons of the form RRGGBB where each RR, GG, BB is in the range
00..FF?

It looks like the output wants percentages, so you'd have to
divide each part of the triplet by (100.0/255.0), or just multiple by
100.0 if the range was 0.0 .. 1.0. I leave the third variant for the
student <G>
start = int(raw_input("Start value> "))
end = int(raw_input("End value> "))
steps = int(raw_input("Number of steps> "))

if start > end:
print "Swapping start and end points"
(start, end) = (end, start)

span = end - start
incr = float(span) / steps

print "Data Point Value"
for n in range(steps+1):
print " %5s %5s (%5s)" % (n,
int(start + incr * n),
(start + incr * n))
Input:
5
18
10
Output:
Data Point Value
0 5 ( 5.0)
1 6 ( 6.3)
2 7 ( 7.6)
3 8 ( 8.9)
4 10 ( 10.2)
5 11 ( 11.5)
6 12 ( 12.8)
7 14 ( 14.1)
8 15 ( 15.4)
9 16 ( 16.7)
10 18 ( 18.0)

Input:
240
128
8
Output:
Swapping start and end points
Data Point Value
0 128 (128.0)
1 142 (142.0)
2 156 (156.0)
3 170 (170.0)
4 184 (184.0)
5 198 (198.0)
6 212 (212.0)
7 226 (226.0)
8 240 (240.0)

Extrapolate for three color components (I'm assuming you want to
do this in RGB coordinates -- you'll likely get different results if you
convert the end-points to HSV and interpolate through that coordinate,
then convert back to RGB)
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #2
Krista Bailie schrieb:
and I know I need to create a fraction for each color by using the formula,
fraction = (i+1.0)/nsteps
r = (1-fraction)*red1 + fraction*red2

but the program won't recognize i... anyway, here is the html...if anyone
can give me any help I'd be so grateful!!


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

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

colorstring = ""
for i in range(nsteps+1):
fraction = i/nsteps
mix = (1-fraction)*red1 + fraction*red2
rgb = longtorgb(mix)
colorstring += "color %d is rgb(%d,%d,%d)\n" % ((i,)+rgb)

This is a start. You can insert tuples in a html string with the
formatting operator (%). Literal % characters in the string have to
be doubled if you are using the formatting operator.

Mit freundlichen Gruessen,

Peter Maas

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0
E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
-------------------------------------------------------------------
Jul 18 '05 #3
On Wed, 14 Jul 2004, Dennis Lee Bieber wrote:
Where's the INPUT HTML? I presume you display something from
which the user is supposed to pick two colors. If "i" is one of the
fields being returned from the input HTML then... ... it would be accessed as
form["i"]
(unless I misread the help file for the cgi module).


You did ;) It's form["i"].value (form["i"] returns a FieldStorage
instance).

Jul 18 '05 #4
On Wed, 14 Jul 2004 10:05:41 -0400, Christopher T King
<sq******@WPI.EDU> declaimed the following in comp.lang.python:

You did ;) It's form["i"].value (form["i"] returns a FieldStorage
instance).
Ah well... It would, at least, have returned /something/,
opposed to referencing an undefined i

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #5
Okay, I've almost got this, but I still don't understand how to actually
make the data appear. It is supposed to show up in bars(nsteps) that blend
one color to the other, but I can only get the static html colors to show.
Should I just erase the html div class info and put in a formula instead?
(File is below...)

Many, many thanks,
Krista

import cgi
form = cgi.FieldStorage()
def longtorgb(c):
'''long to (r,g,b) tuple assuming that r
the r bits are high and the b bits are low'''

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

def nsteps():
nsteps = int(raw_input("steps> "))
def red1():
red1 = int(raw_input("red1> "))
def red2():
red2 = int(raw_input("red2> "))
def blue1():
blue1 = int(raw_input("blue1> "))
def blue2():
blue2 = int(raw_input("blue2> "))
def green1():
green1 = int(raw_input("green1> "))
def green2():
green2 = int(raw_input("green2> "))

colorstring = ""
def fraction():
fraction = i/nsteps
def mix():
mix = (1-fraction)*blue1 + fraction*blue2
mix = (1-fraction)*red1 + fraction*red2
mix = (1-fraction)*green1 + fraction*green2
def rgb():
rgb = longtorgb(mix)
colorstring += "color %d is rgb(%d,%d,%d)\n" % ((i,)+rgb)

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 two colours you specified:
</p>
<div id="blend">

<div class="colourblock" style="background-color: rgb(100.0%,10.0%,10.0%)">
</div>
<div class="colourblock" style="background-color: rgb(100.0%,20.0%,20.0%)">
</div>
<div class="colourblock" style="background-color: rgb(100.0%,30.0%,30.0%)">
</div>
<div class="colourblock" style="background-color: rgb(100.0%,40.0%,40.0%)">
</div>
<div class="colourblock" style="background-color: rgb(100.0%,50.0%,50.0%)">
</div>
<div class="colourblock" style="background-color: rgb(100.0%,60.0%,60.0%)">
</div>
<div class="colourblock" style="background-color: rgb(100.0%,70.0%,70.0%)">
</div>
<div class="colourblock" style="background-color: rgb(100.0%,80.0%,80.0%)">
</div>
<div class="colourblock" style="background-color: rgb(100.0%,90.0%,90.0%)">
</div>
<div class="colourblock" style="background-color:
rgb(100.0%,100.0%,100.0%)"> </div>
</div>

</div>

</body>
</html>"""

"Krista Bailie" <ba*****@telus.net> wrote in message
news:FV3Jc.43514$Rf.4604@edtnps84...
I'm sure this is so easy that it will hurt for anyone to read it, but I
really need some direction. I'm trying to create a color chart (RGB) that
shows steps between two different colors as entered by the user. I know the web script must convert the values into integers, store them as variables
and then output them, but I cannot seem to establish the calculation for
this. I'm attaching the html I've got, that works when the page is static, and I know I need to create a fraction for each color by using the formula, fraction = (i+1.0)/nsteps
r = (1-fraction)*red1 + fraction*red2

but the program won't recognize i... anyway, here is the html...if anyone
can give me any help I'd be so grateful!!

Thanks,
Krista

import cgi
form = cgi.FieldStorage()
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 two colours you specified:
</p>
<div id="blend">

<div class="colourblock" style="background-color: rgb(100.0%,10.0%,10.0%)"> </div>
<div class="colourblock" style="background-color: rgb(100.0%,20.0%,20.0%)"> </div>
<div class="colourblock" style="background-color: rgb(100.0%,30.0%,30.0%)"> </div>
<div class="colourblock" style="background-color: rgb(100.0%,40.0%,40.0%)"> </div>
<div class="colourblock" style="background-color: rgb(100.0%,50.0%,50.0%)"> </div>
<div class="colourblock" style="background-color: rgb(100.0%,60.0%,60.0%)"> </div>
<div class="colourblock" style="background-color: rgb(100.0%,70.0%,70.0%)"> </div>
<div class="colourblock" style="background-color: rgb(100.0%,80.0%,80.0%)"> </div>
<div class="colourblock" style="background-color: rgb(100.0%,90.0%,90.0%)"> </div>
<div class="colourblock" style="background-color:
rgb(100.0%,100.0%,100.0%)"> </div>

</div>

</body>
</html>"""


Jul 18 '05 #6
Krista Bailie schrieb:
Okay, I've almost got this, but I still don't understand how to actually
make the data appear.

import cgi

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 & 0xff00) >> 8, c & 0xff)

if __name__ == '__main__':
form = cgi.FieldStorage()

# fetch input
#col1 = int(form['color1'].value)
#col2 = int(form['color2'].value)
# test only , to be deleted
col1 = 0xff # blue
col2 = 0xff00 # green
nsteps = 5
print col1, col2

# 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)*col1 + fraction*col2)
rgb = longtorgb(mix)
print '''
<div style="width: 50px; height: 20px; background: rgb(%d,%d,%d)">
step %d
</div>
''' % (rgb+(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')
-------------------------------------------------------------------
Jul 18 '05 #7
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')
-------------------------------------------------------------------
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Govind | last post by:
Hi All, I want to Convert 32 bit integers to byte in right alighed format . For 32 = the usual way is BitConverter.GetBytes(int32)==> xx xx 00 00 , but i want right aligned like 00 00 xx xx.Is...
12
by: David Williams | last post by:
Hi all, i have been able to convert an ASCII character to an INT however im lost as to how to change them back. Cant find anything on the net (though im probably looking in the wrong places!)....
8
by: prabha | last post by:
Hello Everybody, I have to conert the word doc to multiple html files,according to the templates in the word doc. I had converted the word to xml.Also through Exsl ,had finished the multiple...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
21
by: py_genetic | last post by:
Hello, I'm importing large text files of data using csv. I would like to add some more auto sensing abilities. I'm considing sampling the data file and doing some fuzzy logic scoring on the...
12
by: cmdolcet69 | last post by:
Can anyone give me some ideas on how to convert the following program to vb 2003? I would like to jsut take this code and implement it in vb. def.h /* Global Type Definitions */ typedef ...
22
by: kotlakirankumar | last post by:
please help me out the program for converting the integers to roman numerals using files in the c language from 1-5000 range send the program to my mail id ::::::: kotlakirankumar@gmail.com...
13
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
0
by: Paul McGuire | last post by:
On Sep 2, 12:36 pm, hofer <bla...@dungeon.dewrote: All that sed'ing, grep'ing and awk'ing, you might want to take a look at pyparsing. Here is a pyparsing take on your posted problem: from...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.