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