File: color.h

package info (click to toggle)
fractalnow 0.8.2-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,656 kB
  • sloc: ansic: 8,201; cpp: 4,517; sh: 571; makefile: 9
file content (248 lines) | stat: -rw-r--r-- 7,054 bytes parent folder | download | duplicates (5)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 *  color.h -- part of FractalNow
 *
 *  Copyright (c) 2011 Marc Pegon <pe.marc@free.fr>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

 /**
  * \file color.h
  * \brief Header file related to colors.
  * \author Marc Pegon
  */

#ifndef __COLOR_H__
#define __COLOR_H__

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \def GET_R8(x)
 * \brief Get red component of uint32_t color.
 */
#define GET_R8(x) ((x >> 16) & 0xFF)

/**
 * \def GET_G8(x)
 * \brief Get green component of uint32_t color.
 */
#define GET_G8(x) ((x >> 8) & 0xFF)

/**
 * \def GET_B8(x)
 * \brief Get blue component of uint32_t color.
 */
#define GET_B8(x) (x & 0xFF)

/**
 * \def RGB8_TO_UINT32(r,g,b)
 * \brief Create uint32_t color from 8-bits red, green, and blue components.
 *
 * alpha is set to 0xFF by default.
 */
#define RGB8_TO_UINT32(r,g,b) (0xFF000000+((uint32_t)(r)<<16)+((uint32_t)(g)<<8)+(uint32_t)b)

/**
 * \def ARGB8_TO_UINT32(a,r,g,b)
 * \brief Create uint32_t color from 8-bits alpha, red, green, and blue components.
 */
#define ARGB8_TO_UINT32(a,r,g,b) (((uint32_t)(a)<<24)+((uint32_t)(r)<<16)+((uint32_t)(g)<<8)+(uint32_t)b)

/**
 * \def GET_R16(x)
 * \brief Get red component of uint64_t color.
 */
#define GET_R16(x) ((x >> 32) & 0xFFFF)

/**
 * \def GET_G16(x)
 * \brief Get green component of uint64_t color.
 */
#define GET_G16(x) ((x >> 16) & 0xFFFF)

/**
 * \def GET_B16(x)
 * \brief Get blue component of uint64_t color.
 */
#define GET_B16(x) (x & 0xFFFF)

/**
 * \def RGB16_TO_UINT64(r,g,b)
 * \brief Create uint64_t color from 16-bits red, green, and blue components.
 *
 * alpha is set to 0xFFFF by default.
 */
#define RGB16_TO_UINT64(r,g,b) (0xFFFF000000000000+((uint64_t)(r)<<32)+((uint64_t)(g)<<16)+(uint64_t)b)

/**
 * \def ARGB16_TO_UINT64(r,g,b)
 * \brief Create uint64_t color from 16-bits alpha, red, green, and blue components.
 */
#define ARGB16_TO_UINT64(a,r,g,b) (((uint64_t)(a)<<48)+((uint64_t)(r)<<32)+((uint64_t)(g)<<16)+(uint64_t)b)

/**
 * \struct Color
 * \brief Simple RGB color structure.
 */
/**
 * \typedef Color
 * \brief Convenient typedef for struct Color.
 */
typedef struct Color {
	uint_fast8_t bytesPerComponent; /*!< Either 1 (RGB8) or 2 (RGB16).*/
	uint_fast16_t r; /*!< Red component.*/
	uint_fast16_t g; /*!< Green component.*/
	uint_fast16_t b; /*!< Blue component.*/
} Color;

/**
 * \fn Color ColorFromRGB(uint8_t bytesPerComponent, uint16_t r, uint16_t g, uint16_t b)
 * \brief Create color from RGB values.
 *
 * bytesPerComponent must be 1 or 2.
 *
 * \param bytesPerComponent Bytes per component.
 * \param r Red value.
 * \param g Green value.
 * \param b Blue value.
 * \return Corresponding color structure.
 */
Color ColorFromRGB(uint8_t bytesPerComponent, uint16_t r, uint16_t g, uint16_t b);

/**
 * \fn Color ColorFromUint32(uint32_t color)
 * \brief Convert a uint32_t color to Color structure.
 *
 * Creates a RGB8 Color structure.
 * alpha component (most significant 8 bits) is ignored.
 *
 * \param color Color to convert.
 * \return Converted color.
 */
Color ColorFromUint32(uint32_t color);

/**
 * \fn Color ColorFromUint64(uint64_t color)
 * \brief Convert a uint64_t color to Color structure.
 *
 * Creates a RGB16 Color structure.
 * alpha component (most significant 16 bits) is ignored.
 *
 * \param color Color to convert.
 * \return Converted color.
 */
Color ColorFromUint64(uint64_t color);

/**
 * \fn Color Color16(Color color)
 * \brief Convert color to 16 bits color.
 *
 * Simple copy if color bytes per component is already 2.
 *
 * \param color Color to be converted.
 * \return 16 bits color.
 */
Color Color16(Color color);

/**
 * \fn Color Color8(Color color)
 * \brief Convert color to 8 bits color.
 *
 * Simple copy if color bytes per component is already 1.
 *
 * \param color Color to be converted.
 * \return 8 bits color.
 */
Color Color8(Color color);

/**
 * \fn int CompareColors(const Color color1, const Color color2)
 * \brief Compare two colors.
 *
 * \param color1 First color.
 * \param color2 Second color.
 * \return 0 if colors are the same, 1 otherwise.
 */
int CompareColors(const Color color1, const Color color2);

/**
 * \fn Color MixColors(Color C1, double weight1, Color C2, double weight2)
 * \brief Mix two weighted colors.
 *
 * Both colors must have same number of bytes per component
 * (undefined behaviour otherwise).
 *
 * \param C1 First Color.
 * \param weight1 Weight given to first color for mixing.
 * \param C2 Second Color.
 * \param weight2 Weight given to second color for mixing.
 * \return Result of the mixing of colors C1 and C2 according to given weights.
 */
Color MixColors(Color C1, double weight1, Color C2, double weight2);

/**
 * \fn double ColorManhattanDistance(Color C1, Color C2)
 * \brief Compute manhattan distance between two colors.
 *
 * Distance is normalized (i.e. between 0 and 1).\n
 * Both colors must have same number of bytes per component
 * (undefined behaviour otherwise).
 *
 * \param C1 First color.
 * \param C2 Second color.
 * \return Manhattan distance between colors C1 and C2.
 */
double ColorManhattanDistance(Color C1, Color C2);

/**
 * \fn double QuadAvgDissimilarity(const Color C[4])
 * \brief Compute average dissimilarity of a quadrilateral given its corner colors.
 *
 * Result is normalized (between 0 and 1).\n
 * Colors must have same number of bytes per component
 * (undefined behaviour otherwise).
 *
 * \param C Point to colors at the four corners.
 * \return Quadrilateral average anhattan dissimilarity.
 */
double QuadAvgDissimilarity(const Color C[4]);

/**
 * \fn Color QuadLinearInterpolation(const Color C[4], double x, double y)
 * \brief Interpolate linearly some color of a quadrilateral.
 *
 * Interpolate color at point (x,y) according to its corner colors.\n
 * Coordinates x and y are relative (undefined behaviour otherwise) :
 * (0,0 is the top left corner, (1,0) the top right, etc...\n
 * Colors must have same number of bytes per component
 * (undefined behaviour otherwise).
 *
 * \param C Quadrilateral corner colors.
 * \param x X (relative) coordinate of quad point to interpolate.
 * \param y Y (relative) coordinate of quad point to interpolate.
 * \return Linearly interpolated color at point (x,y) of quadrilateral.
 */
Color QuadLinearInterpolation(const Color C[4], double x, double y);

#ifdef __cplusplus
}
#endif

#endif