GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-6596d49e63
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 <string.h>
17 
18 #include <grass/gis.h>
19 #include <grass/colors.h>
20 
21 /* The order in this table is important! It will be indexed by color number */
22 static const struct color_rgb standard_colors_rgb[] = {
23  {0, 0, 0}, /* This is a dummy value to make lookup easier */
24  {0, 0, 0}, /* BLACK */
25  {255, 0, 0}, /* RED */
26  {0, 255, 0}, /* GREEN */
27  {0, 0, 255}, /* BLUE */
28  {255, 255, 0}, /* YELLOW */
29  {0, 255, 255}, /* CYAN */
30  {255, 0, 255}, /* MAGENTA */
31  {255, 255, 255}, /* WHITE */
32  {128, 128, 128}, /* GRAY */
33  {255, 128, 0}, /* ORANGE */
34  {100, 128, 255}, /* AQUA */
35  {0, 128, 255}, /* INDIGO */
36  {128, 0, 255}, /* VIOLET */
37  {180, 77, 25} /* BROWN */
38 };
39 
40 /* The order in this table has no meaning. */
41 static const struct color_name standard_color_names[] = {
42  {"black", BLACK}, {"red", RED}, {"green", GREEN},
43  {"blue", BLUE}, {"yellow", YELLOW}, {"cyan", CYAN},
44  {"magenta", MAGENTA}, {"white", WHITE}, {"grey", GREY},
45  {"gray", GRAY}, {"orange", ORANGE}, {"aqua", AQUA},
46  {"indigo", INDIGO}, {"violet", VIOLET}, {"purple", PURPLE},
47  {"brown", BROWN}};
48 
49 /*!
50  \brief Get number of named colors (RGB triplets)
51 
52  \return number of colors
53  */
55 {
56  return sizeof(standard_colors_rgb) / sizeof(standard_colors_rgb[0]);
57 }
58 
59 /*!
60  \brief Get RGB triplet of given color
61 
62  \param n color index
63  */
64 struct color_rgb G_standard_color_rgb(int n)
65 {
66  return standard_colors_rgb[n];
67 }
68 
69 /*!
70  \brief Get number of named colors (color names)
71 
72  \return number of colors
73  */
75 {
76  return sizeof(standard_color_names) / sizeof(standard_color_names[0]);
77 }
78 
79 /*!
80  \brief Get color name
81 
82  \param n color index
83  */
84 const struct color_name *G_standard_color_name(int n)
85 {
86  return &standard_color_names[n];
87 }
88 
89 /*!
90  \brief Parse color string and set red,green,blue
91 
92  \param str color string
93  \param[out] red red value
94  \param[out] grn green value
95  \param[out] blu blue value
96 
97  \return 1 OK
98  \return 2 NONE
99  \return 0 on error
100  */
101 int G_str_to_color(const char *str, int *red, int *grn, int *blu)
102 {
103  char buf[100];
104  int num_names = G_num_standard_color_names();
105  int i;
106 
107  strcpy(buf, str);
108  G_chop(buf);
109 
110  G_debug(3, "G_str_to_color(): str = '%s'", buf);
111 
112  if (G_strcasecmp(buf, "NONE") == 0)
113  return 2;
114 
115  if (sscanf(buf, "%d%*[,:; ]%d%*[,:; ]%d", red, grn, blu) == 3) {
116  if (*red < 0 || *red > 255 || *grn < 0 || *grn > 255 || *blu < 0 ||
117  *blu > 255)
118  return 0;
119 
120  return 1;
121  }
122 
123  unsigned int hex;
124 
125  if (sscanf(buf, "#%x", &hex) == 1) {
126  *red = (hex >> 16) & 0xFF;
127  *grn = (hex >> 8) & 0xFF;
128  *blu = hex & 0xFF;
129  if (*red < 0 || *red > 255 || *grn < 0 || *grn > 255 || *blu < 0 ||
130  *blu > 255)
131  return 0;
132 
133  return 1;
134  }
135 
136  /* Look for this color in the standard (preallocated) colors */
137  for (i = 0; i < num_names; i++) {
138  const struct color_name *name = &standard_color_names[i];
139 
140  if (G_strcasecmp(buf, name->name) == 0) {
141  struct color_rgb rgb = standard_colors_rgb[name->number];
142 
143  *red = (int)rgb.r;
144  *grn = (int)rgb.g;
145  *blu = (int)rgb.b;
146 
147  return 1;
148  }
149  }
150 
151  return 0;
152 }
int G_num_standard_colors(void)
Get number of named colors (RGB triplets)
Definition: color_str.c:54
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:101
int G_num_standard_color_names(void)
Get number of named colors (color names)
Definition: color_str.c:74
struct color_rgb G_standard_color_rgb(int n)
Get RGB triplet of given color.
Definition: color_str.c:64
const struct color_name * G_standard_color_name(int n)
Get color name.
Definition: color_str.c:84
#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
const char * name
Definition: named_colr.c:6
#define strcpy
Definition: parson.c:62
unsigned char g
Definition: colors.h:38
unsigned char b
Definition: colors.h:38
unsigned char r
Definition: colors.h:38