这是一个包含20种颜色的网站,这些颜色最简单明了:http://sashat.me/2017/01/11/list-of-20-simple-distinct-colors/
我正在制作一个程序来检测颜色并为它们命名。
问题是我需要一个能够:
的功能接受3个参数,R,G和B.
当函数被赋予RGB时,确定20中哪一个是最接近的颜色。
以下是理想函数的一些示例:
[127,2,1] -> Outputs Maroon
[245,7,6] -> Outputs Red
[7,235,0] -> Outputs Green
如何制作这样的东西的任何帮助将不胜感激!谢谢!
答案 0 :(得分:2)
我已经回答了我自己的问题,以帮助未来的观众。
使用维基百科上的色差公式并在评论中显示,此功能将采用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
来减少输入的公式。