Skip to content

Instantly share code, notes, and snippets.

@ThatGuySam
Created November 18, 2014 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThatGuySam/f2a691a94f8f4f82476c to your computer and use it in GitHub Desktop.
Save ThatGuySam/f2a691a94f8f4f82476c to your computer and use it in GitHub Desktop.
Convert Hex colors to RGB
<?php
function hex2rgb($hex) {
$hex = str_replace("#", "", $hex);
if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else {
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
}
$rgb = array($r, $g, $b);
//return implode(",", $rgb); // returns the rgb values separated by commas
return $rgb; // returns an array with the rgb values
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment