Skip to content

Instantly share code, notes, and snippets.

@ryanmr
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanmr/a9f3615159af818b49ff to your computer and use it in GitHub Desktop.
Save ryanmr/a9f3615159af818b49ff to your computer and use it in GitHub Desktop.
<?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