Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2005
    Location
    UK
    Posts
    90
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Hex code from backgroundColor in firefox

    Hi.

    I want to get the colour in HEX format using "thing.style.backgroundColor". Like "#FFFFFF" or whatever. IE gives this but FF gives it as "rgb(0,0,0)" format.

    Is there a simple function/method of getting it as hex? (or a conversion function)

    Thanks.
    Last edited by _Dan; 06-26-2006 at 07:56 PM.
    Daniel
    XZIST.org

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,521
    Thanks
    3
    Thanked 507 Times in 494 Posts
    Code:
    <script language="JavaScript">
    <!--
    R = HexToR("#FFFFFF");
    G = HexToG("#FFFFFF");
    B = HexToB("#FFFFFF");
    
    function HexToR(h) { return parseInt((cutHex(h)).substring(0,2),16) }
    function HexToG(h) { return parseInt((cutHex(h)).substring(2,4),16) }
    function HexToB(h) { return parseInt((cutHex(h)).substring(4,6),16) }
    function cutHex(h) { return (h.charAt(0)=="#") ? h.substring(1,7) : h}
    
    function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
    
    function toHex(N) {
     if (N==null) return "00";
     N=parseInt(N); if (N==0 || isNaN(N)) return "00";
     N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
     return "0123456789ABCDEF".charAt((N-N%16)/16)
          + "0123456789ABCDEF".charAt(N%16);
    }
    
    //-->
    </script>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    New Coder
    Join Date
    Feb 2005
    Location
    UK
    Posts
    90
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'd still have to parse the string to get the rgb numbers. What it returns should be a standard.

    Thanks.
    Daniel
    XZIST.org

  • #4
    New Coder
    Join Date
    Feb 2005
    Location
    UK
    Posts
    90
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm sure a cleaner "RGBstrToHex" could be written but hey it works!
    Code:
    function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
    
    function toHex(N)
    {
     if (N==null) return "00";
     N=parseInt(N);
     if (N==0 || isNaN(N)) return "00";
     N=Math.max(0,N);
     N=Math.min(N,255);
     N=Math.round(N);
     return ("0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16) );
    }
    
    function RGBstrToHex(str)
    {
    	if (str.indexOf('rgb') == -1) return str;
    	str = str.replace("rgb", '');
    	str = str.replace("(", '');
    	str = str.replace(")", '');
    	str = str.replace(" ", '');
    	str = str.replace(" ", '');
    	var temp = new Array();
    	temp = str.split(',');
    	var r = temp[0];
    	var g = temp[1];
    	var b = temp[2];
    	return "#" + RGBtoHex(r,g,b);
    }
    Daniel
    XZIST.org


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •