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
33/** @addtogroup bu_color
34 * @brief
35 * Support for storing and manipulating color data.
36 */
37/** @{ */
38/** @file bu/color.h */
39
40#define RED 0
41#define GRN 1
42#define BLU 2
43#define ALP 3
44
45#define BU_COLOR_INDEX_RED 0
46#define BU_COLOR_INDEX_GREEN 1
47#define BU_COLOR_INDEX_BLUE 2
48#define BU_COLOR_INDEX_ALPHA 3
49
50#define HUE 0
51#define SAT 1
52#define VAL 2
53
54#define BU_COLOR_INDEX_HUE 0
55#define BU_COLOR_INDEX_SATURATION 1
56#define BU_COLOR_INDEX_VALUE 2
57
58/**
59 * a single color value, stored as a 0.0 to 1.0 triplet for RGBA
60 */
62{
64};
65typedef struct bu_color bu_color_t;
66#define BU_COLOR_NULL ((struct bu_color *) 0)
67
68/**
69 * initializes a bu_color struct without allocating any memory.
70 */
71#define BU_COLOR_INIT(_c) { \
72 (_c)->buc_rgb[RED] = (_c)->buc_rgb[GRN] = (_c)->buc_rgb[BLU] = 0; (_c)->buc_rgb[ALP] = 0; \
73 }
74
75/**
76 * Check whether two colors are equal within a tolerance.
77 */
78#define BU_COLOR_NEAR_EQUAL(_c1, _c2, _tol) \
79 HNEAR_EQUAL(_c1.buc_rgb, _c2.buc_rgb, _tol)
80
81
82/**
83 * macro suitable for declaration statement initialization of a bu_color
84 * struct. does not allocate memory.
85 */
86#define BU_COLOR_INIT_ZERO {{0, 0, 0, 0}}
87
88/* Initializers for commonly used colors */
89#define BU_COLOR_BLUE {{0, 0, 1, 0}}
90#define BU_COLOR_CYAN {{0, 1, 1, 0}}
91#define BU_COLOR_GREEN {{0, 1, 0, 0}}
92#define BU_COLOR_PURPLE {{1, 0, 1, 0}}
93#define BU_COLOR_RED {{1, 0, 0, 0}}
94#define BU_COLOR_WHITE {{1, 1, 1, 0}}
95#define BU_COLOR_YELLOW {{1, 1, 0, 0}}
96
97/**
98 * Copy a bu_color
99 */
100#define BU_COLOR_CPY(_dest, _src) {\
101 (_dest)->buc_rgb[RED] = (_src)->buc_rgb[RED]; \
102 (_dest)->buc_rgb[GRN] = (_src)->buc_rgb[GRN]; \
103 (_dest)->buc_rgb[BLU] = (_src)->buc_rgb[BLU]; \
104 (_dest)->buc_rgb[ALP] = (_src)->buc_rgb[ALP]; \
105}
106
107
108/** random color generating methods */
113
114/**
115 * Function to generate random color
116 *
117 * Refactoring points:
118 * truly random color
119 * 3dm-g: src/libgcv/plugins/rhino/rhino_read.cpp
120 * "constrained" random
121 * BRLCADWrapper:getRandomColor(): src/conv/step/BRLCADWrapper.cpp
122
123 */
125
126#if 0
127
128/**
129 * Refactoring points:
130 * color command (set specified color)
131 * src/libged/color.c
132 * src/libged/brep.c
133 * get color from string
134 * src/libbu/color.c
135
136* Possible calling syntax:
137 @code
138 * // draw a purely random color in 0/0/0 to 255/255/255 range
139 * bn_color_samples(colors, NULL, COLOR_RANDOM, 1); // problematic in libbu, random is libbn domain
140 *
141 * // draw a golden ratio distribution random color in 0/0/0 to 255/255/255 range, s=0.5, v=0.95
142 * bn_color_samples(colors, NULL, COLOR_RANDOM_LIGHTENED, 1); // problematic in libbu, random is libbn domain
143 *
144 * // draw bezier interpolated and lightened samples
145 * struct bu_color range[4] = {0};
146 * bu_color_from_str(&range[0], "#0f0"); // green
147 * bu_color_from_str(&range[1], "0.f/0.f/1.f") // blue
148 * bu_color_from_str(&range[2], "purple");
149 * bn_color_samples(colors, range, COLOR_LINEAR, 10); // 10 dark colors from green to blue to purple
150 *
151 * // return a standard "heat map" with 18 quantized samples
152 * bn_color_samples(colors, NULL, COLOR_STANDARD_HEAT, 18);
153 @endcode
154 *
155 * Need:
156 * way to map from different color specifications to color including
157 * name: "red"
158 * rgbint: 255/0/0
159 * rgbfloat: 1.0f/0f/0f
160 * hexint: #FF0000
161 * hexshort: #F00
162 * hsv: 0/100%/100%
163 * hsl: 0/100%/50%
164 * ignoring YCbCr, YPbPr, YUV, YIQ, CMYK, CIE LAB
165 * ignoring grayscale specification
166 */
167/**
168 * Return a set of sampled colors given a range of zero or more colors
169 * (a NULL-terminated list of colors), a sample method, and desired
170 * number of color samples to return.
171 *
172 * Specifying no colors implies full spectrum. The default sampling
173 * method uses a golden ratio distribution to give a "balanced" random
174 * distribution that is effective with dark backgrounds and/or text.
175 *
176 * Returns the number of samples allocated.
177 */
178size_t bn_color_samples(struct bu_color **samples, const bu_color *colors, enum sampleMethod, size_t numSamples);
179#endif
180
181
182/**
183 * Convert between RGB and HSV color models
184 *
185 * R, G, and B are in {0, 1, ..., 255},
186 *
187 * H is in [0.0, 360.0), and S and V are in [0.0, 1.0],
188 *
189 * If S == 0.0, H is achromatic and set to 0.0
190 *
191 * These two routines are adapted from:
192 * pp. 592-3 of J.D. Foley, A. van Dam, S.K. Feiner, and J.F. Hughes,
193 * _Computer graphics: principles and practice_, 2nd ed., Addison-Wesley,
194 * Reading, MA, 1990.
195 */
196BU_EXPORT extern void bu_rgb_to_hsv(const unsigned char *rgb, fastf_t *hsv);
197BU_EXPORT extern int bu_hsv_to_rgb(const fastf_t *hsv, unsigned char *rgb);
198
199
200/**
201 * Utility functions to convert between various containers
202 * for color handling.
203 *
204 * FIXME: inconsistent input/output parameters!
205 * TODO: consider stdarg ... to consolidate all the _from_ functions, e.g.,
206 * // 3 colors
207 * bu_color_create(struct bu_color **colors, "red", "0/255/0", "#0000ff", NULL);
208 *
209 * // 2 colors from existing data
210 * struct bu_color *colors = NULL;
211 * bu_color_create(&colors, "%d/%d/%d", rgb[0], rgb[1], rgb[2], "hsv(%lf,0.5,0.95)", hsv, NULL);
212 * bu_color_destroy(colors);
213 */
214BU_EXPORT extern int bu_color_from_rgb_floats(struct bu_color *cp, const fastf_t *rgb);
215BU_EXPORT extern int bu_color_from_rgb_chars(struct bu_color *cp, const unsigned char *rgb);
216BU_EXPORT extern int bu_color_from_str(struct bu_color *cp, const char *str);
217/* UNIMPLEMENTED: BU_EXPORT extern int bu_color_from_hsv_floats(struct bu_color *cp, fastf_t *hsv); */
219BU_EXPORT extern int bu_str_to_rgb(const char *str, unsigned char *rgb); /* inconsistent, deprecate */
220
221BU_EXPORT extern int bu_color_to_rgb_floats(const struct bu_color *cp, fastf_t *rgb); /* bu_color_as_rgb_3fv */
222BU_EXPORT extern int bu_color_to_rgb_chars(const struct bu_color *cp, unsigned char *rgb); /* bu_color_as_rgb */
223BU_EXPORT extern int bu_color_to_rgb_ints(const struct bu_color *cp, int *r, int *g, int *b); /* bu_color_as_rgb_3i */
224/* UNIMPLEMENTED: BU_EXPORT extern int bu_color_to_hsv_floats(struct bu_color *cp, fastf_t *hsv); */ /* bu_color_as_hsv_3fv */
226
227/** @} */
228
230
231#endif /* BU_COLOR_H */
232
233/*
234 * Local Variables:
235 * mode: C
236 * tab-width: 8
237 * indent-tabs-mode: t
238 * c-file-style: "stroustrup"
239 * End:
240 * ex: shiftwidth=4 tabstop=8
241 */
Definition dvec.h:74
Header file for the BRL-CAD common definitions.
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)
void bu_rgb_to_hsv(const unsigned char *rgb, fastf_t *hsv)
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:109
int bu_color_rand(struct bu_color *c, bu_color_rand_t type)
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)
@ BU_COLOR_RANDOM
Definition color.h:110
@ BU_COLOR_RANDOM_LIGHTENED
Definition color.h:111
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:63
fundamental vector, matrix, quaternion math macros