php无法获得最接近的匹配颜色

发布于 2024-10-11 02:37:48 字数 1159 浏览 8 评论 0原文

我需要帮助从一组预定义颜色和单个随机颜色中找到最接近的颜色匹配,这是我的代码:

color = array('124','197','118'); // LIGHT GREEN

$match = array(
    array('255', '000', '000', 'FF0000'), //red
    array('000', '255', '000', '00FF00'), //green
    array('000', '000', '255', '0000FF'), //blue    
    array('0', '255', '255', '00ffff'), //cyan
    array('117', '076', '036', '754c24'), //brown
    array('000', '000', '000', '000000'), //black
    array('149', '149', '149', '959595'), //grey
    array('242', '101', '034', 'f26522'), //orange
    array('245', '152', '157', 'f5989d'), //pink
    array('255', '255', '000', 'FFFF00'), //yellow
    array('102', '045', '145', '662d91'), //purple
    array('255', '255', '255', 'FFFFFF')); //white

echo 'Color: <div style="background-color:#'.$color.';width:25px;height:25px"></div>';    //color

foreach($match as $co) $temp[] = array( sqrt(($color[0]-$co[0])^2+($color[1]-$co[1])^2+($color[2]-$co[2])^2) , $co[3]);

asort($temp);

foreach($temp as $ta) { echo 'Matched Color: <div style="background-color:#'.$ta[1].';width:25px;height:25px"></div>'; break; } 

它返回灰色而不是绿色?我该如何解决这个问题?比

I need help finding the closest color match from a set of predefined colors and a single random color, here is my code:

color = array('124','197','118'); // LIGHT GREEN

$match = array(
    array('255', '000', '000', 'FF0000'), //red
    array('000', '255', '000', '00FF00'), //green
    array('000', '000', '255', '0000FF'), //blue    
    array('0', '255', '255', '00ffff'), //cyan
    array('117', '076', '036', '754c24'), //brown
    array('000', '000', '000', '000000'), //black
    array('149', '149', '149', '959595'), //grey
    array('242', '101', '034', 'f26522'), //orange
    array('245', '152', '157', 'f5989d'), //pink
    array('255', '255', '000', 'FFFF00'), //yellow
    array('102', '045', '145', '662d91'), //purple
    array('255', '255', '255', 'FFFFFF')); //white

echo 'Color: <div style="background-color:#'.$color.';width:25px;height:25px"></div>';    //color

foreach($match as $co) $temp[] = array( sqrt(($color[0]-$co[0])^2+($color[1]-$co[1])^2+($color[2]-$co[2])^2) , $co[3]);

asort($temp);

foreach($temp as $ta) { echo 'Matched Color: <div style="background-color:#'.$ta[1].';width:25px;height:25px"></div>'; break; } 

It returns grey instead of green? How can I fix this problem? Than

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

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

发布评论

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

评论(2

霞映澄塘 2024-10-18 02:37:48

这里是一个匹配问题及其答案的链接:

RGB to 最接近的预定义颜色

但是,如果您已经有索引图像,我建议使用它:

http://php. net/manual/en/function.imagecolorclosest.php

here is a link to a matching question with answer:

RGB to closest predefined color

but, if you have an indexed image already, I suggest using this instead:

http://php.net/manual/en/function.imagecolorclosest.php

彡翼 2024-10-18 02:37:48

我把你的颜色放入我的 颜色转换器来分析您的问题。我想你可以看到灰色也是视觉上最接近的搭配。

我认为三色 124/197/118 不是浅绿色,但最重要的是一种不饱和的绿色,这解释了结果。

我不确定你的一些颜色定义。对我来说

000 255 000 = Lime Green
000 128 000 = Green
128 128 128 = Gray (50%)

如果您仍然不喜欢结果:

1, leave out gray from your predefined colors

或者

2, define a threshold for returning gray
-> If result is gray but distance > threshold x,
then take the second colosest match

I put your colors into my color converter to analyze your problem. I think you can see that gray is also the colosest match visually.

I think the tripple 124/197/118 is not a light green but foremost a desaturated green which explains the result.

I'm not sure about some of your color definitions. For me

000 255 000 = Lime Green
000 128 000 = Green
128 128 128 = Gray (50%)

If you still don't like the result:

1, leave out gray from your predefined colors

or

2, define a threshold for returning gray
-> If result is gray but distance > threshold x,
then take the second colosest match
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文