summaryrefslogtreecommitdiff
path: root/include/colors.php
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2013-04-15 17:01:51 +0400
committerAndrew Dolgov <[email protected]>2013-04-15 17:01:51 +0400
commit6ac722d5b1e336dcb8eebec04496150c2f116154 (patch)
treea4f927ce114939769839945a542ecc211c8c81fe /include/colors.php
parente7480cc13b55162510001b5b9d91af289606a9f1 (diff)
try to improve color guessing algorithm a bit
Diffstat (limited to 'include/colors.php')
-rw-r--r--include/colors.php60
1 files changed, 60 insertions, 0 deletions
diff --git a/include/colors.php b/include/colors.php
index d1e970728..19c891517 100644
--- a/include/colors.php
+++ b/include/colors.php
@@ -1,5 +1,7 @@
<?php
+require_once "lib/floIcon.php";
+
function _resolve_htmlcolor($color) {
$htmlcolors = array ("aliceblue" => "#f0f8ff",
"antiquewhite" => "#faebd7",
@@ -278,4 +280,62 @@ function hsl2rgb($arr) {
return array($r, $g, $B);
}
+ function colorPalette($imageFile, $numColors, $granularity = 5) {
+ $granularity = max(1, abs((int)$granularity));
+ $colors = array();
+
+ $size = @getimagesize($imageFile);
+
+ if (strtolower($size['mime']) == 'image/vnd.microsoft.icon') {
+ $ico = new floIcon();
+ @$ico->readICO($imageFile);
+
+ if(count($ico->images)==0)
+ return null;
+ else
+ $img = @$ico->images[count($ico->images)-1]->getImageResource();
+
+ } else {
+ $img = @imagecreatefromstring(file_get_contents($imageFile));
+ }
+
+ if (!$img) return false;
+
+ for($x = 0; $x < $size[0]; $x += $granularity) {
+ for($y = 0; $y < $size[1]; $y += $granularity) {
+ $thisColor = imagecolorat($img, $x, $y);
+ $rgb = imagecolorsforindex($img, $thisColor);
+ $red = round(round(($rgb['red'] / 0x33)) * 0x33);
+ $green = round(round(($rgb['green'] / 0x33)) * 0x33);
+ $blue = round(round(($rgb['blue'] / 0x33)) * 0x33);
+ $thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue);
+ if(array_key_exists($thisRGB, $colors)) {
+ $colors[$thisRGB]++;
+ } else{
+ $colors[$thisRGB] = 1;
+ }
+ }
+ }
+
+ arsort($colors);
+ return array_slice(array_keys($colors), 0, $numColors);
+ }
+
+ function calculate_avg_color($iconFile) {
+ $palette = colorPalette($iconFile, 4, 4);
+
+ if (is_array($palette)) {
+ foreach ($palette as $p) {
+ $hsl = rgb2hsl(_color_unpack("#$p"));
+
+ if ($hsl[1] > 0.25 && $hsl[2] > 0.25 &&
+ !($hsl[0] >= 0 && $hsl[0] < 0.01 && $hsl[1] < 0.01) &&
+ !($hsl[0] >= 0 && $hsl[0] < 0.01 && $hsl[2] > 0.99)) {
+
+ return _color_pack(hsl2rgb($hsl));
+ }
+ }
+ }
+ return false;
+ }
?>