hex color to rgb values?

Discussion in 'PHP' started by Supah!, Nov 9, 2008.

  1. #1
    I'm making a hit counter service, I want users to be able input hex colors, but then convert that to rgb values when the script is run.

    How would i do that?
     
    Supah!, Nov 9, 2008 IP
  2. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
  3. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #3
    I made this function for my scripts. You can use it :)
    <?php
    
    $color = '#FF00CC';
    $new_color = getRGBCode($color);
    print_r($new_color);
    
        function getRGBCode($s) {
            $rgbArr = array();
            $s = str_replace('#','',$s);
            if (6 == strlen($s)) {
                $rgbArr[] = hexdec(substr($s, 0, 2));
                $rgbArr[] = hexdec(substr($s, 2, 2));
                $rgbArr[] = hexdec(substr($s, 4, 2));
            }
            return $rgbArr;
        }
    ?>
    PHP:
     
    xrvel, Nov 9, 2008 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    $int = hexdec(str_replace('#', '', $hex));
    $r = $int >> 16 & 255;
    $g = $int >> 8 & 255
    $b = $int & 255;
    Code (markup):
     
    joebert, Nov 10, 2008 IP