Last active
August 29, 2015 14:16
-
-
Save ryanmr/a9f3615159af818b49ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'vendor/autoload.php'; | |
/* | |
Using https://github.com/thephpleague/color-extractor | |
for library | |
http://stackoverflow.com/questions/1855884/determine-font-color-based-on-background-color | |
for brightness text | |
*/ | |
use League\ColorExtractor\Client as ColorExtractor; | |
function show_album($path) { | |
echo "<div style=\"padding: 1em; margin: 1em; display: inline-block;\"><img src=\"{$path}\" style='width: 200px;' /></div>"; | |
} | |
function show_color($color) { | |
if ( is_array($color) ) { | |
foreach ($color as $value) { | |
show_color($value); | |
} | |
} else { | |
$text = find_text_color($color) ? '#000' : '#fff'; | |
echo "<div style=\"color: {$text}; background-color: {$color}; width: 100px; height: 100px; padding: 1em; margin: 1em; display: inline-block;\">{$color}</div>"; | |
} | |
} | |
function find_text_color($color) { | |
$c = substr($color, 1); | |
$r = (substr($c, 0, 2)); | |
$g = (substr($c, 2, 2)); | |
$b = (substr($c, 4, 2)); | |
$z = array($r, $g, $b); | |
$vs = array_map(function($x){return hexdec($x);}, $z); | |
$a = 1 - (0.299 * $vs[0] + 0.587 * $vs[1] + 0.114 * $vs[2]) / 255; | |
return ($a < 0.5); | |
} | |
function extract_album_color($path) { | |
$client = new ColorExtractor; | |
$image = $client->loadJpeg($path); | |
$palette = $image->extract(3); | |
show_album($path); | |
show_color($palette); | |
echo "<br style='clear:both;' /><hr />"; | |
} | |
/* See visual example here: http://adept.work/j http://adept.work/m */ | |
$albums = ['atn.jpg', 'cs.jpg', 'eb.jpg']; | |
foreach ($albums as $album) { | |
extract_album_color($album); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment