GRASS GIS 8 Programmer's Manual  8.5.0dev(2025)-f22b1278f8
color_str.c
Go to the documentation of this file.
1 /*!
2  \file lib/gis/color_str.c
3 
4  \brief GIS library - color management, named color to RGB triplet
5 
6  (C) 2001-2016 by the GRASS Development Team
7 
8  This program is free software under the
9  GNU General Public License (>=v2).
10  Read the file COPYING that comes with GRASS
11  for details.
12 
13  \author Original author CERL
14  */
15 
16 #include <math.h>
17 #include <string.h>
18 
19 #include <grass/gis.h>
20 #include <grass/colors.h>
21 
22 /* The order in this table is important! It will be indexed by color number */
23 static const struct color_rgb standard_colors_rgb[] = {
24  {0, 0, 0}, /* This is a dummy value to make lookup easier */
25  {0, 0, 0}, /* BLACK */
26  {255, 0, 0}, /* RED */
27  {0, 255, 0}, /* GREEN */
28  {0, 0, 255}, /* BLUE */
29  {255, 255, 0}, /* YELLOW */
30  {0, 255, 255}, /* CYAN */
31  {255, 0, 255}, /* MAGENTA */
32  {255, 255, 255}, /* WHITE */
33  {128, 128, 128}, /* GRAY */
34  {255, 128, 0}, /* ORANGE */
35  {100, 128, 255}, /* AQUA */
36  {0, 128, 255}, /* INDIGO */
37  {128, 0, 255}, /* VIOLET */
38  {180, 77, 25} /* BROWN */
39 };
40 
41 /* The order in this table has no meaning. */
42 static const struct color_name standard_color_names[] = {
43  {"black", BLACK}, {"red", RED}, {"green", GREEN},
44  {"blue", BLUE}, {"yellow", YELLOW}, {"cyan", CYAN},
45  {"magenta", MAGENTA}, {"white", WHITE}, {"grey", GREY},
46  {"gray", GRAY}, {"orange", ORANGE}, {"aqua", AQUA},
47  {"indigo", INDIGO}, {"violet", VIOLET}, {"purple", PURPLE},
48  {"brown", BROWN}};
49 
50 /*!
51  \brief Get number of named colors (RGB triplets)
52 
53  \return number of colors
54  */
56 {
57  return sizeof(standard_colors_rgb) / sizeof(standard_colors_rgb[0]);
58 }
59 
60 /*!
61  \brief Get RGB triplet of given color
62 
63  \param n color index
64  */
65 struct color_rgb G_standard_color_rgb(int n)
66 {
67  return standard_colors_rgb[n];
68 }
69 
70 /*!
71  \brief Get number of named colors (color names)
72 
73  \return number of colors
74  */
76 {
77  return sizeof(standard_color_names) / sizeof(standard_color_names[0]);
78 }
79 
80 /*!
81  \brief Get color name
82 
83  \param n color index
84  */
85 const struct color_name *G_standard_color_name(int n)
86 {
87  return &standard_color_names[n];
88 }
89 
90 /*!
91  \brief Parse color string and set red,green,blue
92 
93  \param str color string
94  \param[out] red red value
95  \param[out] grn green value
96  \param[out] blu blue value
97 
98  \return 1 OK
99  \return 2 NONE
100  \return 0 on error
101  */
102 int G_str_to_color(const char *str, int *red, int *grn, int *blu)
103 {
104  char buf[100];
105  int num_names = G_num_standard_color_names();
106  int i;
107 
108  strcpy(buf, str);
109  G_chop(buf);
110 
111  G_debug(3, "G_str_to_color(): str = '%s'", buf);
112 
113  if (G_strcasecmp(buf, "NONE") == 0)
114  return 2;
115 
116  if (sscanf(buf, "%d%*[,:; ]%d%*[,:; ]%d", red, grn, blu) == 3) {
117  if (*red < 0 || *red > 255 || *grn < 0 || *grn > 255 || *blu < 0 ||
118  *blu > 255)
119  return 0;
120 
121  return 1;
122  }
123 
124  unsigned int hex;
125 
126  if (sscanf(buf, "#%x", &hex) == 1) {
127  *red = (hex >> 16) & 0xFF;
128  *grn = (hex >> 8) & 0xFF;
129  *blu = hex & 0xFF;
130  if (*red < 0 || *red > 255 || *grn < 0 || *grn > 255 || *blu < 0 ||
131  *blu > 255)
132  return 0;
133 
134  return 1;
135  }
136 
137  /* Look for this color in the standard (preallocated) colors */
138  for (i = 0; i < num_names; i++) {
139  const struct color_name *name = &standard_color_names[i];
140 
141  if (G_strcasecmp(buf, name->name) == 0) {
142  struct color_rgb rgb = standard_colors_rgb[name->number];
143 
144  *red = (int)rgb.r;
145  *grn = (int)rgb.g;
146  *blu = (int)rgb.b;
147 
148  return 1;
149  }
150  }
151 
152  return 0;
153 }
154 
155 /*!
156  \brief Converts RGB color values to HSV format.
157 
158  \note This implementation is experimental and may be subject to change.
159 
160  \param r red component of the RGB color
161  \param g green component of the RGB color
162  \param b blue component of the RGB color
163  \param[out] h pointer to store the calculated hue
164  \param[out] s pointer to store the calculated saturation
165  \param[out] v pointer to store the calculated value
166  */
167 void G_rgb_to_hsv(int r, int g, int b, float *h, float *s, float *v)
168 {
169  float r_norm = (float)r / 255.0f;
170  float g_norm = (float)g / 255.0f;
171  float b_norm = (float)b / 255.0f;
172 
173  float cmax = MAX(r_norm, MAX(g_norm, b_norm));
174  float cmin = MIN(r_norm, MIN(g_norm, b_norm));
175  float diff = cmax - cmin;
176 
177  if (cmax == cmin) {
178  *h = 0;
179  }
180  else if (cmax == r_norm) {
181  *h = fmodf((60.0f * ((g_norm - b_norm) / diff) + 360.0f), 360.0f);
182  }
183  else if (cmax == g_norm) {
184  *h = fmodf((60.0f * ((b_norm - r_norm) / diff) + 120.0f), 360.0f);
185  }
186  else {
187  *h = fmodf((60.0f * ((r_norm - g_norm) / diff) + 240.0f), 360.0f);
188  }
189 
190  if (cmax == 0) {
191  *s = 0;
192  }
193  else {
194  *s = (diff / cmax) * 100.0f;
195  }
196 
197  *v = cmax * 100.0f;
198 }
void G_rgb_to_hsv(int r, int g, int b, float *h, float *s, float *v)
Converts RGB color values to HSV format.
Definition: color_str.c:167
int G_num_standard_colors(void)
Get number of named colors (RGB triplets)
Definition: color_str.c:55
int G_str_to_color(const char *str, int *red, int *grn, int *blu)
Parse color string and set red,green,blue.
Definition: color_str.c:102
int G_num_standard_color_names(void)
Get number of named colors (color names)
Definition: color_str.c:75
struct color_rgb G_standard_color_rgb(int n)
Get RGB triplet of given color.
Definition: color_str.c:65
const struct color_name * G_standard_color_name(int n)
Get color name.
Definition: color_str.c:85
#define AQUA
Definition: colors.h:20
#define PURPLE
Definition: colors.h:26
#define INDIGO
Definition: colors.h:21
#define MAGENTA
Definition: colors.h:16
#define BLUE
Definition: colors.h:13
#define BLACK
Definition: colors.h:10
#define WHITE
Definition: colors.h:17
#define RED
Definition: colors.h:11
#define VIOLET
Definition: colors.h:22
#define BROWN
Definition: colors.h:23
#define YELLOW
Definition: colors.h:14
#define ORANGE
Definition: colors.h:19
#define GREEN
Definition: colors.h:12
#define CYAN
Definition: colors.h:15
#define GREY
Definition: colors.h:25
#define GRAY
Definition: colors.h:18
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition: strings.c:47
char * G_chop(char *)
Chop leading and trailing white spaces.
Definition: strings.c:332
int G_debug(int, const char *,...) __attribute__((format(printf
#define MIN(a, b)
Definition: gis.h:154
#define MAX(a, b)
Definition: gis.h:149
float g
Definition: named_colr.c:7
const char * name
Definition: named_colr.c:6
#define strcpy
Definition: parson.c:62
double b
Definition: r_raster.c:39
double r
Definition: r_raster.c:39
unsigned char g
Definition: colors.h:38
unsigned char b
Definition: colors.h:38
unsigned char r
Definition: colors.h:38