查找最近的RGB颜色 [英] Finding nearest RGB colour

查看:186
本文介绍了查找最近的RGB颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被告知要使用距离公式来查找颜色是否与其他颜色匹配,所以我有,

I was told to use distance formula to find if the color matches the other one so I have,

struct RGB_SPACE
{
    float R, G, B;
};

RGB_SPACE p = (255, 164, 32);  //pre-defined
RGB_SPACE u = (192, 35, 111);  //user defined

long distance = static_cast<long>(pow(u.R - p.R, 2) + pow(u.G - p.G, 2) + pow(u.B - p.B, 2));

这只是一个距离,但我怎么知道颜色是否匹配用户定义的至少25%?

this gives just a distance, but how would i know if the color matches the user-defined by at least 25%?

我不是很确定,但我有一个想法,检查每个颜色值,看看差异是否为25%。例如

I'm not just sure but I have an idea to check each color value to see if the difference is 25%. for example.

float R = u.R/p.R * 100;
float G = u.G/p.G * 100;
float B = u.B/p.B * 100;

if (R <= 25 && G <= 25 && B <= 25)
{
   //color matches with pre-defined color.
}


推荐答案

在RGB空间中。如果你有(0,0,0)和(100,0,0),他们是相似的,根据cababungas公式(以及根据casablanca的考虑太多的颜色类似)。但是,他们看起来很不一样。

I would suggest not to check in RGB space. If you have (0,0,0) and (100,0,0) they are similar according to cababungas formula (as well as according to casablanca's which considers too many colors similar). However, they LOOK pretty different.

HSL和HSV 颜色模型是基于人类对颜色的解释,然后您可以轻松地相互独立地指定色调,饱和度和亮度的距离(取决于您的情况下类似的含义)。

The HSL and HSV color models are based on human interpretation of colors and you can then easily specify a distance for hue, saturation and brightness independently of each other (depending on what "similar" means in your case).

这篇关于查找最近的RGB颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆