-
Notifications
You must be signed in to change notification settings - Fork 1
/
color.c
82 lines (72 loc) · 1.91 KB
/
color.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* color.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: azaghlou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/19 23:25:38 by yhachami #+# #+# */
/* Updated: 2023/08/31 05:15:13 by yhachami ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
int find_end(char *s)
{
int i;
i = 0;
while (s[i] != ' ' && s[i] != '\n' && s[i] != '\0')
i++;
return (i);
}
t_rgb int2rgb(long long int mono)
{
t_rgb color;
if (mono > 0xFFFFFF)
{
color.r = (mono >> 24) & 0xFF;
color.g = (mono >> 16) & 0xFF;
color.b = (mono >> 8) & 0xFF;
color.a = mono & 0xFF;
}
else
{
color.r = (mono >> 16) & 0xFF;
color.g = (mono >> 8) & 0xFF;
color.b = mono & 0xFF;
color.a = 255;
}
return (color);
}
int rgb2int(t_rgb c)
{
return (c.r << 24 | c.g << 16 | c.b << 8 | c.a);
}
int get_chanel(char *s)
{
int i;
int v;
int base;
i = find_end(s);
v = 0;
base = 1;
while (--i >= 0)
{
if (s[i] >= 'A' && s[i] <= 'Z')
v += (s[i] - 55) * base;
if (s[i] >= 'a' && s[i] <= 'z')
v += (s[i] - 87) * base;
else if (s[i] >= '0' && s[i] <= '9')
v += (s[i] - '0') * base;
base *= 16;
}
return (v);
}
t_rgb get_color(char *s)
{
long long int mono;
if (s[0] == '0' && s[1] == 'x')
mono = get_chanel(s + 2);
else
mono = get_chanel("FFFFFF");
return (int2rgb(mono));
}