00001
00016 #include <string.h>
00017
00018 #include <grass/gis.h>
00019 #include <grass/colors.h>
00020
00021
00022 static const struct color_rgb standard_colors_rgb[] = {
00023 {0, 0, 0},
00024 {0, 0, 0},
00025 {255, 0, 0},
00026 {0, 255, 0},
00027 {0, 0, 255},
00028 {255, 255, 0},
00029 {0, 255, 255},
00030 {255, 0, 255},
00031 {255, 255, 255},
00032 {128, 128, 128},
00033 {255, 128, 0},
00034 {100, 128, 255},
00035 {0, 128, 255},
00036 {128, 0, 255},
00037 {180, 77, 25}
00038 };
00039
00040
00041 static const struct color_name standard_color_names[] = {
00042 {"black", BLACK},
00043 {"red", RED},
00044 {"green", GREEN},
00045 {"blue", BLUE},
00046 {"yellow", YELLOW},
00047 {"cyan", CYAN},
00048 {"magenta", MAGENTA},
00049 {"white", WHITE},
00050 {"grey", GREY},
00051 {"gray", GRAY},
00052 {"orange", ORANGE},
00053 {"aqua", AQUA},
00054 {"indigo", INDIGO},
00055 {"violet", VIOLET},
00056 {"purple", PURPLE},
00057 {"brown", BROWN}
00058 };
00059
00065 int G_num_standard_colors(void)
00066 {
00067 return sizeof(standard_colors_rgb) / sizeof(standard_colors_rgb[0]);
00068 }
00069
00075 struct color_rgb G_standard_color_rgb(int n)
00076 {
00077 return standard_colors_rgb[n];
00078 }
00079
00085 int G_num_standard_color_names(void)
00086 {
00087 return sizeof(standard_color_names) / sizeof(standard_color_names[0]);
00088 }
00089
00095 const struct color_name *G_standard_color_name(int n)
00096 {
00097 return &standard_color_names[n];
00098 }
00099
00112 int G_str_to_color(const char *str, int *red, int *grn, int *blu)
00113 {
00114 char buf[100];
00115 int num_names = G_num_standard_color_names();
00116 int i;
00117
00118 G_strcpy(buf, str);
00119 G_chop(buf);
00120
00121 G_debug(3, "G_str_to_color(): str = '%s'", buf);
00122
00123 if (G_strcasecmp(buf, "NONE") == 0)
00124 return 2;
00125
00126 if (sscanf(buf, "%d%*[,:; ]%d%*[,:; ]%d", red, grn, blu) == 3) {
00127 if (*red < 0 || *red > 255 ||
00128 *grn < 0 || *grn > 255 || *blu < 0 || *blu > 255)
00129 return 0;
00130
00131 return 1;
00132 }
00133
00134
00135 for (i = 0; i < num_names; i++) {
00136 const struct color_name *name = &standard_color_names[i];
00137
00138 if (G_strcasecmp(buf, name->name) == 0) {
00139 struct color_rgb rgb = standard_colors_rgb[name->number];
00140
00141 *red = (int)rgb.r;
00142 *grn = (int)rgb.g;
00143 *blu = (int)rgb.b;
00144
00145 return 1;
00146 }
00147 }
00148
00149 return 0;
00150 }