Php rbg或hex以收集所选颜色?
我有一个不同的颜色六角提交。目前,我正在使用一个rgb2hex函数,所以要么使用,要么使用。 目标是我有一组需要使用的颜色,我希望现有的十六进制或rgb颜色更改为数组或其他颜色中最接近的颜色Php rbg或hex以收集所选颜色?,php,colors,gd,rgb,Php,Colors,Gd,Rgb,我有一个不同的颜色六角提交。目前,我正在使用一个rgb2hex函数,所以要么使用,要么使用。 目标是我有一组需要使用的颜色,我希望现有的十六进制或rgb颜色更改为数组或其他颜色中最接近的颜色 基本上,我有大约15个颜色值,我想要一个函数,它取rgb,看看哪一个最接近(数组)如果我理解正确,你想将任意十六进制颜色近似为预定义颜色吗 制作一个分割r-g-b(输入和矩阵)的函数 $inHex = array(r,g,b); $colArray = array(array(r1,g1,b1),array
基本上,我有大约15个颜色值,我想要一个函数,它取rgb,看看哪一个最接近(数组)如果我理解正确,你想将任意十六进制颜色近似为预定义颜色吗 制作一个分割r-g-b(输入和矩阵)的函数
$inHex = array(r,g,b);
$colArray = array(array(r1,g1,b1),array(...))
$minDiff = 10000;
$color = false;
for($i=0;$i<sizeof($colArray);$i++) {
$diff = abs($inHex(0) - $colArray[$i][0]) +
abs($inHex(1) - $colArray[$i][2]) +
abs($inHex(2) - $colArray[$i][2]);
if ($diff<$minDiff) $color = $i;
}
//ok, $color is pointing at closest color..
$inHex=数组(r,g,b);
$colArray=数组(数组(r1、g1、b1)、数组(…)
$minDiff=10000;
$color=false;
对于($i=0;$iEdit):您是否曾经在谷歌上搜索过某个东西,却只找到了自己的答案?以下是一个功能,如果它是自包含的,可能会更有用:
你可能想考虑完全使用一个完全不同的颜色模型。所以,如果我理解你的话,我首先需要做15个数组,很容易在$CalSub里面用R G B。
function convertToClosest($c) {
// set minimum difference you'll allow between colors
$minDiff = 1000;
// generate color array
$colorArrayOriginal = array(
"black" => "000000",
"brown" => "6E4700",
"gray" => "555555",
"white" => "FFFFFF",
"red" => "EB0000",
"orange" => "FF9914",
"yellow" => "FFF71C",
"green" => "1BB500",
"blue" => "005BB5",
"purple" => "4E00B5"
);
foreach ($colorArrayOriginal as $colorID => $color) {
$r = substr($color,0,2);
$g = substr($color,2,2);
$b = substr($color,4,2);
$colorArray[$colorID] = array($r,$g,$b);
}
// here, we break apart the color we input, $c
$r = substr($c,0,2);
$g = substr($c,2,2);
$b = substr($c,4,2);
$inHex = array($r,$g,$b);
$color = false;
// we define the "best so far" variable as the min, since we can't have a best that's more
$bestDiff = $minDiff;
// here, we parse through each of the colors finding the closest, using the native hexdec function to parse
// out the best values to compare
foreach ($colorArray as $colorID => $cc) {
$diff = abs(hexdec($inHex[0]) - hexdec($cc[0])) + abs(hexdec($inHex[1]) - hexdec($cc[1])) + abs(hexdec($inHex[2]) - hexdec($cc[2]));
// if the difference in value between the colors is less than the best one of all the ones we've tried...
if ($diff<=$bestDiff) {
$color = $colorID;
$bestDiff = $diff;
}
}
return $color;
}
function convertToClosest($c,$colorArray) {
// here, we break apart the color we input, $c
$r = substr($c,0,2);
$g = substr($c,2,2);
$b = substr($c,4,2);
$inHex = array($r,$g,$b);
$color = false;
// we define the "best so far" variable as the min, since we can't have a best that's more
$bestDiff = $minDiff;
// here, we parse through each of the colors finding the closest, using the native hexdec function to parse
// out the best values to compare
foreach ($colorArray as $colorID => $cc) {
$diff = abs(hexdec($inHex[0]) - hexdec($cc[0])) + abs(hexdec($inHex[1]) - hexdec($cc[1])) + abs(hexdec($inHex[2]) - hexdec($cc[2]));
// if the difference in value between the colors is less than the best one of all the ones we've tried...
if ($diff<=$bestDiff) {
$color = $colorID;
$bestDiff = $diff;
}
}
return $color;
}
$colorArrayOriginal = [two dimensional array of your colors ]
foreach ($colorArrayOriginal as $c) {
$r = substr($c["hex"],0,2);
$g = substr($c["hex"],2,2);
$b = substr($c["hex"],4,2);
$colorArray["$c[id]"] = array($r,$g,$b);
}