Get background color from IE in rgb format?

Discussion in 'Javascript' started by teresni@ucia.gov, May 19, 2006.

  1. Guest

    We've got some JavaScript code that gets the current background color.
    It works, but Netscape returns it in rgb format, while IE returns it as
    the color
    text name (e.g., 'white'). We need to do some math calculations on the
    color,
    so I want the rgb values. How can I get IE to return the background
    color in rgb
    format, or how do I convert the text color to its rgb value? Thanks!
     
    , May 19, 2006
    #1
    1. Advertising

  2. Hal Rosser Guest

    Here's a link that may help
    http://web.njit.edu/~kevin/rgb.txt.html

    <> wrote in message
    news:...
    > We've got some JavaScript code that gets the current background color.
    > It works, but Netscape returns it in rgb format, while IE returns it as
    > the color
    > text name (e.g., 'white'). We need to do some math calculations on the
    > color,
    > so I want the rgb values. How can I get IE to return the background
    > color in rgb
    > format, or how do I convert the text color to its rgb value? Thanks!
    >


    Here's a link that may help
    http://web.njit.edu/~kevin/rgb.txt.html
    HTH
    Hal
     
    Hal Rosser, May 19, 2006
    #2
    1. Advertising

  3. Guest

    Thanks, but I don't want to hardcode the rgb values in my code. I was
    hoping for some attribute I could access or parameter I could pass to
    an existing method that would return the current background color as an
    rgb value rather than as the color name.
     
    , May 21, 2006
    #3
  4. James Black Guest

    wrote:
    > Thanks, but I don't want to hardcode the rgb values in my code. I was
    > hoping for some attribute I could access or parameter I could pass to
    > an existing method that would return the current background color as an
    > rgb value rather than as the color name.


    I haven't tried it, but you could look at
    http://www.phpied.com/rgb-color-parser-in-javascript/
     
    James Black, May 21, 2006
    #4
  5. Marc Guest

    <> schreef in bericht
    news:...
    > We've got some JavaScript code that gets the current background color.
    > It works, but Netscape returns it in rgb format, while IE returns it as
    > the color
    > text name (e.g., 'white'). We need to do some math calculations on the
    > color,
    > so I want the rgb values. How can I get IE to return the background
    > color in rgb
    > format, or how do I convert the text color to its rgb value? Thanks!
    >


    Only works for Internet Explorer!!!!!!!!!!

    Create a table with it's visibility style set to hidden, set this tables
    bgcolor
    to the named color:
    <table id="temptable" bgcolor="indigo"><tr><td></td></tr></table>

    then request back the color:
    document.getElementById("temptable").bgColor

    it should return a hex color...

    full sample:

    <html>
    <head>
    <style>
    #temptable{visibility:hidden}
    </style>
    <script>
    var hexChars = "0123456789ABCDEF";
    function Dec2Hex (Dec) {
    // this function isn't used in this sample
    var a = Dec % 16;
    var b = (Dec - a)/16;
    hex = "" + hexChars.charAt(b) + hexChars.charAt(a);
    return hex;
    }
    function Hex2Dec(HexVal){
    HexVal = HexVal.toUpperCase();
    var DecVal = 0;
    var temp = HexVal.substring(0,1);
    DecVal = (hexChars.indexOf(temp) * 16);
    temp = HexVal.substring(1);
    DecVal += hexChars.indexOf(temp);
    return DecVal;
    }

    function test(){
    var HexString = document.getElementById("temptable").bgColor;
    var r = Hex2Dec(HexString.substring(1,3));
    var g = Hex2Dec(HexString.substring(3,5));
    var b = Hex2Dec(HexString.substring(5,7));
    alert("hex: " + HexString + "\nrgb: " + r + " " + g + " " + b);
    }
    </script>
    </head>
    <body onclick="test()">

    <table id="temptable" bgcolor="indigo"><tr><td></td></tr></table>

    </body>
    </html>
     
    Marc, May 21, 2006
    #5
  6. Evertjan. Guest

    Marc wrote on 21 mei 2006 in comp.lang.javascript:

    > function Hex2Dec(HexVal){
    > HexVal = HexVal.toUpperCase();
    > var DecVal = 0;
    > var temp = HexVal.substring(0,1);
    > DecVal = (hexChars.indexOf(temp) * 16);
    > temp = HexVal.substring(1);
    > DecVal += hexChars.indexOf(temp);
    > return DecVal;
    >}
    >


    function Hex2Dec(x){
    var y = 0, z;
    for(var i=0;i<x.length;i++){
    z = x.toUpperCase().charCodeAt(i)
    y = 16*y+z-((z<58)?48:55)
    }
    return y;
    }

    [works for any length hex string within reason, even empty string]

    ==================

    function colorhex2dec(x){
    return x.replace(/(..)/g,function($1){return Hex2Dec($1)+' '})
    }

    alert(colorhex2dec('abcd10')) // 171 205 16



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., May 21, 2006
    #6
  7. Marc Guest

    <knip>
    >
    > function Hex2Dec(x){
    > var y = 0, z;
    > for(var i=0;i<x.length;i++){
    > z = x.toUpperCase().charCodeAt(i)
    > y = 16*y+z-((z<58)?48:55)
    > }
    > return y;
    > }
    >
    > [works for any length hex string within reason, even empty string]
    >
    > ==================
    >
    > function colorhex2dec(x){
    > return x.replace(/(..)/g,function($1){return Hex2Dec($1)+' '})
    > }
    >
    > alert(colorhex2dec('abcd10')) // 171 205 16
    >


    Ah! nice... ;-)
     
    Marc, May 22, 2006
    #7
  8. Evertjan. Guest

    Marc wrote on 22 mei 2006 in comp.lang.javascript:

    > <knip>


    Going Dutch?

    Chello.nl?

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., May 22, 2006
    #8
  9. Marc Guest

    >> <knip>
    >
    > Going Dutch?
    >
    > Chello.nl?
    >

    yeps... but now at work ;-)
     
    Marc, May 22, 2006
    #9
    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. tshad
    Replies:
    10
    Views:
    15,054
    tshad
    Jan 12, 2005
  2. =?Utf-8?B?a3N0cml5aG9u?=

    RGB color in c# help

    =?Utf-8?B?a3N0cml5aG9u?=, Sep 30, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    5,970
    =?Utf-8?B?a3N0cml5aG9u?=
    Sep 30, 2005
  3. Johan Holst Nielsen

    Regarding color conversion (RGB->CMYK)

    Johan Holst Nielsen, Nov 12, 2004, in forum: Python
    Replies:
    4
    Views:
    1,896
    Robert Kern
    Nov 13, 2004
  4. xeroxero

    Color Picker Post RGB Values?

    xeroxero, Sep 19, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    391
    Walter Wang [MSFT]
    Sep 22, 2006
  5. John Salerno

    OT: Question about RGB color method

    John Salerno, Apr 10, 2007, in forum: Python
    Replies:
    8
    Views:
    523
    John Salerno
    Apr 12, 2007
Loading...

Share This Page