Skip to content

Commit 6f80087

Browse files
mreidenksubileau
authored andcommitted
Inline rearrangeColors function to improve performance of sumColors
1 parent 27cad44 commit 6f80087

File tree

1 file changed

+12
-30
lines changed

1 file changed

+12
-30
lines changed

lib/ColorThief/ColorThief.php

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -371,15 +371,20 @@ private static function sumColors($axis, $histo, $vBox)
371371
$sum = 0;
372372
foreach ($secondRange as $secondColor) {
373373
foreach ($thirdRange as $thirdColor) {
374-
list($redBucket, $greenBucket, $blueBucket) = static::rearrangeColors(
375-
$colorIterateOrder,
376-
$firstColor,
377-
$secondColor,
378-
$thirdColor
379-
);
374+
// Rearrange color components
375+
$bucket = [
376+
$colorIterateOrder[0] => $firstColor,
377+
$colorIterateOrder[1] => $secondColor,
378+
$colorIterateOrder[2] => $thirdColor,
379+
];
380380

381381
// The getColorIndex function takes RGB values instead of buckets. The left shift converts our bucket into its RGB value.
382-
$bucketIndex = static::getColorIndex($redBucket << self::RSHIFT, $greenBucket << self::RSHIFT, $blueBucket << self::RSHIFT, self::SIGBITS);
382+
$bucketIndex = static::getColorIndex(
383+
$bucket['r'] << self::RSHIFT,
384+
$bucket['g'] << self::RSHIFT,
385+
$bucket['b'] << self::RSHIFT,
386+
self::SIGBITS
387+
);
383388

384389
if (isset($histo[$bucketIndex])) {
385390
$sum += $histo[$bucketIndex];
@@ -393,29 +398,6 @@ private static function sumColors($axis, $histo, $vBox)
393398
return [$total, $partialSum];
394399
}
395400

396-
/**
397-
* @param array $order
398-
* @param int $color1
399-
* @param int $color2
400-
* @param int $color3
401-
*
402-
* @return array
403-
*/
404-
private static function rearrangeColors(array $order, $color1, $color2, $color3)
405-
{
406-
$data = [
407-
$order[0] => $color1,
408-
$order[1] => $color2,
409-
$order[2] => $color3,
410-
];
411-
412-
return [
413-
$data['r'],
414-
$data['g'],
415-
$data['b'],
416-
];
417-
}
418-
419401
/**
420402
* @param VBox $vBox
421403
* @param array $order

0 commit comments

Comments
 (0)