21 #include <grass/gjson.h>
25 #define COLOR_STRING_LENGTH 30
32 static void close_file(FILE *fp)
50 static void rgb_to_hsv(
int r,
int g,
int b,
float *h,
float *s,
float *v)
52 float r_norm = (float)
r / 255.0f;
53 float g_norm = (float)
g / 255.0f;
54 float b_norm = (float)
b / 255.0f;
56 float cmax =
MAX(r_norm,
MAX(g_norm, b_norm));
57 float cmin =
MIN(r_norm,
MIN(g_norm, b_norm));
58 float diff = cmax - cmin;
63 else if (cmax == r_norm) {
64 *h = fmodf((60.0f * ((g_norm - b_norm) / diff) + 360.0f), 360.0f);
66 else if (cmax == g_norm) {
67 *h = fmodf((60.0f * ((b_norm - r_norm) / diff) + 120.0f), 360.0f);
70 *h = fmodf((60.0f * ((r_norm - g_norm) / diff) + 240.0f), 360.0f);
77 *s = (diff / cmax) * 100.0f;
100 snprintf(color_string,
sizeof(color_string),
"rgb(%d, %d, %d)",
r,
g,
106 snprintf(color_string,
sizeof(color_string),
"#%02X%02X%02X",
r,
g,
b);
111 rgb_to_hsv(
r,
g,
b, &h, &s, &v);
112 snprintf(color_string,
sizeof(color_string),
"hsv(%d, %d, %d)", (
int)h,
118 snprintf(color_string,
sizeof(color_string),
"%d:%d:%d",
r,
g,
b);
145 static int r0 = -1, g0 = -1, b0 = -1;
148 if (v0 == *val && r0 ==
r && g0 ==
g && b0 ==
b)
151 v0 = *val, r0 =
r, g0 =
g, b0 =
b;
154 if (color_value ==
NULL) {
157 G_fatal_error(
_(
"Failed to initialize JSON object. Out of memory?"));
168 set_color(
r,
g,
b, clr_frmt, color_object);
187 if (root_value ==
NULL) {
189 G_fatal_error(
_(
"Failed to initialize JSON array. Out of memory?"));
200 for (
int i = lo; i <= hi; i++) {
201 unsigned char r,
g,
b, set;
206 write_json_rule(&val, &
min, &
max,
r,
g,
b, root_array, perc,
207 clr_frmt, fp, root_value);
214 for (
int i = 0; i <
count; i++) {
216 unsigned char r1, g1, b1, r2, g2, b2;
220 colors,
count - 1 - i);
223 write_json_rule(&val1, &
min, &
max, r1, g1, b1, root_array, perc,
224 clr_frmt, fp, root_value);
225 write_json_rule(&val2, &
min, &
max, r2, g2, b2, root_array, perc,
226 clr_frmt, fp, root_value);
237 if (nv_value ==
NULL) {
241 _(
"Failed to initialize JSON object. Out of memory?"));
245 set_color(
r,
g,
b, clr_frmt, nv_object);
251 if (default_value ==
NULL) {
255 _(
"Failed to initialize JSON object. Out of memory?"));
259 set_color(
r,
g,
b, clr_frmt, default_object);
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int Rast_colors_count(const struct Colors *)
Get both modular and fixed rules count.
void Rast_get_c_color_range(CELL *, CELL *, const struct Colors *)
Get color range values (CELL)
int Rast_get_fp_color_rule(DCELL *, unsigned char *, unsigned char *, unsigned char *, DCELL *, unsigned char *, unsigned char *, unsigned char *, const struct Colors *, int)
Get color rule from both modular and fixed rules.
void Rast_get_null_value_color(int *, int *, int *, const struct Colors *)
Gets color for null value.
void Rast_lookup_c_colors(const CELL *, unsigned char *, unsigned char *, unsigned char *, unsigned char *, int, struct Colors *)
Lookup an array of colors.
void Rast_get_default_color(int *, int *, int *, const struct Colors *)
Gets default color.
char * G_json_serialize_to_string_pretty(const JSON_Value *value)
void G_json_value_free(JSON_Value *value)
void G_json_free_serialized_string(char *string)
JSON_Status G_json_object_set_number(JSON_Object *object, const char *name, double number)
JSON_Array * G_json_array(const JSON_Value *value)
JSON_Value * G_json_value_init_object(void)
JSON_Value * G_json_value_init_array(void)
JSON_Status G_json_object_set_string(JSON_Object *object, const char *name, const char *string)
JSON_Status G_json_array_append_value(JSON_Array *array, JSON_Value *value)
JSON_Object * G_json_object(const JSON_Value *value)
void Rast_print_json_colors(struct Colors *colors, DCELL min, DCELL max, FILE *fp, int perc, ColorFormat clr_frmt)
Print color table in JSON format.
#define COLOR_STRING_LENGTH
const char * json_string(const JSON_Value *value)
struct json_array_t JSON_Array
struct json_value_t JSON_Value
struct json_object_t JSON_Object
ColorFormat
Color format identifiers (enum)