19 #include <grass/gis.h>
20 #include <grass/glocale.h>
21 #include <grass/colors.h>
24 static const struct color_rgb standard_colors_rgb[] = {
43 static const struct color_name standard_color_names[] = {
44 {
"black", BLACK}, {
"red", RED}, {
"green", GREEN},
45 {
"blue", BLUE}, {
"yellow", YELLOW}, {
"cyan", CYAN},
46 {
"magenta", MAGENTA}, {
"white", WHITE}, {
"grey", GREY},
47 {
"gray", GRAY}, {
"orange", ORANGE}, {
"aqua", AQUA},
48 {
"indigo", INDIGO}, {
"violet", VIOLET}, {
"purple", PURPLE},
58 return sizeof(standard_colors_rgb) /
sizeof(standard_colors_rgb[0]);
68 return standard_colors_rgb[n];
78 return sizeof(standard_color_names) /
sizeof(standard_color_names[0]);
88 return &standard_color_names[n];
112 G_debug(3,
"G_str_to_color(): str = '%s'", buf);
117 if (sscanf(buf,
"%d%*[,:; ]%d%*[,:; ]%d", red, grn, blu) == 3) {
118 if (*red < 0 || *red > 255 || *grn < 0 || *grn > 255 || *blu < 0 ||
127 if (sscanf(buf,
"#%x", &hex) == 1) {
128 *red = (hex >> 16) & 0xFF;
129 *grn = (hex >> 8) & 0xFF;
131 if (*red < 0 || *red > 255 || *grn < 0 || *grn > 255 || *blu < 0 ||
139 for (i = 0; i < num_names; i++) {
140 const struct color_name *
name = &standard_color_names[i];
143 struct color_rgb rgb = standard_colors_rgb[
name->number];
170 float r_norm = (float)
r / 255.0f;
171 float g_norm = (float)
g / 255.0f;
172 float b_norm = (float)
b / 255.0f;
174 float cmax =
MAX(r_norm,
MAX(g_norm, b_norm));
175 float cmin =
MIN(r_norm,
MIN(g_norm, b_norm));
176 float diff = cmax - cmin;
181 else if (cmax == r_norm) {
182 *h = fmodf((60.0f * ((g_norm - b_norm) / diff) + 360.0f), 360.0f);
184 else if (cmax == g_norm) {
185 *h = fmodf((60.0f * ((b_norm - r_norm) / diff) + 120.0f), 360.0f);
188 *h = fmodf((60.0f * ((r_norm - g_norm) / diff) + 240.0f), 360.0f);
195 *s = (diff / cmax) * 100.0f;
216 snprintf(str, COLOR_STRING_LENGTH,
"rgb(%d, %d, %d)",
r,
g,
b);
220 snprintf(str, COLOR_STRING_LENGTH,
"#%02X%02X%02X",
r,
g,
b);
225 snprintf(str, COLOR_STRING_LENGTH,
"hsv(%d, %d, %d)", (
int)h, (
int)s,
230 snprintf(str, COLOR_STRING_LENGTH,
"%d:%d:%d",
r,
g,
b);
256 if (strcmp(option->answer,
"rgb") == 0) {
259 if (strcmp(option->answer,
"triplet") == 0) {
262 if (strcmp(option->answer,
"hsv") == 0) {
265 if (strcmp(option->answer,
"hex") == 0) {
269 G_fatal_error(_(
"Unknown color format '%s'"), option->answer);
ColorFormat G_option_to_color_format(const struct Option *option)
Get color format from the option.
void G_rgb_to_hsv(int r, int g, int b, float *h, float *s, float *v)
Converts RGB color values to HSV format.
int G_num_standard_colors(void)
Get number of named colors (RGB triplets)
void G_color_to_str(int r, int g, int b, ColorFormat clr_frmt, char *str)
Parse red,green,blue and set color string.
int G_str_to_color(const char *str, int *red, int *grn, int *blu)
Parse color string and set red,green,blue.
int G_num_standard_color_names(void)
Get number of named colors (color names)
struct color_rgb G_standard_color_rgb(int n)
Get RGB triplet of given color.
const struct color_name * G_standard_color_name(int n)
Get color name.
int G_debug(int level, const char *msg,...)
Print debugging message.
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
char * G_chop(char *line)
Chop leading and trailing white spaces.
size_t G_strlcpy(char *dst, const char *src, size_t dsize)
Safe string copy function.