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

Conversation

NishantBansal2003
Copy link
Contributor

Enhancement: Add Color Option to Category Output

Fixes: #4071

Overview

This PR introduces a new color option for r.category, allowing users to retrieve category colors alongside labels. This enhancement is useful for visualization workflows where knowing both the category label and its associated color is important.

Changes Made

  • Added a color option that allows specifying the output format:
    • none (default) – No color output.
    • rgb – Outputs colors in rgb(r, g, b) format.
    • hex – Outputs colors in #RRGGBB format.
    • triplet – Outputs colon-separated R:G:B values.
  • Updated PLAIN output format to include color:
    1 trees, very green rgb(68, 1, 84)
    2 water, very deep rgb(253, 231, 37)
    
  • Updated JSON output format to include color:
    [
        {
            "category": 1,
            "description": "trees, very green",
            "color": "rgb(68, 1, 84)"
        },
        {
            "category": 2,
            "description": "water, very deep",
            "color": "rgb(253, 231, 37)"
        }
    ]

Testing & Validation

  • Existing feature tests were taken from the r.category doctest file.
  • New feature tests for the color option have been added at the end.

Signed-off-by: Nishant Bansal <[email protected]>
@github-actions github-actions bot added raster Related to raster data processing Python Related code is in Python C Related code is in C module labels Jan 30, 2025
raster/r.category/main.c Outdated Show resolved Hide resolved
@@ -440,3 +488,47 @@ int scan_vals(const char *s, double *x)
return 1;
return 0;
}

void get_color(enum ColorOutput color_format, int red, int grn, int blu,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I would make sure you can't reuse some of the functions from the library you wrote. Maybe it's not possible, but it would be nice to avoid code duplication.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I will keep that in mind.

raster/r.category/main.c Outdated Show resolved Hide resolved
Copy link
Contributor

@petrasovaa petrasovaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, this looks great, just couple comments.

NishantBansal2003 and others added 2 commits February 4, 2025 11:16
Co-authored-by: Anna Petrasova <[email protected]>
Signed-off-by: Nishant Bansal <[email protected]>
Comment on lines +25 to +26
# Replacing '\r' because Windows uses '\r\n' for line endings, but we
# want to remove the '\r' (carriage return) to standardize line endings.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you have actual issues with this when you ran the tests on windows? That's why the replace is there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I tested this, and pytest was failing on Windows with an error similar to this for each test:

FAILED raster/r.category/tests/r_category_test.py::test_r_category_plain_output - AssertionError: Expected 1,trees
  2,water
  , but got 1,trees

  2,water

  
assert '1,trees\r\n2,water\r\n' == '1,trees\n2,water\n'
  
  - 1,trees
  + 1,trees

  ?        +
  - 2,water
  + 2,water

  ?        +

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should just read it internally with text mode (aka universal newlines) to avoid the issues at this level.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should just read it internally with text mode (aka universal newlines) to avoid the issues at this level.

See #4517?

Signed-off-by: Nishant Bansal <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C Related code is in C libraries module Python Related code is in Python raster Related to raster data processing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feat] r.category: add color option
4 participants