You are here

function _textimage_hex2rgb in Textimage 7.2

Same name and namespace in other branches
  1. 5.2 textimage.module \_textimage_hex2rgb()
  2. 5 textimage.module \_textimage_hex2rgb()
  3. 6.2 textimage.module \_textimage_hex2rgb()

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

Parameters

string $hex: Hex representation of the color. Can be in the formats: '#ABC','ABC','#AABBCC','AABBCC'

Return value

array Array with three components RGB.

3 calls to _textimage_hex2rgb()
textimage_image_from_preset in ./textimage.module
Loads the Textimage preset and generates the GD image resource.
textimage_image_stroke_change_pixels in ./textimage.utils.inc
Utility function for image_stroke.
textimage_text_to_image in ./textimage.utils.inc
Generate an image containing text with the given parameters.

File

./textimage.utils.inc, line 352
Utility routines of Textimage.

Code

function _textimage_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,
  );
}