/** * 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. * * @param string $rgba_hex * A string specifing an RGBA color in the format '#RRGGBBAA'. * * @return array * An array with four elements for red, green, blue, and alpha. */ protected function hexToRgba($rgba_hex) { $rgbHex = Unicode::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; } }
/** * {@inheritdoc} */ protected function validateArguments(array $arguments) { // Ensure 'rectangle' is an expected PositionedRectangle object. if (!$arguments['rectangle'] instanceof PositionedRectangle) { throw new \InvalidArgumentException("PositionedRectangle passed to the 'draw_rectangle' operation is invalid"); } // Match color luma. if ($arguments['fill_color'] && $arguments['fill_color_luma']) { $arguments['fill_color'] = ColorUtility::matchLuma($arguments['fill_color']); } if ($arguments['border_color'] && $arguments['border_color_luma']) { $arguments['border_color'] = ColorUtility::matchLuma($arguments['border_color']); } return $arguments; }
/** * Processes a 'image_effects_color' form element. * * @param array $element * The form element to process. Properties used: * '#allow_null' - if set to TRUE, a checkbox is displayed to set the * color as a full transparency, In this case, color hex and opacity are * hidden, and the value returned is NULL. * '#allow_opacity' - if set to TRUE, a textfield is displayed to capture the * 'opacity' value, as a percentage. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * @param array $complete_form * The complete form structure. * * @return array * The processed element. */ public static function processImageEffectsColor(&$element, FormStateInterface $form_state, &$complete_form) { // Make sure element properties are set. $element += ['#allow_null' => FALSE, '#allow_opacity' => FALSE, '#description' => NULL, '#states' => NULL, '#title' => t('Color'), '#checkbox_title' => t('Transparent')]; // In case default value is transparent, set hex and opacity to default // values (white, fully opaque) so that if transparency is unchecked, // we have a starting value. $transparent = empty($element['#default_value']) ? TRUE : FALSE; $hex = $transparent ? '#FFFFFF' : Unicode::substr($element['#default_value'], 0, 7); $opacity = $transparent ? 100 : ColorUtility::rgbaToOpacity($element['#default_value']); $colorPlugin = \Drupal::service('plugin.manager.image_effects.color_selector')->getPlugin(); if ($element['#allow_null'] || $element['#allow_opacity']) { // More sub-fields are needed to define the color, wrap them in a // container fieldset. $element['container'] = array('#type' => 'fieldset', '#description' => $element['#description'], '#title' => $element['#title'], '#states' => $element['#states']); // Checkbox for transparency. if ($element['#allow_null']) { $element['container']['transparent'] = array('#type' => 'checkbox', '#title' => $element['#checkbox_title'], '#default_value' => $transparent); } // Color field. $element['container']['hex'] = $colorPlugin->selectionElement(array('#default_value' => $hex)); // States management for color field. $element['container']['hex']['#states'] = array('visible' => array(':input[name="' . $element['#name'] . '[container][transparent]"]' => array('checked' => FALSE))); // Textfield for opacity. if ($element['#allow_opacity']) { $element['container']['opacity'] = array('#type' => 'number', '#title' => t('Opacity'), '#default_value' => $opacity, '#maxlength' => 3, '#size' => 2, '#field_suffix' => '%', '#min' => 0, '#max' => 100, '#states' => array('visible' => array(':input[name="' . $element['#name'] . '[container][transparent]"]' => array('checked' => FALSE)))); } } else { // No transparency or opacity, straight color field. $options = $element; $options['#default_value'] = $hex; $element['hex'] = $colorPlugin->selectionElement($options); } unset($element['#description'], $element['#title']); return $element; }