hex(dummy)[2:] - issue...

  • Thread starter Florian Wollenschein
  • Start date
F

Florian Wollenschein

Hi there,

I need some advice :)
I'm using hex(dummy)[2:] to represent a color in hexadecimal format for
the bgcolor in an html file. dummy is the color value in RGB of course...

Now, if there's an R, G or B value of zero, this command only prints one
single 0 instead of two. What's wrong with the code?

Thanks,
Listick
http://www.listick.org
 
T

Tim Chase

I need some advice :)
I'm using hex(dummy)[2:] to represent a color in hexadecimal format for
the bgcolor in an html file. dummy is the color value in RGB of course...

Now, if there's an R, G or B value of zero, this command only prints one
single 0 instead of two. What's wrong with the code?

You can try

PLACES = 2 # 6?
hex(dummy)[2:].zfill(PLACES)

Alternatively, you can output decimal numbers in HTML/CSS with

rgb(r, g, b)

such as

style="rgb(255,0,0)"

However, I recommend doing this via CSS unless you have a strong
reason to sully your HTML with style information.

-tkc
 
M

MRAB

Florian said:
Hi there,

I need some advice :)
I'm using hex(dummy)[2:] to represent a color in hexadecimal format for
the bgcolor in an html file. dummy is the color value in RGB of course...

Now, if there's an R, G or B value of zero, this command only prints one
single 0 instead of two. What's wrong with the code?
hex() returns '0x' followed by no more digits than are necessary:
'0xf'

Try "%02X" instead (it'll pad with leading zeros up to a width of 2):
'0F'
 
F

Florian Wollenschein

Tim said:
I need some advice :)
I'm using hex(dummy)[2:] to represent a color in hexadecimal format
for the bgcolor in an html file. dummy is the color value in RGB of
course...

Now, if there's an R, G or B value of zero, this command only prints
one single 0 instead of two. What's wrong with the code?

You can try

PLACES = 2 # 6?
hex(dummy)[2:].zfill(PLACES)

Alternatively, you can output decimal numbers in HTML/CSS with

rgb(r, g, b)

such as

style="rgb(255,0,0)"

However, I recommend doing this via CSS unless you have a strong reason
to sully your HTML with style information.

-tkc

hey tkc,

I used your first alternative. This did it! Thanks a lot.
I think I will write the style stuff into a .css file in the next few
days but until then I'm just working on get my program to work...

Listick
http://www.listick.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top