-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.c
84 lines (76 loc) · 1.76 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
83
84
#include <stdio.h>
#include <stdlib.h>
#include "color.h"
#include <string.h>
void init_color(COLOR *c, int r, int g, int b)
{
(*c) = (COLOR)malloc(sizeof(Color));
(*c)->red = r;
(*c)->green = g;
(*c)->blue = b;
}
int *getArray(COLOR c)
{
int *arr = (int *)malloc(sizeof(int));
// arr[0] = c->red;
// arr[1] = c->green;
// arr[2] = c->blue;
// arr[3] = arr[0] + arr[1] + arr[2];
return arr;
}
char *int_to_str(int a)
{
char *temp = (char *)malloc(sizeof(char) * 100);
sprintf(temp, "%d", a);
return temp;
}
char *toString(COLOR c)
{
char *str = malloc(sizeof(char) * 100);
char *r = int_to_str(c->red);
char *g = int_to_str(c->green);
char *b = int_to_str(c->blue);
int i = 0;
for (int j = 0; j < strlen(r); j++)
{
str[i] = r[j];
i++;
}
for (int j = 0; j < strlen(g); j++)
{
str[i] = g[j];
i++;
}
for (int j = 0; j < strlen(b); j++)
{
str[i] = b[j];
i++;
}
return str;
}
COLOR normalized(COLOR c, int pixelCount)
{
COLOR col = (COLOR)malloc(sizeof(Color));
col->red = c->red / pixelCount;
col->green = c->green / pixelCount;
col->blue = c->blue / pixelCount;
return col;
}
// void addColor(COLOR c1, COLOR c2)
// {
// c1->red = c1->red + c2->red;
// c1->green = c1->green + c2->green;
// c1->blue = c1->blue + c2->blue;
// }
int getColorIndex(COLOR color, int level)
{
int index = 0;
int mask = 0b10000000 >> level;
if (color->red & mask)
index = index | 0b100;
if (color->green & mask)
index = index | 0b010;
if (color->blue & mask)
index = index | 0b001;
return index;
}