<?php
class ntc{
public $color;
public $rgb;
public $hsl;
public $colorFile = '[URL unfurl="true"]http://dl.dropbox.com/u/194358/colorSerialized.php';[/URL]
public function __construct(){
$this->names = unserialize(file_get_contents($this->colorFile));
$this->shades = unserialize('a:10:{i:0;a:2:{i:0;s:6:"FF0000";i:1;s:3:"Red";}i:1;a:2:{i:0;s:6:"FFA500";i:1;s:6:"Orange";}i:2;a:2:{i:0;s:6:"FFFF00";i:1;s:6:"Yellow";}i:3;a:2:{i:0;s:6:"008000";i:1;s:5:"Green";}i:4;a:2:{i:0;s:6:"0000FF";i:1;s:4:"Blue";}i:5;a:2:{i:0;s:6:"EE82EE";i:1;s:6:"Violet";}i:6;a:2:{i:0;s:6:"A52A2A";i:1;s:5:"Brown";}i:7;a:2:{i:0;s:6:"000000";i:1;s:5:"Black";}i:8;a:2:{i:0;s:6:"808080";i:1;s:4:"Grey";}i:9;a:2:{i:0;s:6:"FFFFFF";i:1;s:5:"White";}}');
for($i = 0; $i < count($this->names) ; $i++):
$color = "#" + $this->names[$i][0];
$rgb = $this->html2rgb($color);
$hsl = $this->rgb2hsl($rgb);
array_push($this->names[$i], $rgb[0], $rgb[1], $rgb[2], $hsl[0], $hsl[1], $hsl[2]);
endfor;
}
public function name($color){
$color = strtoupper($color);
if(strlen($color) < 3 || strlen($color) > 7):
return array("#000000", "Invalid Color: " . $color, "#000000", "", false);
endif;
if(strlen($color) % 3 == 0):
$color = "#" + $color;
endif;
if(strlen($color) == 4):
$_color = '#';
for($i=0; $i<3;$i++):
$_color .= $color[$i];
$_color .= $color[$i];
endfor;
$color = $_color;
endif;
$rgb = $this->html2rgb($color);
list($r, $g, $b) = $rgb;
$hsl = $this->rgb2hsl($rgb);
list($h,$s,$l) = $hsl;
$ndf1 = 0; $ndf2 = 0; $ndf = 0;
$cl = -1;
$df = -1;
for($i = 0; $i < count($this->names) ; $i++):
if($color == "#" + $this->names[$i][0]):
return array("#" + $this->names[$i][0],
$this->names[$i][1],
$this->shadergb($this->names[$i][2]), $this->names[$i][2],
true);
endif;
//print_R($this->names[$i]);
$ndf1 = pow($r - $this->names[$i][3], 2)
+ pow($g - $this->names[$i][4], 2)
+ pow($b - $this->names[$i][5], 2);
$ndf2 = abs(pow($h - $this->names[$i][6], 2))
+ pow($s - $this->names[$i][7], 2)
+ abs(pow($l - $this->names[$i][8], 2));
$ndf = $ndf1 + $ndf2 * 2;
if($df < 0 || $df > $ndf):
$df = $ndf;
$cl = $i;
endif;
endfor;
return $cl < 0
? array("#000000", "Invalid Color: " + $color, "#000000", "", false)
: array("#" + $this->names[$cl][0], $this->names[$cl][1], $this->shadergb($this->names[$cl][2]), $this->names[$cl][2], false);
}
private function html2rgb($color){
if ($color[0] == '#')
$color = substr($color, 1);
if (strlen($color) == 6):
list($r, $g, $b) = array($color[0].$color[1],
$color[2].$color[3],
$color[4].$color[5]);
elseif (strlen($color) == 3):
list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
else:
return false;
endif;
$r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
return array($r, $g, $b);
}
private function rgb2hsl($rgb){
list($clrR,$clrG,$clrB) = $rgb;
$clrMin = min($clrR, $clrG, $clrB);
$clrMax = max($clrR, $clrG, $clrB);
$deltaMax = $clrMax - $clrMin;
$L = ($clrMax + $clrMin) / 510;
if (0 == $deltaMax){
$H = 0;
$S = 0;
}
else{
if (0.5 > $L){
$S = $deltaMax / ($clrMax + $clrMin);
}
else{
$S = $deltaMax / (510 - $clrMax - $clrMin);
}
if ($clrMax == $clrR) {
$H = ($clrG - $clrB) / (6.0 * $deltaMax);
}
else if ($clrMax == $clrG) {
$H = 1/3 + ($clrB - $clrR) / (6.0 * $deltaMax);
}
else {
$H = 2 / 3 + ($clrR - $clrG) / (6.0 * $deltaMax);
}
if (0 > $H) $H += 1;
if (1 < $H) $H -= 1;
}
return array($H, $S,$L);
}
private function shadergb($shade){
foreach($this->shades as $_shade):
if($shade == $_shade[1])
return "#" + $_shade[0];
endforeach;
return "#000000";
}
}
$ntc = new ntc;
$result = $ntc->name('#6195ED');
print_r($result);
?>