Skip to content

Commit

Permalink
added changes based on review
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Bansal <[email protected]>
  • Loading branch information
NishantBansal2003 committed Nov 2, 2024
1 parent 4fa1857 commit c16a63a
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 78 deletions.
2 changes: 1 addition & 1 deletion include/grass/gis.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
9 changes: 5 additions & 4 deletions lib/gis/parser_standard_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion raster/r.colors.out/local_proto.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <grass/gis.h>
#include <grass/parson.h>

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);
50 changes: 22 additions & 28 deletions raster/r.colors.out/prt_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}
}

/*!
Expand All @@ -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
*/
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions raster/r.colors.out/raster3d_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions raster/r.colors.out/raster_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 19 additions & 19 deletions raster/r.colors.out/tests/r3_colors_out_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down
38 changes: 19 additions & 19 deletions raster/r.colors.out/tests/r_colors_out_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down

0 comments on commit c16a63a

Please sign in to comment.