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 4 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/raster.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ void Rast__organize_colors(struct Colors *);
void Rast_print_colors(struct Colors *, DCELL, DCELL, FILE *, int);

/* json_color_out.c */
void Rast_rgb_to_hsv(int, int, int, float *, float *, float *);
void Rast_print_json_colors(struct Colors *, DCELL, DCELL, FILE *, int,
ColorFormat);

Expand Down
4 changes: 2 additions & 2 deletions lib/raster/json_color_out.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static void close_file(FILE *fp)
\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)
void Rast_rgb_to_hsv(int r, int g, int b, float *h, float *s, float *v)
NishantBansal2003 marked this conversation as resolved.
Show resolved Hide resolved
{
float r_norm = (float)r / 255.0f;
float g_norm = (float)g / 255.0f;
Expand Down Expand Up @@ -108,7 +108,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);
Rast_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__ */
104 changes: 94 additions & 10 deletions raster/r.category/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,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 +114,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 +135,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 +321,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);
NishantBansal2003 marked this conversation as resolved.
Show resolved Hide resolved

/* 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 +352,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 +370,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 +392,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 +441,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 +488,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:
Rast_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;
}
}
27 changes: 27 additions & 0 deletions raster/r.category/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Fixture for r.category test"""

import os
import pytest
import grass.script as gs


@pytest.fixture
def simple_dataset(tmp_path):
"""Set up a GRASS session and create test rasters with color rules."""
project = tmp_path / "raster_color_project"
gs.create_project(project)
with gs.setup.init(project, env=os.environ.copy()) as session:
gs.run_command(
"g.region",
s=0,
n=100,
w=0,
e=100,
res=10,
env=session.env,
)
gs.mapcalc("test = if(col() < 3, col(), 2)", env=session.env)
gs.mapcalc("test_1 = if(col() < 5, col(), 4)", env=session.env)
gs.mapcalc("test_d = if(col() < 5, col() / 2., 4.5)", env=session.env)

yield session
Loading