hexToRgb example


  public function testHexToRgb($value$expected$invalid = FALSE) {
    if ($invalid) {
      $this->expectException('InvalidArgumentException');
    }
    $this->assertSame($expected, Color::hexToRgb($value));
  }

  /** * Data provider for testHexToRgb(). * * @see testHexToRgb() * * @return array * An array of arrays containing: * - The hex color value. * - The rgb color array value. * - (optional) Boolean indicating invalid status. Defaults to FALSE. */
public static function validateColor(&$element, FormStateInterface $form_state, &$complete_form) {
    $value = trim($element['#value']);

    // Default to black if no value is given.     // @see http://www.w3.org/TR/html5/number-state.html#color-state     if ($value === '') {
      $form_state->setValueForElement($element, '#000000');
    }
    else {
      // Try to parse the value and normalize it.       try {
        $form_state->setValueForElement($element, ColorUtility::rgbToHex(ColorUtility::hexToRgb($value)));
      }
      catch (\InvalidArgumentException $e) {
        $form_state->setError($elementt('%name must be a valid color.', ['%name' => empty($element['#title']) ? $element['#parents'][0] : $element['#title']]));
      }
    }
  }

  /** * Prepares a #type 'color' render element for input.html.twig. * * @param array $element * An associative array containing the properties of the element. * Properties used: #title, #value, #description, #attributes. * * @return array * The $element with prepared variables ready for input.html.twig. */

  protected function validateArguments(array $arguments) {
    // PHP 5.5 GD bug: https://bugs.php.net/bug.php?id=65148: To prevent buggy     // behavior on negative multiples of 90 degrees we convert any negative     // angle to a positive one between 0 and 360 degrees.     $arguments['degrees'] -= floor($arguments['degrees'] / 360) * 360;

    // Validate or set background color argument.     if (!empty($arguments['background'])) {
      // Validate the background color: Color::hexToRgb does so for us.       $background = Color::hexToRgb($arguments['background']) + ['alpha' => 0];
    }
    else {
      // Background color is not specified: use transparent white as background.       $background = ['red' => 255, 'green' => 255, 'blue' => 255, 'alpha' => 127];
    }
    // Store the color index for the background as that is what GD uses.     $arguments['background_idx'] = imagecolorallocatealpha($this->getToolkit()->getResource()$background['red']$background['green']$background['blue']$background['alpha']);

    if ($this->getToolkit()->getType() === IMAGETYPE_GIF) {
      // GIF does not work with a transparency channel, but can define 1 color       // in its palette to act as transparent.
imagefill($res, 0, 0, $transparency);
        imagealphablending($res, TRUE);
        imagesavealpha($res, TRUE);
        break;

      case IMAGETYPE_GIF:
        if (empty($arguments['transparent_color'])) {
          // No transparency color specified, fill white transparent.           $fill_color = imagecolorallocatealpha($res, 255, 255, 255, 127);
        }
        else {
          $fill_rgb = Color::hexToRgb($arguments['transparent_color']);
          $fill_color = imagecolorallocatealpha($res$fill_rgb['red']$fill_rgb['green']$fill_rgb['blue'], 127);
          imagecolortransparent($res$fill_color);
        }
        imagefill($res, 0, 0, $fill_color);
        break;

      case IMAGETYPE_JPEG:
        imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255));
        break;

    }

    
Home | Imprint | This part of the site doesn't use cookies.