validateHex example

'#default_value' => $this->configuration['random'],
      '#title' => $this->t('Randomize'),
      '#description' => $this->t('Randomize the rotation angle for each image. The angle specified above is used as a maximum.'),
    ];
    return $form;
  }

  /** * {@inheritdoc} */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    if (!$form_state->isValueEmpty('bgcolor') && !Color::validateHex($form_state->getValue('bgcolor'))) {
      $form_state->setErrorByName('bgcolor', $this->t('Background color must be a hexadecimal color value.'));
    }
  }

  /** * {@inheritdoc} */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form$form_state);

    $this->configuration['degrees'] = $form_state->getValue('degrees');
    
$arguments['height'] = (int) round($arguments['height']);

    // Fail when width or height are 0 or negative.     if ($arguments['width'] <= 0) {
      throw new \InvalidArgumentException("Invalid width ('{$arguments['width']}') specified for the image 'create_new' operation");
    }
    if ($arguments['height'] <= 0) {
      throw new \InvalidArgumentException("Invalid height ({$arguments['height']}) specified for the image 'create_new' operation");
    }

    // Assure transparent color is a valid hex string.     if ($arguments['transparent_color'] && !Color::validateHex($arguments['transparent_color'])) {
      throw new \InvalidArgumentException("Invalid transparent color ({$arguments['transparent_color']}) specified for the image 'create_new' operation");
    }

    return $arguments;
  }

  /** * {@inheritdoc} */
  protected function execute(array $arguments) {
    // Get the image type.

  public static function hexToRgb($hex) {
    if (!self::validateHex($hex)) {
      throw new \InvalidArgumentException("'$hex' is not a valid hex value.");
    }

    // Ignore '#' prefixes.     $hex = ltrim($hex, '#');

    // Convert shorthands like '#abc' to '#aabbcc'.     if (strlen($hex) == 3) {
      $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
    }

    
/** * @covers \Drupal\Component\Utility\Color::validateHex * * @param bool $expected * The expected result of validation. * @param string $value * The hex color value. * * @dataProvider providerTestValidateHex */
  public function testValidateHex($expected$value) {
    $this->assertSame($expected, Color::validateHex($value));
  }

  /** * Provides data for testValidateHex(). */
  public function providerTestValidateHex() {
    return [
      // Tests length.       [FALSE, ''],
      [FALSE, '#'],
      [FALSE, '1'],
      [
Home | Imprint | This part of the site doesn't use cookies.