Skip to content

Commit 5b205ad

Browse files
mreidenksubileau
authored andcommitted
Inline isClearlyVisible and isNonWhite functions to improve loading performance
1 parent 6f80087 commit 5b205ad

File tree

1 file changed

+14
-29
lines changed

1 file changed

+14
-29
lines changed

lib/ColorThief/ColorThief.php

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,21 @@ private static function loadImage($sourceImage, $quality, array &$histo, array $
195195
$y = (int) ($startY + $i / $width);
196196
$color = $image->getPixelColor($x, $y);
197197

198-
if (static::isClearlyVisible($color) && static::isNonWhite($color)) {
199-
// Count this pixel in its histogram bucket.
200-
$numUsefulPixels++;
201-
$bucketIndex = static::getColorIndex($color->red, $color->green, $color->blue);
202-
$histoSpl[$bucketIndex] = $histoSpl[$bucketIndex] + 1;
198+
// Pixel is too transparent. Its alpha value is larger (more transparent) than THRESHOLD_ALPHA.
199+
// PHP's transparency range (0-127 opaque-transparent) is reverse that of Javascript (0-255 tranparent-opaque).
200+
if ($color->alpha > self::THRESHOLD_ALPHA) {
201+
continue;
203202
}
203+
204+
// Pixel is too white to be useful. Its RGB values all exceed THRESHOLD_WHITE
205+
if ($color->red > self::THRESHOLD_WHITE && $color->green > self::THRESHOLD_WHITE && $color->blue > self::THRESHOLD_WHITE) {
206+
continue;
207+
}
208+
209+
// Count this pixel in its histogram bucket.
210+
$numUsefulPixels++;
211+
$bucketIndex = static::getColorIndex($color->red, $color->green, $color->blue);
212+
$histoSpl[$bucketIndex] = $histoSpl[$bucketIndex] + 1;
204213
}
205214

206215
// Copy the histogram buckets that had pixels back to a normal array.
@@ -221,30 +230,6 @@ private static function loadImage($sourceImage, $quality, array &$histo, array $
221230
return $numUsefulPixels;
222231
}
223232

224-
/**
225-
* @param object $color
226-
*
227-
* @return bool
228-
*/
229-
protected static function isClearlyVisible($color)
230-
{
231-
return $color->alpha <= self::THRESHOLD_ALPHA;
232-
}
233-
234-
/**
235-
* @param object $color
236-
*
237-
* @return bool
238-
*/
239-
protected static function isNonWhite($color)
240-
{
241-
return !(
242-
$color->red > self::THRESHOLD_WHITE &&
243-
$color->green > self::THRESHOLD_WHITE &&
244-
$color->blue > self::THRESHOLD_WHITE
245-
);
246-
}
247-
248233
/**
249234
* @param array $histo
250235
*

0 commit comments

Comments
 (0)