You are here

public static function ColorUtility::matchLuma in Image Effects 8.2

Same name and namespace in other branches
  1. 8.3 src/Component/ColorUtility.php \Drupal\image_effects\Component\ColorUtility::matchLuma()
  2. 8 src/Component/ColorUtility.php \Drupal\image_effects\Component\ColorUtility::matchLuma()

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 also

http://en.wikipedia.org/wiki/Luma_video

3 calls to ColorUtility::matchLuma()
DrawRectangleTrait::validateArguments in src/Plugin/ImageToolkit/Operation/DrawRectangleTrait.php
image_effects_preprocess_image_effects_color_detail in ./image_effects.module
Prepares variables to get a color info.
TextToWrapper::drawDebugBox in src/Plugin/ImageToolkit/Operation/gd/TextToWrapper.php
Display a polygon enclosing the text line, and conspicuous points.

File

src/Component/ColorUtility.php, line 21

Class

ColorUtility
Color handling methods for image_effects.

Namespace

Drupal\image_effects\Component

Code

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([
    $d,
    $d,
    $d,
  ]);
}