You are here

function iek_image_hex2rgb in Image effect kit 7

Converts color format from Hex to RGB.

Parameters

string $hex_str: A Hex color format like '#cccccc'.

bool $return_as_string: A boolean flag to indicate to return a string or an array.

string $separator: A character or string to join the RGB array elements.

Return value

array An associative RGB array if 'return_as_string' is set to FALSE.

string A RGB string that was split by the 'separator' will be returned if 'return_as_string' is set to TRUE.

bool Return FALSE if an invalid Hex color code.

6 calls to iek_image_hex2rgb()
iek_gd_border in ./iek.gd.inc
Add an image border by using the GD toolkit.
iek_gd_padding in ./iek.gd.inc
Add an image padding by using the GD toolkit.
iek_gd_resize in ./iek.gd.inc
Resize an image by using the GD toolkit.
iek_gd_watermark in ./iek.gd.inc
Add a watermark text on an image by using the GD toolkit.
iek_image_effect_blank_margin_bg_color_validate in ./iek.module
Validation function for color format.

... See full list

File

./iek.module, line 1353
Primarily Drupal hooks and global API functions to manipulate image styles.

Code

function iek_image_hex2rgb($hex_str, $return_as_string = FALSE, $separator = ',') {

  // Gets a proper Hex string.
  $hex_str = preg_replace("/[^0-9A-Fa-f]/", '', $hex_str);
  $rgb_array = array();

  // If a proper Hex code, convert using bitwise operation.
  if (strlen($hex_str) == 6) {
    $border_color_val = hexdec($hex_str);
    $rgb_array['red'] = 0xff & $border_color_val >> 0x10;
    $rgb_array['green'] = 0xff & $border_color_val >> 0x8;
    $rgb_array['blue'] = 0xff & $border_color_val;
  }
  elseif (strlen($hex_str) == 3) {
    $rgb_array['red'] = hexdec(str_repeat(substr($hex_str, 0, 1), 2));
    $rgb_array['green'] = hexdec(str_repeat(substr($hex_str, 1, 1), 2));
    $rgb_array['blue'] = hexdec(str_repeat(substr($hex_str, 2, 1), 2));
  }
  else {

    // Invalid Hex color code.
    return FALSE;
  }

  // Returns the RGB string or the associative array.
  return $return_as_string ? implode($separator, $rgb_array) : $rgb_array;
}