2017-05-25 65 views
0

这里最接近于给出的RGB值是一个网站,其中包含20种颜色是最简单,最明显的:http://sashat.me/2017/01/11/list-of-20-simple-distinct-colors/寻找哪种颜色是用C

我在做检测的颜色,并让他们有一个名称的程序。

的问题是,我需要一个函数,将:

  • 采取3个参数,R,G和B.

  • 确定哪个20的是该颜色最接近时该功能被赋予RGB。

这里是理想的功能的一些例子:

[127,2,1] -> Outputs Maroon 
[245,7,6] -> Outputs Red 
[7,235,0] -> Outputs Green 

如何使这样的事情,将不胜感激任何帮助!谢谢!

+4

https://en.wikipedia.org/wiki/Color_difference –

+0

我不是专家,但也许采取差异的总和或差异的平方和的最接近的价值? – agbinfo

+0

没有确切的答案 - 尝试创建一个感知均匀的色彩空间,让您在深水中非常迅速。但是,红色差异5倍,绿色差异9倍,蓝色差异2倍是一个好的开始。 –

回答

0

我已经回答了我自己的问题,以帮助未来的观众。

使用维基百科上找到的颜色公式和注释中显示的颜色公式,该功能将采用3个参数并返回最接近的颜色。

const int distinctRGB[22][3] = {{255, 255, 255},{0,0,0},{128,0,0},{255,0,0},{255, 200, 220},{170, 110, 40},{255, 150, 0},{255, 215, 180},{128, 128, 0},{255, 235, 0},{255, 250, 200},{190, 255, 0},{0, 190, 0},{170, 255, 195},{0, 0, 128},{100, 255, 255},{0, 0, 128},{67, 133, 255},{130, 0, 150},{230, 190, 255},{255, 0, 255},{128, 128, 128}}; 
const String distinctColors[22] = {"white","black","maroon","red","pink","brown","orange","coral","olive","yellow","beige","lime","green","mint","teal","cyan","navy","blue","purple","lavender","magenta","grey"}; 
String closestColor(int r,int g,int b) { 
    String colorReturn = "NA"; 
    int biggestDifference = 1000; 
    for (int i = 0; i < 22; i++) { 
    if (sqrt(pow(r - distinctRGB[i][0],2) + pow(g - distinctRGB[i][1],2) + pow(b - distinctRGB[i][2],2)) < biggestDifference) { 
     colorReturn = distinctColors[i]; 
     biggestDifference = sqrt(pow(r - distinctRGB[i][0],2) + pow(g - distinctRGB[i][1],2) + pow(b - distinctRGB[i][2],2)); 
    } 
    } 
    return colorReturn; 
} 

此功能,为了做到式较少打字使用Math.h