Last active
August 29, 2015 14:15
-
-
Save ffoodd/02d1278f9ad2909f0c9c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
$path = '/home/www/7studio/www/assets/stylesheets/'; | |
$cmd = 'find '.$path.' -iname "*.css"'; | |
$out = array(); | |
exec($cmd, $out); | |
echo '<strong>Fichiers CSS explorés</strong>'; | |
echo '<ul>'; | |
foreach($out as $V){ | |
echo '<li>'.$V.'</li>'; | |
} | |
echo '</ul>'; | |
echo '<hr />'; | |
$out = array_map('file_get_contents', $out); | |
$out = array_map('CSSCleaner', $out); | |
$out = implode("", $out); | |
$formats = array( | |
'/(rgb|hsl)a?\((\s*[a-z0-9\.]{1,3}%?\s*,?){3,4}\)/i', | |
'/#[0-9A-F]{3,6}/i' | |
); | |
$colors = array(); | |
foreach ($formats as $format) { | |
if( preg_match_all($format, $out, $matches) ){ | |
$colors = array_merge($colors, $matches[0]); | |
} | |
} | |
if( !empty($colors) ){ | |
$_colors = array(); | |
foreach ($colors as $V) { | |
$V = preg_replace('/\s*/i', '', $V); | |
$C = new Color($V); | |
$_colors[$C->hex][0] = $C; | |
if (!isset($_colors[$C->hex][1])) { | |
$_colors[$C->hex][1] = array(); | |
} | |
$_colors[$C->hex][1][] = $V; | |
} | |
uasort($_colors, function($a,$b){ | |
if ($a[0]->hue == $b[0]->hue) { | |
return ($a[0]->lightness > $b[0]->lightness) ? -1 : 1; | |
} | |
return ($a[0]->hue < $b[0]->hue) ? -1 : 1; | |
}); | |
echo '<strong>Couleurs uniques utilisées</strong>'; | |
echo '<ol>'; | |
foreach($_colors as $K=>$color){ | |
echo '<li><span style="display:inline-block;width:5em;height:1em;background-color:#'.$color[0]->hex.'"></span> <small>(x'.count($color[1]).')</small> : <code>'.implode('</code>, <code>', array_unique($color[1])).'</code></li>'; | |
} | |
echo '</ol>'; | |
} | |
class Color { | |
//hex | |
public $hex; | |
// rgb | |
public $red = 0; | |
public $green = 0; | |
public $blue = 0; | |
// hsl | |
public $hue = 0; | |
public $saturation = 0; | |
public $lightness = 0; | |
// alpha | |
public $alpha = 1; | |
function __construct ($value) { | |
$value = preg_replace('/\s/u', '', $value); | |
// ex. : rgb(80, 0, 0), rgba(0, 0, 0, .5) | |
if (strpos($value, 'rgb') !== false) { | |
preg_match_all('/[0-9\.]+/u', $value, $matches); | |
$components = $matches[0]; | |
$this->red = $components[0]; | |
$this->green = $components[1]; | |
$this->blue = $components[2]; | |
if (isset($components[3]) && $components[3] != 1) { | |
$this->alpha = $components[3]; | |
} | |
$this->setHslFromRgb(); | |
$this->setHex(); | |
} | |
// ex. : hsl(0, 100%, 16%), hsla(0, 100%, 0%, .5) | |
elseif (strpos($value, 'hsl') !== false) { | |
preg_match_all('/[0-9\.]+/u', $value, $matches); | |
$components = $matches[0]; | |
$this->hue = $components[0]; | |
$this->lightness = $components[1]; | |
$this->saturation = $components[2]; | |
$rgb = $this->hslToRgb($this->hue, $this->lightness, $this->saturation); | |
$this->red = $rgb['r']; | |
$this->green = $rgb['g']; | |
$this->blue = $rgb['b']; | |
if (isset($components[3]) && $components[3] != 1) { | |
$this->alpha = $components[3]; | |
} | |
$this->setHex(); | |
} | |
// ex. : #00ff00", #fb0 | |
else { | |
$value = str_replace('#', '', $value); | |
$d = (strlen($value) / 3); | |
preg_match_all('/[0-9a-fA-F]{' . $d . '}/u', $value, $matches); | |
$bits = $matches[0]; | |
if ($d == 1) { | |
array_walk($bits, function(&$item){ $item .= $item; }); | |
} | |
$this->red = hexdec($bits[0]); | |
$this->green = hexdec($bits[1]); | |
$this->blue = hexdec($bits[2]); | |
$this->setHslFromRgb(); | |
$this->setHex(); | |
} | |
} | |
public function setHex () { | |
foreach (array($this->red, $this->green, $this->blue) as $color) { | |
$color = dechex($color); | |
$this->hex .= (strlen($color) < 2 ? '0' : '') . $color; | |
} | |
} | |
public function setHslFromRgb () { | |
$hsl = $this->rgbToHsl($this->red, $this->green, $this->blue); | |
$this->hue = $hsl['h']; | |
$this->saturation = $hsl['s']; | |
$this->lightness = $hsl['l']; | |
} | |
// https://github.com/indyarmy/color2color | |
public function rgbToHsl ($r, $g, $b) { | |
$rgb = array(); | |
$rgb['r'] = ($r % 256) / 256; | |
$rgb['g'] = ($g % 256) / 256; | |
$rgb['b'] = ($b % 256) / 256; | |
$max = max($rgb); | |
$min = min($rgb); | |
$hsl = array(); | |
$d; | |
$hsl['l'] = ($max + $min) / 2; | |
if ($max === $min) { | |
$hsl['h'] = 0; | |
$hsl['s'] = 0; | |
} | |
else { | |
$d = ($max - $min); | |
$hsl['s'] = $hsl['l'] > 0.5 ? $d / (2 - $max - $min) : $d / ($max + $min); | |
switch ($max) { | |
case $rgb['r']: | |
$hsl['h'] = ($rgb['g'] - $rgb['b']) / $d + ($rgb['g'] < $rgb['b'] ? 6 : 0); | |
break; | |
case $rgb['g']: | |
$hsl['h'] = ($rgb['b'] - $rgb['r']) / $d + 2; | |
break; | |
case $rgb['b']: | |
$hsl['h'] = ($rgb['r'] - $rgb['g']) / $d + 4; | |
break; | |
} | |
$hsl['h'] /= 6; | |
} | |
$hsl['h'] = round(($hsl['h'] * 360), 0); | |
$hsl['s'] = round(($hsl['s'] * 100), 0); | |
$hsl['l'] = round(($hsl['l'] * 100), 0); | |
return $hsl; | |
} | |
public function hslToRgb ($h, $s, $l) { | |
$rgb = array(); | |
$q = 0; | |
$p = 0; | |
$hsl = array( | |
'h' => ($h % 360) / 360, | |
's' => ($s % 101) / 100, | |
'l' => ($l % 101) / 100 | |
); | |
if ($hsl['s'] === 0) { | |
$v = round(255 * $hsl['l']); | |
$rgb = array('r'=>$v,'g'=>$v,'b'=>$v); | |
} | |
else { | |
$q = $hsl['l'] < 0.5 ? $hsl['l'] * (1 + $hsl['s']) : $hsl['l'] + $hsl['s'] - $hsl['l'] * $hsl['s']; | |
$p = 2 * $hsl['l'] - $q; | |
$rgb['r'] = round(($this->hueToRgb( $p, $q, ($hsl['h'] + 1 / 3) ) * 256), 0); | |
$rgb['g'] = round(($this->hueToRgb( $p, $q, $hsl['h'] ) * 256), 0); | |
$rgb['b'] = round(($this->hueToRgb( $p, $q, ($hsl['h'] - 1 / 3) ) * 256), 0); | |
} | |
return $rgb; | |
} | |
public function hueToRgb ($p, $q, $t) { | |
if ($t < 0) { | |
$t += 1; | |
} | |
if ($t > 1){ | |
$t -= 1; | |
} | |
if ($t < 1 / 6) { | |
return $p + ($q - $p) * 6 * $t; | |
} | |
if ($t < 1 / 2) { | |
return $q; | |
} | |
if ($t < 2 / 3) { | |
return $p + ($q - $p) * (2 / 3 - $t) * 6; | |
} | |
return $p; | |
} | |
} | |
function CSSCleaner($css){ | |
// suppression des commentaires | |
$css = preg_replace('/\/\*[^*]*\*+([^\/][^*]*\*+)*\//', '', $css); | |
// nettoyage des retours à la ligne | |
$css = str_replace(array("\r\n","\r"),"\n",$css); | |
// optimisation des espaces et monolignage des selecteurs | |
$css = preg_replace('/\s*([{:;,])\s*/', "$1", $css); | |
$css = preg_replace('/\s*([}])\s*/', "$1\n", $css); | |
// suppression des lignes vides | |
$css = implode("\n",array_filter(array_map('trim',explode("\n",$css)))); | |
return $css; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment