BRL-CAD
Loading...
Searching...
No Matches
color.h
Go to the documentation of this file.
1/* C O L O R . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-2026 United States Government as represented by
5 * the U.S. Army Research Laboratory.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * version 2.1 as published by the Free Software Foundation.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this file; see the file named COPYING for more
18 * information.
19 */
20
21#ifndef BU_COLOR_H
22#define BU_COLOR_H
23
24#include "common.h"
25
26#include "vmath.h"
27
28#include "bu/defines.h"
29#include "bu/magic.h"
30
32
33struct bu_vls;
34
35/** @addtogroup bu_color
36 * @brief
37 * Support for storing and manipulating color data.
38 */
39/** @{ */
40/** @file bu/color.h */
41
42#define RED 0
43#define GRN 1
44#define BLU 2
45#define ALP 3
46
47#define BU_COLOR_INDEX_RED 0
48#define BU_COLOR_INDEX_GREEN 1
49#define BU_COLOR_INDEX_BLUE 2
50#define BU_COLOR_INDEX_ALPHA 3
51
52#define HUE 0
53#define SAT 1
54#define VAL 2
55
56#define BU_COLOR_INDEX_HUE 0
57#define BU_COLOR_INDEX_SATURATION 1
58#define BU_COLOR_INDEX_VALUE 2
59
60/**
61 * A single color value, stored as a normalized RGBA quadruple.
62 */
64{
66};
67typedef struct bu_color bu_color_t;
68#define BU_COLOR_NULL ((struct bu_color *) 0)
69
70/** Color spaces supported by bu_color_convert(). */
76
77/** Text representations supported by bu_color_format(). */
89
90/**
91 * initializes a bu_color struct without allocating any memory.
92 */
93#define BU_COLOR_INIT(_c) { \
94 (_c)->buc_rgb[RED] = (_c)->buc_rgb[GRN] = (_c)->buc_rgb[BLU] = 0; (_c)->buc_rgb[ALP] = 0; \
95 }
96
97/**
98 * Check whether two colors are equal within a tolerance.
99 */
100#define BU_COLOR_NEAR_EQUAL(_c1, _c2, _tol) \
101 HNEAR_EQUAL(_c1.buc_rgb, _c2.buc_rgb, _tol)
102
103
104/**
105 * macro suitable for declaration statement initialization of a bu_color
106 * struct. does not allocate memory.
107 */
108#define BU_COLOR_INIT_ZERO {{0, 0, 0, 0}}
109
110/* Initializers for commonly used colors */
111#define BU_COLOR_BLUE {{0, 0, 1, 0}}
112#define BU_COLOR_CYAN {{0, 1, 1, 0}}
113#define BU_COLOR_GREEN {{0, 1, 0, 0}}
114#define BU_COLOR_PURPLE {{1, 0, 1, 0}}
115#define BU_COLOR_RED {{1, 0, 0, 0}}
116#define BU_COLOR_WHITE {{1, 1, 1, 0}}
117#define BU_COLOR_YELLOW {{1, 1, 0, 0}}
118
119/**
120 * Copy a bu_color
121 */
122#define BU_COLOR_CPY(_dest, _src) {\
123 (_dest)->buc_rgb[RED] = (_src)->buc_rgb[RED]; \
124 (_dest)->buc_rgb[GRN] = (_src)->buc_rgb[GRN]; \
125 (_dest)->buc_rgb[BLU] = (_src)->buc_rgb[BLU]; \
126 (_dest)->buc_rgb[ALP] = (_src)->buc_rgb[ALP]; \
127}
128
129
130/** random color generating methods */
135
136/**
137 * Function to generate random color
138 *
139 * Refactoring points:
140 * truly random color
141 * 3dm-g: src/libgcv/plugins/rhino/rhino_read.cpp
142 * "constrained" random
143 * BRLCADWrapper:getRandomColor(): src/conv/step/BRLCADWrapper.cpp
144
145 */
147
148#if 0
149
150/**
151 * Refactoring points:
152 * color command (set specified color)
153 * src/libged/color.c
154 * src/libged/brep.c
155 * get color from string
156 * src/libbu/color.c
157
158* Possible calling syntax:
159 @code
160 * // draw a purely random color in 0/0/0 to 255/255/255 range
161 * bn_color_samples(colors, NULL, COLOR_RANDOM, 1); // problematic in libbu, random is libbn domain
162 *
163 * // draw a golden ratio distribution random color in 0/0/0 to 255/255/255 range, s=0.5, v=0.95
164 * bn_color_samples(colors, NULL, COLOR_RANDOM_LIGHTENED, 1); // problematic in libbu, random is libbn domain
165 *
166 * // draw bezier interpolated and lightened samples
167 * struct bu_color range[4] = {0};
168 * bu_color_from_str(&range[0], "#0f0"); // green
169 * bu_color_from_str(&range[1], "0.f/0.f/1.f") // blue
170 * bu_color_from_str(&range[2], "purple");
171 * bn_color_samples(colors, range, COLOR_LINEAR, 10); // 10 dark colors from green to blue to purple
172 *
173 * // return a standard "heat map" with 18 quantized samples
174 * bn_color_samples(colors, NULL, COLOR_STANDARD_HEAT, 18);
175 @endcode
176 *
177 * Need:
178 * way to map from different color specifications to color including
179 * name: "red"
180 * rgbint: 255/0/0
181 * rgbfloat: 1.0f/0f/0f
182 * hexint: #FF0000
183 * hexshort: #F00
184 * hsv: 0/100%/100%
185 * hsl: 0/100%/50%
186 * ignoring YCbCr, YPbPr, YUV, YIQ, CMYK, CIE LAB
187 * ignoring grayscale specification
188 */
189/**
190 * Return a set of sampled colors given a range of zero or more colors
191 * (a NULL-terminated list of colors), a sample method, and desired
192 * number of color samples to return.
193 *
194 * Specifying no colors implies full spectrum. The default sampling
195 * method uses a golden ratio distribution to give a "balanced" random
196 * distribution that is effective with dark backgrounds and/or text.
197 *
198 * Returns the number of samples allocated.
199 */
200size_t bn_color_samples(struct bu_color **samples, const bu_color *colors, enum sampleMethod, size_t numSamples);
201#endif
202
203
204/**
205 * Convert between RGB and HSV color models
206 *
207 * R, G, and B are in {0, 1, ..., 255},
208 *
209 * H is in [0.0, 360.0), and S and V are in [0.0, 1.0],
210 *
211 * If S == 0.0, H is achromatic and set to 0.0
212 *
213 * These two routines are adapted from:
214 * pp. 592-3 of J.D. Foley, A. van Dam, S.K. Feiner, and J.F. Hughes,
215 * _Computer graphics: principles and practice_, 2nd ed., Addison-Wesley,
216 * Reading, MA, 1990.
217 */
218BU_EXPORT extern void bu_rgb_to_hsv(const unsigned char *rgb, fastf_t *hsv);
219BU_EXPORT extern int bu_hsv_to_rgb(const fastf_t *hsv, unsigned char *rgb);
220
221
222/**
223 * Parse a human-readable color specification.
224 *
225 * Accepted representations include the legacy BRL-CAD integer and
226 * normalized floating point RGB triplets, hexadecimal RGB and RGBA,
227 * CSS named colors, and RGB(A), HSL(A), and HSV(A) functional notation.
228 * RGB and alpha are normalized to [0.0, 1.0] in color. An omitted alpha
229 * channel is set to 1.0 (opaque).
230 *
231 * Returns 1 on success and 0 on failure. On failure, color is unchanged.
232 */
233BU_EXPORT extern int bu_color_parse(const char *str, struct bu_color *color);
234
235/**
236 * Append a human-readable representation of color to output.
237 *
238 * RGB(A) uses functional notation with 8-bit RGB channels and normalized
239 * alpha. HSL(A) and HSV(A) use hue in degrees, percentages for the other
240 * model channels, and normalized alpha. Hexadecimal output uses CSS RGBA
241 * channel order. Named output fails when color has no exact CSS name.
242 *
243 * Returns 1 on success and 0 on failure. On failure, output is unchanged.
244 */
245BU_EXPORT extern int bu_color_format(const struct bu_color *color,
246 bu_color_format_t format,
247 struct bu_vls *output);
248
249/**
250 * Convert a numeric color between RGB, HSL, and HSV.
251 *
252 * RGB channels, saturation, lightness, value, and alpha use [0.0, 1.0].
253 * Hue is expressed in degrees and is wrapped to [0.0, 360.0). Alpha is
254 * copied without conversion. All four input components must be finite.
255 *
256 * Returns 1 on success and 0 on failure. On failure, out is unchanged.
257 */
258BU_EXPORT extern int bu_color_convert(const double in[4],
261 double out[4]);
262
263
264/**
265 * Legacy utility functions for converting RGB storage containers.
266 * New color-space and text conversions should use bu_color_convert(),
267 * bu_color_parse(), and bu_color_format().
268 */
269BU_EXPORT extern int bu_color_from_rgb_floats(struct bu_color *cp, const fastf_t *rgb);
270BU_EXPORT extern int bu_color_from_rgb_chars(struct bu_color *cp, const unsigned char *rgb);
271/** Compatibility parser equivalent to bu_color_parse(), except that cp's
272 * alpha channel is left unchanged.
273 */
274BU_EXPORT extern int bu_color_from_str(struct bu_color *cp, const char *str);
275/* UNIMPLEMENTED: BU_EXPORT extern int bu_color_from_hsv_floats(struct bu_color *cp, fastf_t *hsv); */
276
277BU_EXPORT extern int bu_str_to_rgb(const char *str, unsigned char *rgb); /* inconsistent, deprecate */
278
279BU_EXPORT extern int bu_color_to_rgb_floats(const struct bu_color *cp, fastf_t *rgb); /* bu_color_as_rgb_3fv */
280BU_EXPORT extern int bu_color_to_rgb_chars(const struct bu_color *cp, unsigned char *rgb); /* bu_color_as_rgb */
281BU_EXPORT extern int bu_color_to_rgb_ints(const struct bu_color *cp, int *r, int *g, int *b); /* bu_color_as_rgb_3i */
282/* UNIMPLEMENTED: BU_EXPORT extern int bu_color_to_hsv_floats(struct bu_color *cp, fastf_t *hsv); */ /* bu_color_as_hsv_3fv */
283
284
285/** @} */
286
288
289#endif /* BU_COLOR_H */
290
291/*
292 * Local Variables:
293 * mode: C
294 * tab-width: 8
295 * indent-tabs-mode: t
296 * c-file-style: "stroustrup"
297 * End:
298 * ex: shiftwidth=4 tabstop=8
299 */
Definition dvec.h:74
Header file for the BRL-CAD common definitions.
int bu_color_convert(const double in[4], bu_color_space_t in_space, bu_color_space_t out_space, double out[4])
int bu_color_to_rgb_ints(const struct bu_color *cp, int *r, int *g, int *b)
int bu_color_from_str(struct bu_color *cp, const char *str)
int bu_color_from_rgb_floats(struct bu_color *cp, const fastf_t *rgb)
enum bu_color_format bu_color_format_t
void bu_rgb_to_hsv(const unsigned char *rgb, fastf_t *hsv)
bu_color_format
Definition color.h:78
int bu_str_to_rgb(const char *str, unsigned char *rgb)
int bu_hsv_to_rgb(const fastf_t *hsv, unsigned char *rgb)
int bu_color_from_rgb_chars(struct bu_color *cp, const unsigned char *rgb)
bu_color_rand_t
Definition color.h:131
int bu_color_rand(struct bu_color *c, bu_color_rand_t type)
bu_color_space
Definition color.h:71
enum bu_color_space bu_color_space_t
int bu_color_to_rgb_chars(const struct bu_color *cp, unsigned char *rgb)
int bu_color_to_rgb_floats(const struct bu_color *cp, fastf_t *rgb)
int bu_color_parse(const char *str, struct bu_color *color)
@ BU_COLOR_FORMAT_NAME
Definition color.h:87
@ BU_COLOR_FORMAT_RGB
Definition color.h:79
@ BU_COLOR_FORMAT_HEX
Definition color.h:81
@ BU_COLOR_FORMAT_HSL
Definition color.h:83
@ BU_COLOR_FORMAT_HSLA
Definition color.h:84
@ BU_COLOR_FORMAT_RGBA
Definition color.h:80
@ BU_COLOR_FORMAT_HSVA
Definition color.h:86
@ BU_COLOR_FORMAT_HEXA
Definition color.h:82
@ BU_COLOR_FORMAT_HSV
Definition color.h:85
@ BU_COLOR_RANDOM
Definition color.h:132
@ BU_COLOR_RANDOM_LIGHTENED
Definition color.h:133
@ BU_COLOR_SPACE_HSV
Definition color.h:74
@ BU_COLOR_SPACE_HSL
Definition color.h:73
@ BU_COLOR_SPACE_RGB
Definition color.h:72
double fastf_t
fastest 64-bit (or larger) floating point type
Definition vmath.h:333
fastf_t hvect_t[ELEMENTS_PER_HVECT]
4-tuple vector
Definition vmath.h:360
Global registry of recognized magic numbers.
hvect_t buc_rgb
Definition color.h:65
Definition vls.h:53
fundamental vector, matrix, quaternion math macros