Exemple #1
0
 /**
  * Determine best match to over/underlay a defined color.
  *
  * Calculates UCCIR 601 luma of the entered color and returns a black or
  * white color to ensure readibility.
  *
  * @see http://en.wikipedia.org/wiki/Luma_video
  */
 public static function matchLuma($rgba, $soft = FALSE)
 {
     $rgb = Unicode::substr($rgba, 0, 7);
     list($r, $g, $b) = array_values(Color::hexToRgb($rgb));
     $luma = 1 - (0.299 * $r + 0.587 * $g + 0.114 * $b) / 255;
     if ($luma < 0.5) {
         // Bright colors - black.
         $d = 0;
     } else {
         // Dark colors - white.
         $d = 255;
     }
     return Color::rgbToHex(array($d, $d, $d));
 }
Exemple #2
0
 /**
  * Form element validation handler for #type 'color'.
  */
 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($element, t('%name must be a valid color.', array('%name' => empty($element['#title']) ? $element['#parents'][0] : $element['#title'])));
         }
     }
 }
Exemple #3
0
 /**
  * Gets the color set for transparency in GIF images.
  *
  * @return string|null
  *   A color string like '#rrggbb', or NULL if not set or not relevant.
  */
 public function getTransparentColor()
 {
     if (!$this->getResource() || $this->getType() != IMAGETYPE_GIF) {
         return NULL;
     }
     // Find out if a transparent color is set, will return -1 if no
     // transparent color has been defined in the image.
     $transparent = imagecolortransparent($this->getResource());
     if ($transparent >= 0) {
         // Find out the number of colors in the image palette. It will be 0 for
         // 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;
 }
Exemple #4
0
 /**
  * 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));
 }