You are here

function _signaturefield_hex2rgb in SignatureField 6

Same name and namespace in other branches
  1. 7.2 signaturefield.module \_signaturefield_hex2rgb()
  2. 7 signaturefield.module \_signaturefield_hex2rgb()

Convert a hex color representation to it's rgb integer components.

Parameters

$hex: Hex representation of the color. Can be in the formats: '#ABC','ABC','#AABBCC','AABBCC' @return Array with three components RGB.

1 call to _signaturefield_hex2rgb()
signaturefield_json_to_image in ./signaturefield.module

File

./signaturefield.module, line 107
Signature Field module.

Code

function _signaturefield_hex2rgb($hex) {
  $r = $g = $b = '';
  $hex = ltrim($hex, '#');
  if (preg_match('/^[0-9a-f]{3}$/i', $hex)) {

    // 'FA3' is the same as 'FFAA33' so r=FF, g=AA, b=33
    $r = str_repeat($hex[0], 2);
    $g = str_repeat($hex[1], 2);
    $b = str_repeat($hex[2], 2);
  }
  elseif (preg_match('/^[0-9a-f]{6}$/i', $hex)) {

    // #FFAA33 or r=FF, g=AA, b=33
    $r = drupal_substr($hex, 0, 2);
    $g = drupal_substr($hex, 2, 2);
    $b = drupal_substr($hex, 4, 2);
  }
  $r = hexdec($r);
  $g = hexdec($g);
  $b = hexdec($b);
  return array(
    $r,
    $g,
    $b,
  );
}