-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgb2name.cpp
49 lines (43 loc) · 1.15 KB
/
rgb2name.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// Created by Melih on 22.01.2017.
//
#include "rgb2name.h"
#include <cmath>
#include <limits>
using namespace std;
vector<vector<double>> RGB2NAME_KNOWN_COLORS = {
{255, 0, 0},
{255, 255, 0},
{0, 255, 0},
{0, 0, 255},
};
double euclideanDist(vector<double> rgb1, vector<double> rgb2) {
return pow(abs(rgb1[0] - rgb2[0]), 2) + pow(abs(rgb1[1] - rgb2[1]), 2) + pow(abs(rgb1[2] - rgb2[2]), 2);
}
/*
["0", "Red"],
["1", "Yellow"],
["2", "Green"],
["3", "Blue"],
*/
string rgb2name(vector<double> inputRGB) {
double min = numeric_limits<double>::max();
int minC = -1;
for(int i=0;i<RGB2NAME_KNOWN_COLORS.size();i++){
double distance = euclideanDist(inputRGB, RGB2NAME_KNOWN_COLORS[i]);
if(distance<min){
min = distance;
minC = i;
}
}
if (minC == 0) {
return std::__cxx11::string("Red");
} else if (minC == 1) {
return std::__cxx11::string("Yellow");
} else if (minC == 2) {
return std::__cxx11::string("Green");
} else if (minC == 3) {
return std::__cxx11::string("Blue");
}
return std::__cxx11::string(" ");
}