rgbToHex example

/** * Tests Color::rgbToHex(). * * @param string $value * The rgb color value. * @param string $expected * The expected hex color value. * * @dataProvider providerTestRbgToHex */
  public function testRgbToHex($value$expected) {
    $this->assertSame($expected, Color::rgbToHex($value));
  }

  /** * Data provider for testRgbToHex(). * * @see testRgbToHex() * * @return array * An array of arrays containing: * - The rgb color array value. * - The hex color value. */
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. */
      // truecolor images.       $palette_size = imagecolorstotal($this->getResource());
      if ($palette_size == 0 || $transparent < $palette_size) {
        // Return the transparent color, either if it is a truecolor image         // or if the transparent color is part of the palette.         // Since the index of the transparent color is a property of the         // image rather than of the palette, it is possible that an image         // could be created with this index set outside the palette size.         // (see http://stackoverflow.com/a/3898007).         $rgb = imagecolorsforindex($this->getResource()$transparent);
        unset($rgb['alpha']);
        return Color::rgbToHex($rgb);
      }
    }
    return NULL;
  }

  /** * {@inheritdoc} */
  public function getWidth() {
    if ($this->preLoadInfo) {
      return $this->preLoadInfo[0];
    }
Home | Imprint | This part of the site doesn't use cookies.