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

Discussion in 'Python' started by Florian Wollenschein, May 6, 2009.

  1. 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
     
    Florian Wollenschein, May 6, 2009
    #1
    1. Advertising

  2. Florian Wollenschein

    Tim Chase Guest

    > 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
     
    Tim Chase, May 6, 2009
    #2
    1. Advertising

  3. Florian Wollenschein

    MRAB Guest

    Florian Wollenschein wrote:
    > 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:

    >>> hex(0xFF)

    '0xff'
    >>> hex(0xF)

    '0xf'

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

    >>> "%02X" % 0xFF

    'FF'
    >>> "%02X" % 0xF

    '0F'
     
    MRAB, May 6, 2009
    #3
  4. Tim Chase wrote:
    >> 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
     
    Florian Wollenschein, May 7, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,398
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    528
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    616
  4. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    19,132
  5. tim
    Replies:
    2
    Views:
    1,595
    Dennis Lee Bieber
    Nov 23, 2005
Loading...

Share This Page