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