473,543 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.FieldStorag e()
print "Content-type: text/html"
print
print """<!DOCTYP E 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="colourbl ock" style="backgrou nd-color: rgb(100.0%,10.0 %,10.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,20.0 %,20.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,30.0 %,30.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,40.0 %,40.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,50.0 %,50.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,60.0 %,60.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,70.0 %,70.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,80.0 %,80.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,90.0 %,90.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color:
rgb(100.0%,100. 0%,100.0%)"> </div>

</div>

</body>
</html>"""


Jul 18 '05 #1
7 1801
On Wed, 14 Jul 2004 05:55:49 GMT, "Krista Bailie" <ba*****@telus. net>
declaimed the following in comp.lang.pytho n:
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.FieldStorag e()
.... 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.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.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 'cGV0ZXIubWFhc0 BtcGx1c3IuZGU=\ n'.decode('base 64')
-------------------------------------------------------------------
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.E DU> declaimed the following in comp.lang.pytho n:

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.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.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.FieldStorag e()
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)*green 1 + 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 """<!DOCTYP E 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="colourbl ock" style="backgrou nd-color: rgb(100.0%,10.0 %,10.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,20.0 %,20.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,30.0 %,30.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,40.0 %,40.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,50.0 %,50.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,60.0 %,60.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,70.0 %,70.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,80.0 %,80.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,90.0 %,90.0%)">
</div>
<div class="colourbl ock" style="backgrou nd-color:
rgb(100.0%,100. 0%,100.0%)"> </div>
</div>

</div>

</body>
</html>"""

"Krista Bailie" <ba*****@telus. net> wrote in message
news:FV3Jc.4351 4$Rf.4604@edtnp s84...
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.FieldStorag e()
print "Content-type: text/html"
print
print """<!DOCTYP E 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="colourbl ock" style="backgrou nd-color: rgb(100.0%,10.0 %,10.0%)"> </div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,20.0 %,20.0%)"> </div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,30.0 %,30.0%)"> </div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,40.0 %,40.0%)"> </div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,50.0 %,50.0%)"> </div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,60.0 %,60.0%)"> </div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,70.0 %,70.0%)"> </div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,80.0 %,80.0%)"> </div>
<div class="colourbl ock" style="backgrou nd-color: rgb(100.0%,90.0 %,90.0%)"> </div>
<div class="colourbl ock" style="backgrou nd-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.FieldStorag e()

# 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 'cGV0ZXIubWFhc0 BtcGx1c3IuZGU=\ n'.decode('base 64')
-------------------------------------------------------------------
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.FieldStorag e()

# 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 'cGV0ZXIubWFhc0 BtcGx1c3IuZGU=\ n'.decode('base 64')
-------------------------------------------------------------------
Jul 18 '05 #8

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

Similar topics

2
2660
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 there any way. Regards, Govind.
12
70517
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!). Could anyone help me out? Thanks Dave
8
5722
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 output html files. The problem is while reading through the worddoc paragraph,the special characters are not identified. So in the xml file,it's...
2
3991
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 functions strtol, strtod, and strtoul, defined in <cstdlib>, convert a null- terminated character string to a long int, double, or unsigned long. You can...
21
1979
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 attributes (colls in a data base/ csv file, eg. height weight income etc.) to determine the most efficient 'type' to convert the attribute coll into...
12
2105
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 unsigned char byte; // 'byte' is an 8-bit unsigned value
22
3182
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 thank you all
13
10378
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 set of integers stored in an array.It would be a great help if you could provide some code for it.I tried the function fscanf but by that I am able...
0
498
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 pyparsing import LineEnd, Word, nums, LineStart, OneOrMore, restOfLine test = """
0
7412
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7697
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5892
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4900
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3395
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
979
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
648
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.