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

Add parsing color_t #2015

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

VittorioSanchez
Copy link

@VittorioSanchez VittorioSanchez commented Feb 24, 2025

Added parsing for:

  • #RRGGBB
  • rgb(R, G, B)
  • hsl(H, S%, L%)
  • hsv(H, S%, V%)
  • hwb(H, W%, B%)
  • cmyk(C%, M%, Y%, K%)
  • CSS color names

These are case insensitive and whitespace insensitive. % symbol is also optional.
Limits are checked and throws error if out of range.
Updated doc and added tests

Fix: #2003

@VittorioSanchez VittorioSanchez marked this pull request as ready for review February 27, 2025 14:44
@VittorioSanchez VittorioSanchez changed the title [DRAFT] Add parsing color_t Add parsing color_t Feb 27, 2025
Copy link

codecov bot commented Feb 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.82%. Comparing base (9dbb696) to head (610e379).
Report is 12 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2015      +/-   ##
==========================================
+ Coverage   95.79%   95.82%   +0.02%     
==========================================
  Files         128      128              
  Lines       10396    10560     +164     
==========================================
+ Hits         9959    10119     +160     
- Misses        437      441       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@VittorioSanchez VittorioSanchez requested a review from snoyer March 4, 2025 13:23
Copy link
Contributor

@snoyer snoyer left a comment

Choose a reason for hiding this comment

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

some nitpicks as per tradition but look good :)

@mwestphal
Copy link
Contributor

@VittorioSanchez Is this ready for review ? :)

@VittorioSanchez
Copy link
Author

@VittorioSanchez Is this ready for review ? :)

Yes it is

Copy link
Contributor

@mwestphal mwestphal left a comment

Choose a reason for hiding this comment

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

nice work, a few questions though

@mwestphal
Copy link
Contributor

Please resolve discussions you have adressed and ping me and @snoyer for a review when ready @VittorioSanchez :)

@snoyer
Copy link
Contributor

snoyer commented Mar 8, 2025

the regex_search("^...$") calls can be replaced with regex_match("...")


See [W3C](https://www.w3.org/TR/css-color-3/#rgb-color) doc for more details on these formats.

When formatting a color into a string, it is formatted as `#RRGGBB` if values are multiple of 255. Otherwise, it is formatted as `R,G,B` where R, G, B are doubles between 0 and 1.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
When formatting a color into a string, it is formatted as `#RRGGBB` if values are multiple of 255. Otherwise, it is formatted as `R,G,B` where R, G, B are doubles between 0 and 1.
When formatting a color into a string, it is formatted as `#RRGGBB` if values are multiple of 255. Otherwise, it is formatted as vector of doubles.

Color are parsed and formatted as a vector of double.
The following formats are supported when parsing a color, case insensitive:

- R,G,B where R, G, B are doubles [0, 1]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- R,G,B where R, G, B are doubles [0, 1]
- R,G,B where R, G, B are doubles >= 0

}

/* Named colors search */
vtkSmartPointer<vtkNamedColors> color = vtkSmartPointer<vtkNamedColors>::New();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
vtkSmartPointer<vtkNamedColors> color = vtkSmartPointer<vtkNamedColors>::New();
vtkNew<vtkNamedColors> color;


/* Vector double format */
std::vector<double> vecColor = options_tools::parse<std::vector<double>>(str);
if (std::any_of(vecColor.begin(), vecColor.end(), [](double value) { return !(value >= 0.0); }))
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should remove this check. Anyone using the struct API can directly put negative values in anyway so its not like we can then skip checks later on, and it restrict a bit what the user can do.

If it makes sense to someone to input negative values, it may be simpler to let them (and catch the error later when we use the value, which we already do)

}
/* We do not catch std::invalid_argument exception from stod as it is covered by the regex */
Copy link
Contributor

Choose a reason for hiding this comment

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

nor std::out_of_range

return options_tools::format(static_cast<std::vector<double>>(var));
const std::vector<double> colors = { var.r(), var.g(), var.b() };
if (std::all_of(colors.begin(), colors.end(),
[](double val) { return (val >= 0 && val <= 1 && std::fmod(val * 255., 1) < 1e-9); }))
Copy link
Contributor

Choose a reason for hiding this comment

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

does using std::epsilon makes sense here ?

@@ -110,8 +110,39 @@ int TestSDKOptionsIO(int argc, char* argv[])

test.parse<f3d::color_t>("color_t", "0.1,0.2,0.3", { 0.1, 0.2, 0.3 });
test.parse<f3d::color_t>("color_t", " 0.1, 0.2 , 0.3 ", { 0.1, 0.2, 0.3 });
test.parse<f3d::color_t>("color_t", "#FFFFFF", { 1.0, 1.0, 1.0 });
Copy link
Contributor

Choose a reason for hiding this comment

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

looking at w3c, it looks like they also list #FFF as supported format. Does that work ? If not is it easy to add ?

test.parse_expect<f3d::color_t, parsing_exception>("incorrect size color_t", "0.1,0.2,0.3,0.4");
test.parse_expect<f3d::color_t, parsing_exception>("invalid color_t", "-0.1,-0.2,-0.3");
test.parse_expect<f3d::color_t, parsing_exception>(
"out of range color_t", std::string(outOfRangeDoubleStr + ",0.2,0.3"));
Copy link
Contributor

Choose a reason for hiding this comment

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

Im not following this test ?

Copy link
Contributor

@mwestphal mwestphal left a comment

Choose a reason for hiding this comment

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

some questions and remarks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add proper parsing for color_t
3 participants