<?php
//connect to database
$link = mysql_connect(
':/Applications/MAMP/tmp/mysql/mysql.sock',
'root',
'root'
);
mysql_select_db('colors');
class ntc {
/**
* [URL unfurl="true"]http://www.emanueleferonato.com/2009/09/08/color-difference-algorithm-part-2/[/URL]
* @param object $color
* @return
*/
private function rgb_to_xyz($color) {
$rgb = $this->rgb($color);
$red = $rgb[0];
$green = $rgb[1];
$blue = $rgb[2];
$_red = $red / 255;
$_green = $green / 255;
$_blue = $blue / 255;
if ($_red > 0.04045) {
$_red = ($_red + 0.055) / 1.055;
$_red = pow($_red, 2.4);
} else {
$_red = $_red / 12.92;
}
if ($_green > 0.04045) {
$_green = ($_green + 0.055) / 1.055;
$_green = pow($_green, 2.4);
} else {
$_green = $_green / 12.92;
}
if ($_blue > 0.04045) {
$_blue = ($_blue + 0.055) / 1.055;
$_blue = pow($_blue, 2.4);
} else {
$_blue = $_blue / 12.92;
}
$_red *= 100;
$_green *= 100;
$_blue *= 100;
$x = $_red * 0.4124 + $_green * 0.3576 + $_blue * 0.1805;
$y = $_red * 0.2126 + $_green * 0.7152 + $_blue * 0.0722;
$z = $_red * 0.0193 + $_green * 0.1192 + $_blue * 0.9505;
return (array($x, $y, $z));
}
/**
* [URL unfurl="true"]http://www.emanueleferonato.com/2009/09/08/color-difference-algorithm-part-2/[/URL]
* @param object $xyz
* @return
*/
private function xyz_to_lab($xyz) {
$x = $xyz[0];
$y = $xyz[1];
$z = $xyz[2];
$_x = $x / 95.047;
$_y = $y / 100;
$_z = $z / 108.883;
if ($_x > 0.008856) {
$_x = pow($_x, 1 / 3);
} else {
$_x = 7.787 * $_x + 16 / 116;
}
if ($_y > 0.008856) {
$_y = pow($_y, 1 / 3);
} else {
$_y = (7.787 * $_y) + (16 / 116);
}
if ($_z > 0.008856) {
$_z = pow($_z, 1 / 3);
} else {
$_z = 7.787 * $_z + 16 / 116;
}
$l = 116 * $_y - 16;
$a = 500 * ($_x - $_y);
$b = 200 * ($_y - $_z);
return (array($l, $a, $b));
}
public function color2lab($color) {
return $this->xyz_to_lab($this->rgb_to_xyz($color));
}
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 = 1; $i <= 3; $i++):
$_color .= $color[$i];
$_color .= $color[$i];
endfor;
$color = $_color;
endif;
$result = mysql_query("
Select *, (select c2.hex from colors c2 where c2.name = c1.shade) as shadeHex
FROM colors c1
WHERE c1.hex = '" .mysql_real_escape_string(substr($color, 1))."'");
$row = mysql_fetch_object($result);
if ($row) {
return $row;
}
$cia = $this->color2lab($color);
list($cial, $ciaa, $ciab) = $cia;
$sql = <<<SQL
select c1.*, (select c2.hex from colors c2 where c2.name = c1.shade) as shadeHex,
sqrt(
(
pow((cial - $cial),2) /* first */
)
+
(
pow(
(
sqrt(pow(ciaa,2) + pow(ciab,2)) /* c1 */
-
sqrt(pow($ciaa,2) + pow($ciab,2)) /*c2 */
)
/ (1 + (0.045 * sqrt(pow(ciaa,2) + pow(ciab,2)))) /* c1 */
,2) /* second */
)
+
(
pow(sqrt(
pow((ciaa - $ciaa),2) /*da*/
+
pow((ciab - $ciab),2) /*db*/
-
pow(
sqrt(pow(ciaa,2) + pow(ciab,2))
-
sqrt(pow($ciaa,2) + pow($ciab,2))
,2)
) /*dh*/
/(1 + ( 0.015 * sqrt(pow(ciaa,2) + pow(ciab,2))))
,2)
)
) /* end sqrt */ AS diff
from colors c1
where
(
sqrt(
(
pow((cial - $cial),2) /* first */
)
+
(
pow(
(
sqrt(pow(ciaa,2) + pow(ciab,2)) /* c1 */
-
sqrt(pow($ciaa,2) + pow($ciab,2)) /*c2 */
)
/ (1 + (0.045 * sqrt(pow(ciaa,2) + pow(ciab,2)))) /* c1 */
,2) /* second */
)
+
(
pow(sqrt(
pow((ciaa - $ciaa),2) /*da*/
+
pow((ciab - $ciab),2) /*db*/
-
pow(
sqrt(pow(ciaa,2) + pow(ciab,2))
-
sqrt(pow($ciaa,2) + pow($ciab,2))
,2)
) /*dh*/
/(1 + ( 0.015 * sqrt(pow(ciaa,2) + pow(ciab,2))))
,2)
)
) /* end sqrt */
) is not null
order by
sqrt(
(
pow((cial - $cial),2) /* first */
)
+
(
pow(
(
sqrt(pow(ciaa,2) + pow(ciab,2)) /* c1 */
-
sqrt(pow($ciaa,2) + pow($ciab,2)) /*c2 */
)
/ (1 + (0.045 * sqrt(pow(ciaa,2) + pow(ciab,2)))) /* c1 */
,2) /* second */
)
+
(
pow(sqrt(
pow((ciaa - $ciaa),2) /*da*/
+
pow((ciab - $ciab),2) /*db*/
-
pow(
sqrt(pow(ciaa,2) + pow(ciab,2))
-
sqrt(pow($ciaa,2) + pow($ciab,2))
,2)
) /*dh*/
/(1 + ( 0.015 * sqrt(pow(ciaa,2) + pow(ciab,2))))
,2)
)
) /* end sqrt */ ASC
LIMIT 1
SQL;
$result = mysql_query($sql) or die($sql);
$row = mysql_fetch_assoc($result);
return $row;
}
private function rgb($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;
$r = hexdec($r);
$g = hexdec($g);
$b = hexdec($b);
return array($r, $g, $b);
}
}
$ntc = new ntc;
$result = $ntc->name('#CCCC99');
print_r($result);
?>