Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r.category: Add color option #5011

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/grass/defs/colors.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ struct color_rgb G_standard_color_rgb(int);
int G_num_standard_color_names(void);
const struct color_name *G_standard_color_name(int);
int G_str_to_color(const char *, int *, int *, int *);
void G_rgb_to_hsv(int, int, int, float *, float *, float *);

#endif
46 changes: 46 additions & 0 deletions lib/gis/color_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
\author Original author CERL
*/

#include <math.h>
#include <string.h>

#include <grass/gis.h>
Expand Down Expand Up @@ -150,3 +151,48 @@ int G_str_to_color(const char *str, int *red, int *grn, int *blu)

return 0;
}

/*!
\brief Converts RGB color values to HSV format.

\note This implementation is experimental and may be subject to change.

\param r red component of the RGB color
\param g green component of the RGB color
\param b blue component of the RGB color
\param[out] h pointer to store the calculated hue
\param[out] s pointer to store the calculated saturation
\param[out] v pointer to store the calculated value
*/
void G_rgb_to_hsv(int r, int g, int b, float *h, float *s, float *v)
{
float r_norm = (float)r / 255.0f;
float g_norm = (float)g / 255.0f;
float b_norm = (float)b / 255.0f;

float cmax = MAX(r_norm, MAX(g_norm, b_norm));
float cmin = MIN(r_norm, MIN(g_norm, b_norm));
float diff = cmax - cmin;

if (cmax == cmin) {
*h = 0;
}
else if (cmax == r_norm) {
*h = fmodf((60.0f * ((g_norm - b_norm) / diff) + 360.0f), 360.0f);
}
else if (cmax == g_norm) {
*h = fmodf((60.0f * ((b_norm - r_norm) / diff) + 120.0f), 360.0f);
}
else {
*h = fmodf((60.0f * ((r_norm - g_norm) / diff) + 240.0f), 360.0f);
}

if (cmax == 0) {
*s = 0;
}
else {
*s = (diff / cmax) * 100.0f;
}

*v = cmax * 100.0f;
}
49 changes: 2 additions & 47 deletions lib/raster/json_color_out.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
* \author Nishant Bansal
*/

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <grass/gis.h>
#include <grass/colors.h>
#include <grass/gjson.h>
#include <grass/glocale.h>
#include <grass/raster.h>
Expand All @@ -35,51 +35,6 @@ static void close_file(FILE *fp)
fclose(fp);
}

/*!
\brief Converts RGB color values to HSV format.

\note This implementation is experimental and may be subject to change.

\param r red component of the RGB color
\param g green component of the RGB color
\param b blue component of the RGB color
\param[out] h pointer to store the calculated hue
\param[out] s pointer to store the calculated saturation
\param[out] v pointer to store the calculated value
*/
static void rgb_to_hsv(int r, int g, int b, float *h, float *s, float *v)
{
float r_norm = (float)r / 255.0f;
float g_norm = (float)g / 255.0f;
float b_norm = (float)b / 255.0f;

float cmax = MAX(r_norm, MAX(g_norm, b_norm));
float cmin = MIN(r_norm, MIN(g_norm, b_norm));
float diff = cmax - cmin;

if (cmax == cmin) {
*h = 0;
}
else if (cmax == r_norm) {
*h = fmodf((60.0f * ((g_norm - b_norm) / diff) + 360.0f), 360.0f);
}
else if (cmax == g_norm) {
*h = fmodf((60.0f * ((b_norm - r_norm) / diff) + 120.0f), 360.0f);
}
else {
*h = fmodf((60.0f * ((r_norm - g_norm) / diff) + 240.0f), 360.0f);
}

if (cmax == 0) {
*s = 0;
}
else {
*s = (diff / cmax) * 100.0f;
}

*v = cmax * 100.0f;
}

/*!
\brief Writes color entry in JSON in specified clr_frmt.

Expand Down Expand Up @@ -108,7 +63,7 @@ static void set_color(int r, int g, int b, ColorFormat clr_frmt,
break;

case HSV:
rgb_to_hsv(r, g, b, &h, &s, &v);
G_rgb_to_hsv(r, g, b, &h, &s, &v);
snprintf(color_string, sizeof(color_string), "hsv(%d, %d, %d)", (int)h,
(int)s, (int)v);
G_json_object_set_string(color_object, "color", color_string);
Expand Down
11 changes: 9 additions & 2 deletions raster/r.category/local_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,24 @@

#include <grass/parson.h>

#define COLOR_STRING_LENGTH 30

enum OutputFormat { PLAIN, JSON };
enum ColorOutput { NONE, RGB_OUTPUT, HEX_OUTPUT, TRIPLET_OUTPUT, HSV_OUTPUT };

/* cats.c */
int get_cats(const char *, const char *);
int next_cat(long *);

/* main.c */
void print_json(JSON_Value *);
int print_label(long, enum OutputFormat, JSON_Array *);
int print_d_label(double, enum OutputFormat, JSON_Array *);
int print_label(long, enum OutputFormat, JSON_Array *, enum ColorOutput,
struct Colors *);
int print_d_label(double, enum OutputFormat, JSON_Array *, enum ColorOutput,
struct Colors *);
int scan_cats(const char *, long *, long *);
int scan_vals(const char *, double *);
void scan_colors(const void *, struct Colors *, enum ColorOutput, char *,
RASTER_MAP_TYPE);

#endif /* __LOCAL_PROTO_H__ */
105 changes: 95 additions & 10 deletions raster/r.category/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <stdio.h>
#include <string.h>
#include <grass/gis.h>
#include <grass/colors.h>
#include <grass/raster.h>
#include <grass/glocale.h>
#include <grass/parson.h>
Expand All @@ -38,15 +39,17 @@ int main(int argc, char *argv[])
RASTER_MAP_TYPE map_type;
int i;
int from_stdin = FALSE;
struct Colors colors;
struct GModule *module;

enum OutputFormat format;
enum ColorOutput color_format;
JSON_Value *root_value;
JSON_Array *root_array;

struct {
struct Option *map, *fs, *cats, *vals, *raster, *file, *fmt_str,
*fmt_coeff, *format;
*fmt_coeff, *format, *color;
} parm;

G_gisinit(argv[0]);
Expand Down Expand Up @@ -112,6 +115,12 @@ int main(int argc, char *argv[])
parm.format->key = "output_format";
parm.format->guisection = _("Print");

parm.color = G_define_standard_option(G_OPT_C_FORMAT);
parm.color->required = NO;
parm.color->options = "none,rgb,hex,triplet,hsv";
parm.color->answer = "none";
parm.color->description = _("Color format for output values or none.");

if (G_parser(argc, argv))
exit(EXIT_FAILURE);

Expand All @@ -127,6 +136,22 @@ int main(int argc, char *argv[])
format = PLAIN;
}

if (strcmp(parm.color->answer, "rgb") == 0) {
color_format = RGB_OUTPUT;
}
else if (strcmp(parm.color->answer, "triplet") == 0) {
color_format = TRIPLET_OUTPUT;
}
else if (strcmp(parm.color->answer, "hex") == 0) {
color_format = HEX_OUTPUT;
}
else if (strcmp(parm.color->answer, "hsv") == 0) {
color_format = HSV_OUTPUT;
}
else {
color_format = NONE;
}

name = parm.map->answer;

fs = G_option_to_separator(parm.fs);
Expand Down Expand Up @@ -297,13 +322,17 @@ int main(int argc, char *argv[])
name, mapset);
}

if (color_format != NONE)
if (Rast_read_colors(name, mapset, &colors) < 0)
G_fatal_error(_("Unable to read colors for input map %s"), name);

/* describe the category labels */
/* if no cats requested, use r.describe to get the cats */
if (parm.cats->answer == NULL) {
if (map_type == CELL_TYPE) {
get_cats(name, mapset);
while (next_cat(&x))
print_label(x, format, root_array);
print_label(x, format, root_array, color_format, &colors);
if (format == JSON) {
print_json(root_value);
}
Expand All @@ -324,7 +353,7 @@ int main(int argc, char *argv[])
for (i = 0; parm.cats->answers[i]; i++) {
scan_cats(parm.cats->answers[i], &x, &y);
while (x <= y)
print_label(x++, format, root_array);
print_label(x++, format, root_array, color_format, &colors);
}
if (format == JSON) {
print_json(root_value);
Expand All @@ -342,7 +371,7 @@ int main(int argc, char *argv[])
}
for (i = 0; parm.vals->answers[i]; i++) {
scan_vals(parm.vals->answers[i], &dx);
print_d_label(dx, format, root_array);
print_d_label(dx, format, root_array, color_format, &colors);
}

if (format == JSON) {
Expand All @@ -364,33 +393,44 @@ void print_json(JSON_Value *root_value)
json_value_free(root_value);
}

int print_label(long x, enum OutputFormat format, JSON_Array *root_array)
int print_label(long x, enum OutputFormat format, JSON_Array *root_array,
enum ColorOutput color_format, struct Colors *colors)
{
char *label;
char *label, color[COLOR_STRING_LENGTH];
JSON_Value *category_value;
JSON_Object *category;

G_squeeze(label = Rast_get_c_cat((CELL *)&x, &cats));

switch (format) {
case PLAIN:
fprintf(stdout, "%ld%s%s\n", x, fs, label);
fprintf(stdout, "%ld%s%s", x, fs, label);
if (color_format != NONE) {
scan_colors((CELL *)&x, colors, color_format, color, CELL_TYPE);
fprintf(stdout, "%s%s", fs, color);
}
fprintf(stdout, "\n");
break;
case JSON:
category_value = json_value_init_object();
category = json_object(category_value);
json_object_set_number(category, "category", x);
json_object_set_string(category, "description", label);
if (color_format != NONE) {
scan_colors((CELL *)&x, colors, color_format, color, CELL_TYPE);
json_object_set_string(category, "color", color);
}
json_array_append_value(root_array, category_value);
break;
}

return 0;
}

int print_d_label(double x, enum OutputFormat format, JSON_Array *root_array)
int print_d_label(double x, enum OutputFormat format, JSON_Array *root_array,
enum ColorOutput color_format, struct Colors *colors)
{
char *label, tmp[40];
char *label, tmp[40], color[COLOR_STRING_LENGTH];
DCELL dtmp;
JSON_Value *category_value;
JSON_Object *category;
Expand All @@ -402,13 +442,22 @@ int print_d_label(double x, enum OutputFormat format, JSON_Array *root_array)
case PLAIN:
sprintf(tmp, "%.10f", x);
G_trim_decimal(tmp);
fprintf(stdout, "%s%s%s\n", tmp, fs, label);
fprintf(stdout, "%s%s%s", tmp, fs, label);
if (color_format != NONE) {
scan_colors((DCELL *)&x, colors, color_format, color, DCELL_TYPE);
fprintf(stdout, "%s%s", fs, color);
}
fprintf(stdout, "\n");
break;
case JSON:
category_value = json_value_init_object();
category = json_object(category_value);
json_object_set_number(category, "category", x);
json_object_set_string(category, "description", label);
if (color_format != NONE) {
scan_colors((DCELL *)&x, colors, color_format, color, DCELL_TYPE);
json_object_set_string(category, "color", color);
}
json_array_append_value(root_array, category_value);
break;
}
Expand Down Expand Up @@ -440,3 +489,39 @@ int scan_vals(const char *s, double *x)
return 1;
return 0;
}

void scan_colors(const void *x, struct Colors *colors,
enum ColorOutput color_format, char *color,
RASTER_MAP_TYPE map_type)
{
int red, grn, blu;
float h, s, v;

if (!Rast_get_color(x, &red, &grn, &blu, colors, map_type)) {
strcpy(color, "*");
return;
}

switch (color_format) {
case RGB_OUTPUT:
snprintf(color, COLOR_STRING_LENGTH, "rgb(%d, %d, %d)", red, grn, blu);
break;

case HEX_OUTPUT:
snprintf(color, COLOR_STRING_LENGTH, "#%02X%02X%02X", red, grn, blu);
break;

case TRIPLET_OUTPUT:
snprintf(color, COLOR_STRING_LENGTH, "%d:%d:%d", red, grn, blu);
break;

case HSV_OUTPUT:
G_rgb_to_hsv(red, grn, blu, &h, &s, &v);
snprintf(color, COLOR_STRING_LENGTH, "hsv(%d, %d, %d)", (int)h, (int)s,
(int)v);
break;

case NONE:
break;
}
}
Loading
Loading