This repository was archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhex2rgb.c
More file actions
77 lines (61 loc) · 1.49 KB
/
hex2rgb.c
File metadata and controls
77 lines (61 loc) · 1.49 KB
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
/*
Hex to RGB converter
gcc -o hex2rgb hex2rgb.c
./hex2rgb [hex]
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int unsigned segby = 0;
char* segarr[2];
char** hex;
int rgb[2];
int validation(char* data) {
int unsigned inputCount;
inputCount = strlen(data);
if (data[strspn(data, "0123456789abcdefABCDEF")] != 0) {
printf("Error: Format is invalid.\n");
return 1;
}
else if (inputCount == 3) {
segby = 1;
} else if (inputCount == 6) {
segby = 2;
} else {
printf("Error: Hex must be 3 or 6 characters long.\n");
return 1;
}
}
char** segregate(char* data) {
segarr[0] = strndup(data, segby);
segarr[1] = strndup(data + segby, segby);
segarr[2] = strndup(data + segby + segby, segby);
return segarr;
}
int* rgbify(char* data) {
int loopomax = 3;
int loopo = 0;
char** hex = segregate(data);
while (loopo < loopomax) {
if (segby == 1) {
strcat(hex[loopo], hex[loopo]);
}
rgb[loopo] = strtol(hex[loopo], NULL, 16);
loopo++;
}
free(segarr[0]);
free(segarr[1]);
free(segarr[2]);
return rgb;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("%s: Too few arguments.\n", argv[0]);
printf("%s [hex]\n", argv[0]);
return 1;
}
if (validation(argv[1]) == 1) exit(1);
int *rgbdata = rgbify(argv[1]);
printf("rgb(%d,%d,%d)\n", rgbdata[0], rgbdata[1], rgbdata[2]);
return 0;
}