View Full Version : hex color to rgb values?
Supah!
Nov 9th 2008, 10:41 am
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?
papa_face
Nov 9th 2008, 11:59 am
Google is lovely.
http://snipplr.com/view/4621/convert-hex-to-rgb--rgb-to-hex/
xrvel
Nov 9th 2008, 6:08 pm
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;
}
?>
joebert
Nov 10th 2008, 4:00 am
$int = hexdec(str_replace('#', '', $hex));
$r = $int >> 16 & 255;
$g = $int >> 8 & 255
$b = $int & 255;
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.