From 5df7ea8371f2822fe19bb0abd5c5b1c2316be88b Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Sun, 29 Sep 2019 14:16:50 +0200 Subject: [PATCH] Remove unused 'best color' algorithm. This code is used to efficiently determine the most saturated colour in an image, Kabuki uses this as the highlight colour on a page, but this styling has always been disabled in HashRU Pics. Hence, we can safely remove the code, freeing up some CPU cycles in the process. ;-) --- models/BestColor.php | 149 -------------------------------------- models/Image.php | 30 -------- templates/AlbumIndex.php | 6 +- templates/PhotosIndex.php | 11 +-- 4 files changed, 2 insertions(+), 194 deletions(-) delete mode 100644 models/BestColor.php diff --git a/models/BestColor.php b/models/BestColor.php deleted file mode 100644 index ff307837..00000000 --- a/models/BestColor.php +++ /dev/null @@ -1,149 +0,0 @@ -best = ['r' => 204, 'g' => 204, 'b' => 204]; // #cccccc - - // We will be needing to read this... - if (!file_exists($asset->getPath())) - return; - - // Try the arcane stuff again. - try - { - $image = new Imagick($asset->getPath()); - $width = $image->getImageWidth(); - $height = $image->getImageHeight(); - - // Sample six points in the image: four based on the rule of thirds, as well as the horizontal and vertical centre. - $topy = round($height / 3); - $bottomy = round(($height / 3) * 2); - $leftx = round($width / 3); - $rightx = round(($width / 3) * 2); - $centery = round($height / 2); - $centerx = round($width / 2); - - // Grab their colours. - $rgb = [ - $image->getImagePixelColor($leftx, $topy)->getColor(), - $image->getImagePixelColor($rightx, $topy)->getColor(), - $image->getImagePixelColor($leftx, $bottomy)->getColor(), - $image->getImagePixelColor($rightx, $bottomy)->getColor(), - $image->getImagePixelColor($centerx, $centery)->getColor(), - ]; - - // We won't be needing this anymore, so save us some memory. - $image->clear(); - $image->destroy(); - } - // In case something does go wrong... - catch (ImagickException $e) - { - // Fall back to default color. - return; - } - - // Process rgb values into hsv values - foreach ($rgb as $i => $color) - { - $colors[$i] = $color; - list($colors[$i]['h'], $colors[$i]['s'], $colors[$i]['v']) = self::rgb2hsv($color['r'], $color['g'], $color['b']); - } - - // Figure out which color is the best saturated. - $best_saturation = $best_brightness = 0; - $the_best_s = $the_best_v = ['v' => 0]; - foreach ($colors as $color) - { - if ($color['s'] > $best_saturation) - { - $best_saturation = $color['s']; - $the_best_s = $color; - } - if ($color['v'] > $best_brightness) - { - $best_brightness = $color['v']; - $the_best_v = $color; - } - } - - // Is brightest the same as most saturated? - $this->best = ($the_best_s['v'] >= ($the_best_v['v'] - ($the_best_v['v'] / 2))) ? $the_best_s : $the_best_v; - } - - public static function hex2rgb($hex) - { - return sscanf($hex, '%2X%2X%2X'); - } - - public static function rgb2hex($red, $green, $blue) - { - return sprintf('%02X%02X%02X', $red, $green, $blue); - } - - public static function rgb2hsv($r, $g, $b) - { - $max = max($r, $g, $b); - $min = min($r, $g, $b); - $delta = $max - $min; - $v = round(($max / 255) * 100); - $s = ($max != 0) ? (round($delta / $max * 100)) : 0; - if ($s == 0) - { - $h = false; - } - else - { - if ($r == $max) - $h = ($g - $b) / $delta; - elseif ($g == $max) - $h = 2 + ($b - $r) / $delta; - elseif ($b == $max) - $h = 4 + ($r - $g) / $delta; - - $h = round($h * 60); - - if ($h > 360) - $h = 360; - - if ($h < 0) - $h += 360; - } - - return [$h, $s, $v]; - } - - /** - * Get a normal (light) background color as hexadecimal value (without hash prefix). - * @return color string - */ - public function hex() - { - $c = $this->best; - return self::rgb2hex($c['r'], $c['g'], $c['b']); - } - - /** - * Get a 50% darker version of the best color as string. - * @param factor, defaults to 0.5 - * @param alpha, defaults to 0.7 - * @return rgba(r * factor, g * factor, b * factor, alpha) - */ - public function rgba($factor = 0.5, $alpha = 0.7) - { - $c = $this->best; - return 'rgba(' . round($c['r'] * $factor) . ', ' . round($c['g'] * $factor) . ', ' . round($c['b'] * $factor) . ', ' . $alpha . ')'; - } - -} diff --git a/models/Image.php b/models/Image.php index 981f542b..dae64229 100644 --- a/models/Image.php +++ b/models/Image.php @@ -90,36 +90,6 @@ class Image extends Asset return $thumbnail->getUrl($width, $height, $crop, $fit, $generate); } - public function bestColor() - { - // Save some computations if we can. - if (isset($this->meta['best_color'])) - return $this->meta['best_color']; - - // Find out what colour is most prominent. - $color = new BestColor($this); - $this->meta['best_color'] = $color->hex(); - $this->save(); - - // There's your colour. - return $this->meta['best_color']; - } - - public function bestLabelColor() - { - // Save some computations if we can. - if (isset($this->meta['best_color_label'])) - return $this->meta['best_color_label']; - - // Find out what colour is most prominent. - $color = new BestColor($this); - $this->meta['best_color_label'] = $color->rgba(); - $this->save(); - - // There's your colour. - return $this->meta['best_color_label']; - } - public function getId() { return $this->id_asset; diff --git a/templates/AlbumIndex.php b/templates/AlbumIndex.php index 37d00c10..c3118845 100644 --- a/templates/AlbumIndex.php +++ b/templates/AlbumIndex.php @@ -35,12 +35,8 @@ class AlbumIndex extends SubTemplate foreach ($photos as $album) { - $color = isset($album['thumbnail']) ? $album['thumbnail']->bestColor() : 'ccc'; - if ($color == 'FFFFFF') - $color = 'ccc'; - echo ' -
'; +
'; if ($this->show_edit_buttons) echo ' diff --git a/templates/PhotosIndex.php b/templates/PhotosIndex.php index e6bf818c..2d911294 100644 --- a/templates/PhotosIndex.php +++ b/templates/PhotosIndex.php @@ -80,19 +80,10 @@ class PhotosIndex extends SubTemplate $this->previous_header = $header; } - protected function color(Image $image) - { - $color = $image->bestColor(); - if ($color == 'FFFFFF') - $color = 'ccc'; - - return $color; - } - protected function photo(Image $image, $className, $width, $height, $crop = true, $fit = true) { echo ' -
'; +
'; if ($this->show_edit_buttons) echo '