From c16a63a125468b9a93912f94a099270fca2a78f8 Mon Sep 17 00:00:00 2001 From: Nishant Bansal Date: Sat, 2 Nov 2024 20:25:36 +0530 Subject: [PATCH] added changes based on review Signed-off-by: Nishant Bansal --- include/grass/gis.h | 2 +- lib/gis/parser_standard_options.c | 9 ++-- raster/r.colors.out/local_proto.h | 2 +- raster/r.colors.out/prt_json.c | 50 ++++++++----------- raster/r.colors.out/raster3d_main.c | 6 +-- raster/r.colors.out/raster_main.c | 6 +-- .../r.colors.out/tests/r3_colors_out_test.py | 38 +++++++------- .../r.colors.out/tests/r_colors_out_test.py | 38 +++++++------- 8 files changed, 73 insertions(+), 78 deletions(-) diff --git a/include/grass/gis.h b/include/grass/gis.h index 48b792c2ae4..fcc53535f47 100644 --- a/include/grass/gis.h +++ b/include/grass/gis.h @@ -318,7 +318,7 @@ typedef enum { G_OPT_C, /*!< color */ G_OPT_CN, /*!< color or none */ - G_OPT_C_FORMAT, /*!< set color format to rgb,hex,hsv or xterm */ + G_OPT_C_FORMAT, /*!< set color format to rgb,hex,hsv or triplet */ G_OPT_M_UNITS, /*!< units */ G_OPT_M_DATATYPE, /*!< datatype */ diff --git a/lib/gis/parser_standard_options.c b/lib/gis/parser_standard_options.c index f4464f4d06c..d708fd6d0b7 100644 --- a/lib/gis/parser_standard_options.c +++ b/lib/gis/parser_standard_options.c @@ -658,14 +658,15 @@ struct Option *G_define_standard_option(int opt) Opt->key_desc = "name"; Opt->required = YES; Opt->multiple = NO; - Opt->answer = "xterm"; - Opt->options = "rgb,hex,hsv,xterm"; + Opt->answer = "hex"; + Opt->options = "rgb,hex,hsv,triplet"; Opt->label = _("Color format"); Opt->description = _("Color format for output values."); G_asprintf( - (char **)&(Opt->descriptions), "rgb;%s;hex;%s;hsv;%s;xterm;%s", + (char **)&(Opt->descriptions), "rgb;%s;hex;%s;hsv;%s;triplet;%s", _("output color in RGB format"), _("output color in HEX format"), - _("output color in HSV format"), _("output color in XTERM format")); + _("output color in HSV format"), + _("output color in colon-separated RGB format")); break; /* misc */ diff --git a/raster/r.colors.out/local_proto.h b/raster/r.colors.out/local_proto.h index cf3492cbb43..5ed09a06e69 100644 --- a/raster/r.colors.out/local_proto.h +++ b/raster/r.colors.out/local_proto.h @@ -1,7 +1,7 @@ #include #include -enum ColorFormat { RGB, HEX, HSV, XTERM }; +enum ColorFormat { RGB, HEX, HSV, TRIPLET }; void print_json_colors(struct Colors *colors, DCELL min, DCELL max, FILE *fp, int perc, enum ColorFormat clr_frmt); diff --git a/raster/r.colors.out/prt_json.c b/raster/r.colors.out/prt_json.c index 223ea602843..816a63e1b0e 100644 --- a/raster/r.colors.out/prt_json.c +++ b/raster/r.colors.out/prt_json.c @@ -10,9 +10,7 @@ #include "local_proto.h" -#define RGB_STRING_LENGTH 20 -#define HEX_STRING_LENGTH 8 -#define HSV_STRING_LENGTH 30 +#define COLOR_STRING_LENGTH 30 /*! \brief Closes the file if it is not stdout. @@ -28,6 +26,8 @@ static void close_file(FILE *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 @@ -74,45 +74,39 @@ static void rgb_to_hsv(int r, int g, int b, float *h, float *s, float *v) \param r red component of RGB color \param g green component of RGB color \param b blue component of RGB color - \param clr_frmt color format to be used (RGB, HEX, HSV, XTERM). + \param clr_frmt color format to be used (RGB, HEX, HSV, TRIPLET). \param color_object pointer to the JSON object */ static void set_color(int r, int g, int b, enum ColorFormat clr_frmt, JSON_Object *color_object) { + char color_string[COLOR_STRING_LENGTH]; + float h, s, v; + switch (clr_frmt) { - case RGB: { - char rgb_string[RGB_STRING_LENGTH]; - snprintf(rgb_string, sizeof(rgb_string), "rgb(%d, %d, %d)", r, g, b); - json_object_set_string(color_object, "rgb", rgb_string); + case RGB: + snprintf(color_string, sizeof(color_string), "rgb(%d, %d, %d)", r, g, + b); + json_object_set_string(color_object, "rgb", color_string); break; - } - case HEX: { - char hex_string[HEX_STRING_LENGTH]; - snprintf(hex_string, sizeof(hex_string), "#%02X%02X%02X", r, g, b); - json_object_set_string(color_object, "hex", hex_string); + case HEX: + snprintf(color_string, sizeof(color_string), "#%02X%02X%02X", r, g, b); + json_object_set_string(color_object, "hex", color_string); break; - } - case HSV: { - float h, s, v; + case HSV: rgb_to_hsv(r, g, b, &h, &s, &v); - char hsv_string[HSV_STRING_LENGTH]; - snprintf(hsv_string, sizeof(hsv_string), "hsv(%d, %d, %d)", (int)h, + snprintf(color_string, sizeof(color_string), "hsv(%d, %d, %d)", (int)h, (int)s, (int)v); - json_object_set_string(color_object, "hsv", hsv_string); + json_object_set_string(color_object, "hsv", color_string); break; - } - case XTERM: { - char default_rgb_string[RGB_STRING_LENGTH]; - snprintf(default_rgb_string, sizeof(default_rgb_string), "%d:%d:%d", r, - g, b); - json_object_set_string(color_object, "RGB", default_rgb_string); + case TRIPLET: + snprintf(color_string, sizeof(color_string), "%d:%d:%d", r, g, b); + json_object_set_string(color_object, "triplet", color_string); break; } - } } /*! @@ -126,7 +120,7 @@ static void set_color(int r, int g, int b, enum ColorFormat clr_frmt, \param b blue component of RGB color \param root_array pointer to the JSON array \param perc TRUE for percentage output - \param clr_frmt color format to be used (RBG, HEX, HSV, XTERM). + \param clr_frmt color format to be used (RBG, HEX, HSV, TRIPLET). \param fp file where to print color table rules \param root_value pointer to json value */ @@ -172,7 +166,7 @@ static void write_json_rule(DCELL *val, DCELL *min, DCELL *max, int r, int g, when \p perc is non-zero) \param fp file where to print color table rules \param perc TRUE for percentage output - \param clr_frmt color format to be used (RBG, HEX, HSV, XTERM). + \param clr_frmt color format to be used (RBG, HEX, HSV, TRIPLET). */ void print_json_colors(struct Colors *colors, DCELL min, DCELL max, FILE *fp, int perc, enum ColorFormat clr_frmt) diff --git a/raster/r.colors.out/raster3d_main.c b/raster/r.colors.out/raster3d_main.c index 9744941aaa7..904db24a91f 100644 --- a/raster/r.colors.out/raster3d_main.c +++ b/raster/r.colors.out/raster3d_main.c @@ -93,14 +93,14 @@ int main(int argc, char **argv) if (strcmp(opt.color_format->answer, "rgb") == 0) { clr_frmt = RGB; } - else if (strcmp(opt.color_format->answer, "hex") == 0) { - clr_frmt = HEX; + else if (strcmp(opt.color_format->answer, "triplet") == 0) { + clr_frmt = TRIPLET; } else if (strcmp(opt.color_format->answer, "hsv") == 0) { clr_frmt = HSV; } else { - clr_frmt = XTERM; + clr_frmt = HEX; } print_json_colors(&colors, range.min, range.max, fp, flag.p->answer ? 1 : 0, clr_frmt); diff --git a/raster/r.colors.out/raster_main.c b/raster/r.colors.out/raster_main.c index 505a82ba578..6b4a8ed36ee 100644 --- a/raster/r.colors.out/raster_main.c +++ b/raster/r.colors.out/raster_main.c @@ -92,14 +92,14 @@ int main(int argc, char **argv) if (strcmp(opt.color_format->answer, "rgb") == 0) { clr_frmt = RGB; } - else if (strcmp(opt.color_format->answer, "hex") == 0) { - clr_frmt = HEX; + else if (strcmp(opt.color_format->answer, "triplet") == 0) { + clr_frmt = TRIPLET; } else if (strcmp(opt.color_format->answer, "hsv") == 0) { clr_frmt = HSV; } else { - clr_frmt = XTERM; + clr_frmt = HEX; } print_json_colors(&colors, range.min, range.max, fp, flag.p->answer ? 1 : 0, clr_frmt); diff --git a/raster/r.colors.out/tests/r3_colors_out_test.py b/raster/r.colors.out/tests/r3_colors_out_test.py index 4827fb2ea05..d6d7505aade 100644 --- a/raster/r.colors.out/tests/r3_colors_out_test.py +++ b/raster/r.colors.out/tests/r3_colors_out_test.py @@ -71,34 +71,34 @@ def test_r3_colors_out_json_with_default_option(raster3_color_dataset): data = gs.parse_command("r3.colors.out", map="b", format="json", env=session.env) validate_common_json_structure(data) expected = [ - {"value": 1, "RGB": "0:191:191"}, - {"value": 1.8, "RGB": "0:255:0"}, - {"value": 2.6, "RGB": "255:255:0"}, - {"value": 3.4, "RGB": "255:127:0"}, - {"value": 4.2, "RGB": "191:127:63"}, - {"value": 5, "RGB": "200:200:200"}, - {"value": "nv", "RGB": "255:255:255"}, - {"value": "default", "RGB": "255:255:255"}, + {"value": 1, "hex": "#00BFBF"}, + {"value": 1.8, "hex": "#00FF00"}, + {"value": 2.6, "hex": "#FFFF00"}, + {"value": 3.4, "hex": "#FF7F00"}, + {"value": 4.2, "hex": "#BF7F3F"}, + {"value": 5, "hex": "#C8C8C8"}, + {"value": "nv", "hex": "#FFFFFF"}, + {"value": "default", "hex": "#FFFFFF"}, ] assert expected == data, f"test failed: expected {expected} but got {data}" -def test_r3_colors_out_json_with_xterm_option(raster3_color_dataset): - """Test r3.colors.out command for JSON output format for xterm color option.""" +def test_r3_colors_out_json_with_crgb_option(raster3_color_dataset): + """Test r3.colors.out command for JSON output format for triplet color option.""" session = raster3_color_dataset data = gs.parse_command( - "r3.colors.out", map="b", format="json", color_format="xterm", env=session.env + "r3.colors.out", map="b", format="json", color_format="triplet", env=session.env ) validate_common_json_structure(data) expected = [ - {"value": 1, "RGB": "0:191:191"}, - {"value": 1.8, "RGB": "0:255:0"}, - {"value": 2.6, "RGB": "255:255:0"}, - {"value": 3.4, "RGB": "255:127:0"}, - {"value": 4.2, "RGB": "191:127:63"}, - {"value": 5, "RGB": "200:200:200"}, - {"value": "nv", "RGB": "255:255:255"}, - {"value": "default", "RGB": "255:255:255"}, + {"value": 1, "triplet": "0:191:191"}, + {"value": 1.8, "triplet": "0:255:0"}, + {"value": 2.6, "triplet": "255:255:0"}, + {"value": 3.4, "triplet": "255:127:0"}, + {"value": 4.2, "triplet": "191:127:63"}, + {"value": 5, "triplet": "200:200:200"}, + {"value": "nv", "triplet": "255:255:255"}, + {"value": "default", "triplet": "255:255:255"}, ] assert expected == data, f"test failed: expected {expected} but got {data}" diff --git a/raster/r.colors.out/tests/r_colors_out_test.py b/raster/r.colors.out/tests/r_colors_out_test.py index 9bf48d13996..be88083308c 100644 --- a/raster/r.colors.out/tests/r_colors_out_test.py +++ b/raster/r.colors.out/tests/r_colors_out_test.py @@ -71,34 +71,34 @@ def test_r_colors_out_json_with_default_option(raster_color_dataset): data = gs.parse_command("r.colors.out", map="a", format="json", env=session.env) validate_common_json_structure(data) expected = [ - {"value": 1, "RGB": "0:191:191"}, - {"value": 1.4, "RGB": "0:255:0"}, - {"value": 1.8, "RGB": "255:255:0"}, - {"value": 2.2, "RGB": "255:127:0"}, - {"value": 2.6, "RGB": "191:127:63"}, - {"value": 3, "RGB": "200:200:200"}, - {"value": "nv", "RGB": "255:255:255"}, - {"value": "default", "RGB": "255:255:255"}, + {"value": 1, "hex": "#00BFBF"}, + {"value": 1.4, "hex": "#00FF00"}, + {"value": 1.8, "hex": "#FFFF00"}, + {"value": 2.2, "hex": "#FF7F00"}, + {"value": 2.6, "hex": "#BF7F3F"}, + {"value": 3, "hex": "#C8C8C8"}, + {"value": "nv", "hex": "#FFFFFF"}, + {"value": "default", "hex": "#FFFFFF"}, ] assert expected == data, f"test failed: expected {expected} but got {data}" -def test_r_colors_out_json_with_xterm_option(raster_color_dataset): - """Test r.colors.out command for JSON output format for xterm color option.""" +def test_r_colors_out_json_with_crgb_option(raster_color_dataset): + """Test r.colors.out command for JSON output format for triplet color option.""" session = raster_color_dataset data = gs.parse_command( - "r.colors.out", map="a", format="json", color_format="xterm", env=session.env + "r.colors.out", map="a", format="json", color_format="triplet", env=session.env ) validate_common_json_structure(data) expected = [ - {"value": 1, "RGB": "0:191:191"}, - {"value": 1.4, "RGB": "0:255:0"}, - {"value": 1.8, "RGB": "255:255:0"}, - {"value": 2.2, "RGB": "255:127:0"}, - {"value": 2.6, "RGB": "191:127:63"}, - {"value": 3, "RGB": "200:200:200"}, - {"value": "nv", "RGB": "255:255:255"}, - {"value": "default", "RGB": "255:255:255"}, + {"value": 1, "triplet": "0:191:191"}, + {"value": 1.4, "triplet": "0:255:0"}, + {"value": 1.8, "triplet": "255:255:0"}, + {"value": 2.2, "triplet": "255:127:0"}, + {"value": 2.6, "triplet": "191:127:63"}, + {"value": 3, "triplet": "200:200:200"}, + {"value": "nv", "triplet": "255:255:255"}, + {"value": "default", "triplet": "255:255:255"}, ] assert expected == data, f"test failed: expected {expected} but got {data}"