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

feat: Add Color::from_hsl #772

Merged
merged 2 commits into from
Jan 24, 2024
Merged

feat: Add Color::from_hsl #772

merged 2 commits into from
Jan 24, 2024

Conversation

kdheepak
Copy link
Collaborator

@kdheepak kdheepak commented Jan 9, 2024

This PR adds Color::from_hsl that returns a valid Color::Rgb.

let color: Color = Color::from_hsl(360.0, 100.0, 100.0);
assert_eq!(color, Color::Rgb(255, 255, 255));

let color: Color = Color::from_hsl(0.0, 0.0, 0.0);
assert_eq!(color, Color::Rgb(0, 0, 0));

HSL stands for Hue (0-360 deg), Saturation (0-100%), and Lightness (0-100%) and working with HSL the values can be more intuitive. For example, if you want to make a red color more orange, you can change the Hue closer toward yellow on the color wheel (i.e. increase the Hue).

Related #763

Copy link

codecov bot commented Jan 9, 2024

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (d726e92) 92.3% compared to head (3518498) 92.4%.

❗ Current head 3518498 differs from pull request most recent head 71678ba. Consider uploading reports for the commit 71678ba to get more accurate results

Files Patch % Lines
src/style/color.rs 96.5% 3 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff           @@
##            main    #772    +/-   ##
======================================
  Coverage   92.3%   92.4%            
======================================
  Files         61      57     -4     
  Lines      16010   15028   -982     
======================================
- Hits       14789   13891   -898     
+ Misses      1221    1137    -84     

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

@joshka
Copy link
Member

joshka commented Jan 9, 2024

Can we import an optional library for this functionality instead?

@orhun
Copy link
Member

orhun commented Jan 9, 2024

I think the logic/conversion is pretty simple and it does not pose a huge maintenance burden (and it is not going to change anytime soon - hsl-rs was updated 5 years ago). On the other hand, if there are more benefits to using an optional library (such as more functionality / faster code) then I'm up for it.

@kdheepak
Copy link
Collaborator Author

kdheepak commented Jan 9, 2024

If we import a library like https://crates.io/crates/palette we can convert from any color space to RGB and it would be worth it in my opinion. HSLuv is the main one that people recommend for perceptually uniform color spaces.

https://www.hsluv.org/comparison/

If we are only going to stick to HSL, I agree with @orhun, this implementation is standard and there's little maintenance cost imo

@joshka
Copy link
Member

joshka commented Jan 9, 2024

Are we just going to stick with HSL? Why that over HSV or some other approach?

@kdheepak
Copy link
Collaborator Author

My guess for why HSL is more popular than HSB/HSV is that in HSB, 100% Brightness/Value gives you the white color only when the Saturation is 0. But in HSL, 100% Lightness will give you white independent of the value of Saturation.

So HSL is more intuitive, since you can increase Lightness to get closer to white and decrease Lightness to get closer to black. With HSB/HSV you have to change two things, and in opposite directions.

@joshka
Copy link
Member

joshka commented Jan 10, 2024

I guess my point is that there are already so many versions of this sort of function in various crates:

No problem with merging this as-is though.

@joshka
Copy link
Member

joshka commented Jan 11, 2024

@kdheepak
Copy link
Collaborator Author

I don't see people needing to use cmyk (because it is primarily used for print and not digital). Also, for terminals, the alpha property is pointless.

imo only HSL and HSLuv (and maybe other equivalent perceptually uniform) color spaces are probably the most useful to have in the library.

@joshka
Copy link
Member

joshka commented Jan 11, 2024

Makes sense.

@joshka joshka merged commit d713201 into main Jan 24, 2024
31 of 32 checks passed
@joshka joshka deleted the color branch January 24, 2024 12:42
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.

3 participants