<?php
include(dirname( __DIR__ ,1) . "/PDO_db_config.php");
function hexToRgb($hex) {
$hex = str_replace('#', '', $hex);
$length = strlen($hex);
$r = hexdec($length == 6 ? substr($hex, 0, 2) : ($length == 3 ? str_repeat(substr($hex, 0, 1), 2) : 0));
$g = hexdec($length == 6 ? substr($hex, 2, 2) : ($length == 3 ? str_repeat(substr($hex, 1, 1), 2) : 0));
$b = hexdec($length == 6 ? substr($hex, 4, 2) : ($length == 3 ? str_repeat(substr($hex, 2, 1), 2) : 0));
return $r.",".$g.",".$b;
}
function getcolorname($mycolor) {
$colors = array(
"black" =>array(0,0,0),
"green" =>array(0,255,0),
"blue" =>array(0,0,255),
"cyan" =>array(0,255,255),
"red" =>array(255,0,0),
"yellow" =>array(255,255,0),
"magenta" =>array(255,0,255),
"grey" =>array(128,128,128),
"white" =>array(255,255,255)
);
$tmpdist = 765;
$tmpname = "none";
foreach($colors as $colorname => $colorset) {
$r_dist = (pow($mycolor[0],2) - pow($colorset[0],2));
$g_dist = (pow($mycolor[1],2) - pow($colorset[1],2));
$b_dist = (pow($mycolor[2],2) - pow($colorset[2],2));
$totaldist = sqrt($r_dist + $g_dist + $b_dist);
if ($totaldist < $tmpdist) {
$tmpname = $colorname;
$tmpdist = $totaldist;
}
}
return $tmpname;
}
$sql = $pdo->prepare("SELECT * FROM colors");
$sql->execute();
foreach ($sql as $data)
{
$RGB = hexToRgb($data['HEX']);
$arry = explode(',', $RGB);
$DEC = "<".round(($arry[0]/255),3).",".round(($arry[1]/255),3).",".round(($arry[2]/255),3).">";
$color = getcolorname($arry);
$sql2 = $pdo->prepare("UPDATE `colors` SET `category` = :CAT, RGB = :RGB, LSL = :DEC WHERE (`color_ID` = :color_ID)");
$sql2->bindValue(':CAT', $color, PDO::PARAM_STR);
$sql2->bindValue(':RGB', $RGB, PDO::PARAM_STR);
$sql2->bindValue(':DEC', $DEC, PDO::PARAM_STR);
$sql2->bindValue(':color_ID', $data['color_ID'], PDO::PARAM_STR);
$sql2->execute();
}
echo "done";
I have a table of colors and I am trying to add color categories to each Color, but it is not categorizing correctly, any Help please.