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

v.colors.out: Add JSON support #4964

Merged
merged 2 commits into from
Jan 22, 2025
Merged
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
8 changes: 4 additions & 4 deletions lib/raster/json_color_out.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,24 @@ static void set_color(int r, int g, int b, ColorFormat clr_frmt,
case RGB:
snprintf(color_string, sizeof(color_string), "rgb(%d, %d, %d)", r, g,
b);
G_json_object_set_string(color_object, "rgb", color_string);
G_json_object_set_string(color_object, "color", color_string);
break;

case HEX:
snprintf(color_string, sizeof(color_string), "#%02X%02X%02X", r, g, b);
G_json_object_set_string(color_object, "hex", color_string);
G_json_object_set_string(color_object, "color", color_string);
break;

case HSV:
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, "hsv", color_string);
G_json_object_set_string(color_object, "color", color_string);
break;

case TRIPLET:
snprintf(color_string, sizeof(color_string), "%d:%d:%d", r, g, b);
G_json_object_set_string(color_object, "triplet", color_string);
G_json_object_set_string(color_object, "color", color_string);
break;
}
}
Expand Down
80 changes: 40 additions & 40 deletions raster/r.colors.out/tests/r3_colors_out_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ 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, "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"},
{"value": 1, "color": "#00BFBF"},
{"value": 1.8, "color": "#00FF00"},
{"value": 2.6, "color": "#FFFF00"},
{"value": 3.4, "color": "#FF7F00"},
{"value": 4.2, "color": "#BF7F3F"},
{"value": 5, "color": "#C8C8C8"},
{"value": "nv", "color": "#FFFFFF"},
{"value": "default", "color": "#FFFFFF"},
]
assert expected == data, f"test failed: expected {expected} but got {data}"

Expand All @@ -91,14 +91,14 @@ def test_r3_colors_out_json_with_triplet_option(raster3_color_dataset):
)
validate_common_json_structure(data)
expected = [
{"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"},
{"value": 1, "color": "0:191:191"},
{"value": 1.8, "color": "0:255:0"},
{"value": 2.6, "color": "255:255:0"},
{"value": 3.4, "color": "255:127:0"},
{"value": 4.2, "color": "191:127:63"},
{"value": 5, "color": "200:200:200"},
{"value": "nv", "color": "255:255:255"},
{"value": "default", "color": "255:255:255"},
]
assert expected == data, f"test failed: expected {expected} but got {data}"

Expand All @@ -115,14 +115,14 @@ def test_r3_colors_out_json_with_rgb_option(raster3_color_dataset):
)
validate_common_json_structure(data)
expected = [
{"value": 1, "rgb": "rgb(0, 191, 191)"},
{"value": 1.8, "rgb": "rgb(0, 255, 0)"},
{"value": 2.6, "rgb": "rgb(255, 255, 0)"},
{"value": 3.4, "rgb": "rgb(255, 127, 0)"},
{"value": 4.2, "rgb": "rgb(191, 127, 63)"},
{"value": 5, "rgb": "rgb(200, 200, 200)"},
{"value": "nv", "rgb": "rgb(255, 255, 255)"},
{"value": "default", "rgb": "rgb(255, 255, 255)"},
{"value": 1, "color": "rgb(0, 191, 191)"},
{"value": 1.8, "color": "rgb(0, 255, 0)"},
{"value": 2.6, "color": "rgb(255, 255, 0)"},
{"value": 3.4, "color": "rgb(255, 127, 0)"},
{"value": 4.2, "color": "rgb(191, 127, 63)"},
{"value": 5, "color": "rgb(200, 200, 200)"},
{"value": "nv", "color": "rgb(255, 255, 255)"},
{"value": "default", "color": "rgb(255, 255, 255)"},
]
assert expected == data, f"test failed: expected {expected} but got {data}"

Expand All @@ -139,14 +139,14 @@ def test_r3_colors_out_json_with_hex_option(raster3_color_dataset):
)
validate_common_json_structure(data)
expected = [
{"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"},
{"value": 1, "color": "#00BFBF"},
{"value": 1.8, "color": "#00FF00"},
{"value": 2.6, "color": "#FFFF00"},
{"value": 3.4, "color": "#FF7F00"},
{"value": 4.2, "color": "#BF7F3F"},
{"value": 5, "color": "#C8C8C8"},
{"value": "nv", "color": "#FFFFFF"},
{"value": "default", "color": "#FFFFFF"},
]
assert expected == data, f"test failed: expected {expected} but got {data}"

Expand All @@ -163,13 +163,13 @@ def test_r3_colors_out_json_with_hsv_option(raster3_color_dataset):
)
validate_common_json_structure(data)
expected = [
{"value": 1, "hsv": "hsv(180, 100, 74)"},
{"value": 1.8, "hsv": "hsv(120, 100, 100)"},
{"value": 2.6, "hsv": "hsv(60, 100, 100)"},
{"value": 3.4, "hsv": "hsv(29, 100, 100)"},
{"value": 4.2, "hsv": "hsv(30, 67, 74)"},
{"value": 5, "hsv": "hsv(0, 0, 78)"},
{"value": "nv", "hsv": "hsv(0, 0, 100)"},
{"value": "default", "hsv": "hsv(0, 0, 100)"},
{"value": 1, "color": "hsv(180, 100, 74)"},
{"value": 1.8, "color": "hsv(120, 100, 100)"},
{"value": 2.6, "color": "hsv(60, 100, 100)"},
{"value": 3.4, "color": "hsv(29, 100, 100)"},
{"value": 4.2, "color": "hsv(30, 67, 74)"},
{"value": 5, "color": "hsv(0, 0, 78)"},
{"value": "nv", "color": "hsv(0, 0, 100)"},
{"value": "default", "color": "hsv(0, 0, 100)"},
]
assert expected == data, f"test failed: expected {expected} but got {data}"
80 changes: 40 additions & 40 deletions raster/r.colors.out/tests/r_colors_out_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ 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, "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"},
{"value": 1, "color": "#00BFBF"},
{"value": 1.4, "color": "#00FF00"},
{"value": 1.8, "color": "#FFFF00"},
{"value": 2.2, "color": "#FF7F00"},
{"value": 2.6, "color": "#BF7F3F"},
{"value": 3, "color": "#C8C8C8"},
{"value": "nv", "color": "#FFFFFF"},
{"value": "default", "color": "#FFFFFF"},
]
assert expected == data, f"test failed: expected {expected} but got {data}"

Expand All @@ -91,14 +91,14 @@ def test_r_colors_out_json_with_triplet_option(raster_color_dataset):
)
validate_common_json_structure(data)
expected = [
{"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"},
{"value": 1, "color": "0:191:191"},
{"value": 1.4, "color": "0:255:0"},
{"value": 1.8, "color": "255:255:0"},
{"value": 2.2, "color": "255:127:0"},
{"value": 2.6, "color": "191:127:63"},
{"value": 3, "color": "200:200:200"},
{"value": "nv", "color": "255:255:255"},
{"value": "default", "color": "255:255:255"},
]
assert expected == data, f"test failed: expected {expected} but got {data}"

Expand All @@ -115,14 +115,14 @@ def test_r_colors_out_json_with_rgb_option(raster_color_dataset):
)
validate_common_json_structure(data)
expected = [
{"value": 1, "rgb": "rgb(0, 191, 191)"},
{"value": 1.4, "rgb": "rgb(0, 255, 0)"},
{"value": 1.8, "rgb": "rgb(255, 255, 0)"},
{"value": 2.2, "rgb": "rgb(255, 127, 0)"},
{"value": 2.6, "rgb": "rgb(191, 127, 63)"},
{"value": 3, "rgb": "rgb(200, 200, 200)"},
{"value": "nv", "rgb": "rgb(255, 255, 255)"},
{"value": "default", "rgb": "rgb(255, 255, 255)"},
{"value": 1, "color": "rgb(0, 191, 191)"},
{"value": 1.4, "color": "rgb(0, 255, 0)"},
{"value": 1.8, "color": "rgb(255, 255, 0)"},
{"value": 2.2, "color": "rgb(255, 127, 0)"},
{"value": 2.6, "color": "rgb(191, 127, 63)"},
{"value": 3, "color": "rgb(200, 200, 200)"},
{"value": "nv", "color": "rgb(255, 255, 255)"},
{"value": "default", "color": "rgb(255, 255, 255)"},
]
assert expected == data, f"test failed: expected {expected} but got {data}"

Expand All @@ -139,14 +139,14 @@ def test_r_colors_out_json_with_hex_option(raster_color_dataset):
)
validate_common_json_structure(data)
expected = [
{"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"},
{"value": 1, "color": "#00BFBF"},
{"value": 1.4, "color": "#00FF00"},
{"value": 1.8, "color": "#FFFF00"},
{"value": 2.2, "color": "#FF7F00"},
{"value": 2.6, "color": "#BF7F3F"},
{"value": 3, "color": "#C8C8C8"},
{"value": "nv", "color": "#FFFFFF"},
{"value": "default", "color": "#FFFFFF"},
]
assert expected == data, f"test failed: expected {expected} but got {data}"

Expand All @@ -163,13 +163,13 @@ def test_r_colors_out_json_with_hsv_option(raster_color_dataset):
)
validate_common_json_structure(data)
expected = [
{"value": 1, "hsv": "hsv(180, 100, 74)"},
{"value": 1.4, "hsv": "hsv(120, 100, 100)"},
{"value": 1.8, "hsv": "hsv(60, 100, 100)"},
{"value": 2.2, "hsv": "hsv(29, 100, 100)"},
{"value": 2.6, "hsv": "hsv(30, 67, 74)"},
{"value": 3, "hsv": "hsv(0, 0, 78)"},
{"value": "nv", "hsv": "hsv(0, 0, 100)"},
{"value": "default", "hsv": "hsv(0, 0, 100)"},
{"value": 1, "color": "hsv(180, 100, 74)"},
{"value": 1.4, "color": "hsv(120, 100, 100)"},
{"value": 1.8, "color": "hsv(60, 100, 100)"},
{"value": 2.2, "color": "hsv(29, 100, 100)"},
{"value": 2.6, "color": "hsv(30, 67, 74)"},
{"value": 3, "color": "hsv(0, 0, 78)"},
{"value": "nv", "color": "hsv(0, 0, 100)"},
{"value": "default", "color": "hsv(0, 0, 100)"},
]
assert expected == data, f"test failed: expected {expected} but got {data}"
32 changes: 29 additions & 3 deletions vector/v.colors.out/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int main(int argc, char **argv)
{
struct GModule *module;
struct {
struct Option *map, *field, *file, *col;
struct Option *map, *field, *file, *col, *format, *color_format;
} opt;
struct {
struct Flag *p;
Expand All @@ -40,6 +40,8 @@ int main(int argc, char **argv)
const char *file, *name, *layer, *column;
FILE *fp;

ColorFormat clr_frmt;

G_gisinit(argv[0]);

module = G_define_module();
Expand All @@ -65,6 +67,12 @@ int main(int argc, char **argv)
opt.col->description = _("If not given, color rules refer to categories");
opt.col->guisection = _("Settings");

opt.format = G_define_standard_option(G_OPT_F_FORMAT);
opt.format->guisection = _("Print");

opt.color_format = G_define_standard_option(G_OPT_C_FORMAT);
opt.color_format->guisection = _("Color");

flag.p = G_define_flag();
flag.p->key = 'p';
flag.p->description = _("Output values as percentages");
Expand Down Expand Up @@ -106,8 +114,26 @@ int main(int argc, char **argv)
else
colors = &cat_colors;

Rast_print_colors(colors, (DCELL)min, (DCELL)max, fp,
flag.p->answer ? 1 : 0);
if (strcmp(opt.format->answer, "json") == 0) {
if (strcmp(opt.color_format->answer, "rgb") == 0) {
clr_frmt = RGB;
}
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 = HEX;
}
Rast_print_json_colors(colors, (DCELL)min, (DCELL)max, fp,
flag.p->answer ? 1 : 0, clr_frmt);
}
else {
Rast_print_colors(colors, (DCELL)min, (DCELL)max, fp,
flag.p->answer ? 1 : 0);
}

exit(EXIT_SUCCESS);
}
27 changes: 27 additions & 0 deletions vector/v.colors.out/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Fixture for v.colors.out test"""

import os
import pytest
import grass.script as gs


@pytest.fixture
def vector_color_dataset(tmp_path):
"""Set up a GRASS session and create test vector with color rules."""
project = tmp_path / "vector_color_project"
gs.create_project(project)
with gs.setup.init(project, env=os.environ.copy()) as session:
gs.run_command(
"g.region",
n=90,
s=-90,
e=180,
w=-180,
res=10,
env=session.env,
)
gs.run_command(
"v.mkgrid", map="a", grid=[10, 10], type="point", env=session.env
)
gs.run_command("v.colors", map="a", color="elevation", env=session.env)
yield session
Loading
Loading