-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyColor.c
More file actions
118 lines (84 loc) · 2.27 KB
/
yColor.c
File metadata and controls
118 lines (84 loc) · 2.27 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
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
111
112
113
114
115
116
117
/*
* Copyright (c) 2009-2017 Yannick Garcia <thaddeus.dupont@free.fr>
*
* yImage is free software; you can redistribute it and/or modify
* it under the terms of the GPL license. See LICENSE for details.
*/
/**
* \file yColor.c
* \brief Implementation of four channels color representation.
*/
#include "yColor.h"
#include <stdlib.h>
void y_init_palette(yColorPalette_t palette, const uint8_t *pal){
int i, j; /* compteur */
if(pal==NULL) return;
for(i=0; i<=255; i++){
for(j=0; j<=2; j++) {
palette[3*i+j]=pal[3*i+j]*4;
}
}
}
int y_get_color_index(yColor *color, yColorPalette_t palette, int index){
if(color==NULL) return(ERR_NULL_COLOR);
if(palette==NULL) return(ERR_NULL_PALETTE);
if((index<0)||(index>255)) return(ERR_BAD_INDEX);
color->r=palette[3*index];
color->g=palette[3*index+1];
color->b=palette[3*index+2];
color->alpha=255;
return(0);
}
// yColor functions
yColor *y_color(ySimpleColor color){
int r,g,b;
yColor *result;
result = malloc(sizeof(yColor));
if(result == NULL) return NULL;
switch(color) {
case BLACK:
r=0; g=0; b=0; break;
case WHITE:
r=255; g=255; b=255; break;
case RED:
r=255; g=0; b=0; break;
case GREEN:
r=0; g=255; b=0; break;
case BLUE:
r=0; g=0; b=255; break;
case ORANGE:
r=255; g=160; b=0; break;
case YELLOW:
r=255; g=255; b=0; break;
case CYAN:
r=0; g=255; b=255; break;
case MAGENTA:
r=255; g=0; b=255; break;
case MARRON:
r=112; g=80; b=0; break;
default:
r=0; g=0; b=0;
}
y_set_color(result, r, g, b, 255);
return result;
}
void y_set_color(yColor *color, unsigned char r, unsigned char g, unsigned char b, unsigned char a){
color->r=r;
color->g=g;
color->b=b;
color->alpha=a;
}
void y_init_color(yColor *color, unsigned int rgba){
color->r=rgba/(256*256*256);
color->g=(rgba/(256*256))%256;
color->b=(rgba/256)%(256*256);
color->alpha=rgba%(256*256*256);
}
int y_compare_colors(yColor *c1, yColor *c2) {
int comp=0;
if(c1->r != c2->r) comp++;
if(c1->g != c2->g) comp++;
if(c1->b != c2->b) comp++;
if(c1->alpha != c2->alpha) comp++;
return comp;
}