-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRGB.cpp
70 lines (64 loc) · 1.45 KB
/
RGB.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "stdafx.h"
RGB _tchar2RGB(_TCHAR* hexstr) {
RGB color;
int hex = _hex2int(hexstr);
color.red = ((hex >> 16) & 0xFF); // Extract the RR byte
color.green = ((hex >> 8) & 0xFF); // Extract the GG byte
color.blue = ((hex)& 0xFF); // Extract the BB byte
return color;
}
int _hex2int(const TCHAR *value)
{
struct CHexMap
{
TCHAR chr;
int value;
};
const int HexMapL = 16;
CHexMap HexMap[HexMapL] =
{
{ '0', 0 }, { '1', 1 },
{ '2', 2 }, { '3', 3 },
{ '4', 4 }, { '5', 5 },
{ '6', 6 }, { '7', 7 },
{ '8', 8 }, { '9', 9 },
{ 'A', 10 }, { 'B', 11 },
{ 'C', 12 }, { 'D', 13 },
{ 'E', 14 }, { 'F', 15 }
};
TCHAR *mstr = _tcsdup(value);
// mstr = _wcsupr(mstr);
TCHAR *s = mstr;
int result = 0;
if(*s == '0' && *(s + 1) == 'x') s += 2;
BOOL firsttime = 1;
while(*s != '\0')
{
BOOL found = 0;
for (int i = 0; i < HexMapL; i++)
{
if (*s == HexMap[i].chr)
{
if (!firsttime) result <<= 4;
result |= HexMap[i].value;
found = 1;
break;
}
}
if (!found) break;
s++;
firsttime = 0;
}
free(mstr);
return result;
}
int /* O - 0 = Out of fault tolerant, 1 = Inside of fault tolerant */
color_cmp(RGB *one, /* I - One color */
RGB *other, /* I - Other color */
double variation) /* I - fault tolerant */
{
double r = one->red - other->red;
double g = one->green - other->green;
double b = one->blue - other->blue;
return sqrt(abs(pow(r, 2) + pow(g, 2) + pow(b, 2))) <= variation;
}