rbg 或 hex 来收集所选颜色?

发布于 2024-10-08 04:39:11 字数 158 浏览 4 评论 0原文

我提交了不同颜色的六角形。目前我使用 rgb2hex 函数,所以 或 都可以。 目标是我有一组需要使用的颜色,并且我希望现有颜色十六进制或 RGB 更改为数组或其他颜色最接近的颜色。

基本上,我有大约 15 个颜色值,我基本上想要一个函数,它接受 rgb 并查看它最接近(数组的)哪一个

i have a submission of different color hexs. At the moment im using a rgb2hex function so either or works.
The goal is I have a set of colors I need to use, and I want the existing colors hex or rgb to change to the closest color of an array or something.

basically, I have about 15 color values, and I want basically a function that takes the rgb and see which one its closest to (of an array)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨的味道风的声音 2024-10-15 04:39:11

编辑:你是否曾经用谷歌搜索过一些东西却找到了自己的答案?这是一个可能更有用的函数,因为它是独立的:


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;
}

另外,如果您想生成 $colorArray 变量

$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);    
}

Edit: Have you ever googled around for something only to find your own answer? Here is a function that might be more useful insofar as it's self-contained:


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;
} 

Here's the original:

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;
}

also, if you want to generate the $colorArray variable

$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);    
}
掩饰不了的爱 2024-10-15 04:39:11

如果我理解正确的话,您想将任意十六进制颜色近似为预定义的颜色吗?

创建一个函数来分割 RGB、输入和矩阵。

$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..

问候,
//t

If I understand you correctly you want to approximate an arbitrarily hex-color to pre-defined?

Make a function that chops up r-g-b, both the input and your matrix.

$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..

regards,
//t

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文