function iek_hex2rgb in Image effect kit 8
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|string|bool
- An associative RGB array if 'return_as_string' is set to FALSE.
- A RGB string that was split by the 'separator' will be returned if 'return_as_string' is set to TRUE.
- Return FALSE if an invalid Hex color code.
4 calls to iek_hex2rgb()
- ImageBorder::execute in src/
Plugin/ ImageToolkit/ Operation/ gd/ ImageBorder.php - Performs the actual manipulation on the image.
- ImagePadding::execute in src/
Plugin/ ImageToolkit/ Operation/ gd/ ImagePadding.php - Performs the actual manipulation on the image.
- ImageResize::execute in src/
Plugin/ ImageToolkit/ Operation/ gd/ ImageResize.php - Performs the actual manipulation on the image.
- ImageWatermark::execute in src/
Plugin/ ImageToolkit/ Operation/ gd/ ImageWatermark.php - Performs the actual manipulation on the image.
File
- ./
iek.module, line 182 - Contains "iek" module.
Code
function iek_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 = [];
// 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;
}