2017-05-25 45 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