Snipped: rgb_str2hex

Converting rgb-values to hex (and vice versa) has been done about a gazillion time, but I haven’t found anything that could convert an rgb-string (e.g. “rgb(255, 255, 255)“). This might be returned through JavaScript, for example via backgroundColor.

This might save you anything up to five minutes, so here it is:

function rgb_str2hex($rgb_str){
  preg_match('!.*((.*),(.*),(.*))!',$rgb_str, $colors);
  return sprintf('%02X%02X%02X', trim($colors[1]), trim($colors[2]), trim($colors[3]));
}