1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Style Color... how to convert rgb() to hex

Discussion in 'XenForo Development Discussions' started by Mr. Goodie2Shoes, Sep 17, 2012.

  1. Mr. Goodie2Shoes

    Mr. Goodie2Shoes Well-Known Member

    Well, the title says it all, I am calling @primaryMedium which needs to be in HEX format... but it's giving me in rgb(x, y, z) format...

    Thanks! :)
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    You might have to write a custom class for this...

    There are some colour related helpers but the end result is always RGB or RGBA.

    This one may help:

    Code:
    function rgb2hex($rgb)
    {
    
    $hex = "#";​
    $hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);​
    $hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);​
    $hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);​

    return $hex; // returns the hex value including the number sign (#)​
    }
     
    Mr. Goodie2Shoes likes this.
  3. Mr. Goodie2Shoes

    Mr. Goodie2Shoes Well-Known Member

    Thanks!
    and what will be the input? rgb(x, y, z) or just x, y, z?
     
    Chris D likes this.
  4. Chris D

    Chris D XenForo Developer Staff Member

    Just x, y, z

    255, 255, 255 would output #FFFFFF
     
    Mr. Goodie2Shoes likes this.
  5. Mr. Goodie2Shoes

    Mr. Goodie2Shoes Well-Known Member

    oh... forgot to mention, the input is actually an array... so you have to separate them by
    PHP:
    $rgb str_replace(' '''$rgb//just to make sure...
    $rgb explode(','$rgb);
     
    Chris D likes this.
  6. bogus

    bogus Guest

  7. Mr. Goodie2Shoes

    Mr. Goodie2Shoes Well-Known Member

    because i am fetching the style property automatically...
     
    Chris D likes this.
  8. Chris D

    Chris D XenForo Developer Staff Member

    I didn't ask, but I assume because he needs to do it programmatically for an add-on he's writing.
     
    Mr. Goodie2Shoes likes this.

Share This Page