You are here

protected function GDOperationTrait::hexToRgba in Image Effects 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/ImageToolkit/Operation/gd/GDOperationTrait.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\GDOperationTrait::hexToRgba()
  2. 8.2 src/Plugin/ImageToolkit/Operation/gd/GDOperationTrait.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\GDOperationTrait::hexToRgba()

Convert a RGBA hex to its RGBA integer GD components.

GD expects a value between 0 and 127 for alpha, where 0 indicates completely opaque while 127 indicates completely transparent. RGBA hexadecimal notation has #00 for transparent and #FF for fully opaque.

Parameters

string $rgba_hex: A string specifing an RGBA color in the format '#RRGGBBAA'.

Return value

array An array with four elements for red, green, blue, and alpha.

1 call to GDOperationTrait::hexToRgba()
GDOperationTrait::allocateColorFromRgba in src/Plugin/ImageToolkit/Operation/gd/GDOperationTrait.php
Allocates a GD color from an RGBA hexadecimal.

File

src/Plugin/ImageToolkit/Operation/gd/GDOperationTrait.php, line 45

Class

GDOperationTrait
Trait for GD image toolkit operations.

Namespace

Drupal\image_effects\Plugin\ImageToolkit\Operation\gd

Code

protected function hexToRgba($rgba_hex) {
  $rgbHex = mb_substr($rgba_hex, 0, 7);
  try {
    $rgb = Color::hexToRgb($rgbHex);
    $opacity = ColorUtility::rgbaToOpacity($rgba_hex);
    $alpha = 127 - floor($opacity / 100 * 127);
    $rgb['alpha'] = $alpha;
    return $rgb;
  } catch (\InvalidArgumentException $e) {
    return FALSE;
  }
}