-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgb2hsl.c
110 lines (83 loc) · 1.84 KB
/
rgb2hsl.c
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include"stdlib.h"
#include"stdio.h"
float Min( float fR, float fG, float fB );
float Max( float fR, float fG, float fB);
void RGBToHSL( int R, int G, int B, int* h, int* s, int* l );
int main(int argc, char *argv[]){
int r, g, b, h, s, l;
if(argc < 2) {
printf("Please input RGB hex code in this format : XXXXXX\n");
return 1;
}
for(int i = 1; i<argc; ++i){
char *hexrgb = argv[i];
int rgb = (int)strtol(hexrgb, NULL, 16);
r = (rgb >> 16) & 0x0000FF;
g = (rgb >> 8) & 0x0000FF;
b = rgb & 0x0000FF;
RGBToHSL(r, g, b, &h, &s, &l);
// printf("%d %d %d\n", h, s, l);
printf("%d %s\n", s + (int)(l*0.7), hexrgb );
}
return 0;
}
float Min( float fR, float fG, float fB ){
float fMin = fR;
if (fG < fMin){
fMin = fG;
}
if (fB < fMin){
fMin = fB;
}
return fMin;
}
float Max( float fR, float fG, float fB){
float fMax = fR;
if (fG > fMax){
fMax = fG;
}
if (fB > fMax){
fMax = fB;
}
return fMax;
}
void RGBToHSL( int R, int G, int B, int* h, int* s, int* l ){
int H = *h;
int S = *s;
int L = *l;
float fR = R / 255.0;
float fG = G / 255.0;
float fB = B / 255.0;
float fCMin = Min(fR, fG, fB);
float fCMax = Max(fR, fG, fB);
L = 50 * (fCMin + fCMax);
if (fCMin == fCMax){
S = 0;
H = 0;
*s = S;
*h = H;
*l = L;
return ;
}
else if (L < 50){
S = 100 * (fCMax - fCMin) / (fCMax + fCMin);
}
else{
S = 100 * (fCMax - fCMin) / (2.0 - fCMax - fCMin);
}
if (fCMax == fR){
H = 60 * (fG - fB) / (fCMax - fCMin);
}
if (fCMax == fG){
H = 60 * (fB - fR) / (fCMax - fCMin) + 120;
}
if (fCMax == fB){
H = 60 * (fR - fG) / (fCMax - fCMin) + 240;
}
if (H < 0){
H = H + 360;
}
*h = H;
*s = S;
*l = L;
}