-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Explicit color conversion methods #10321
Explicit color conversion methods #10321
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for tackling this! I really like the diff on the existing code: it's much more explicit about what exactly is going on.
crates/bevy_render/src/color/mod.rs
Outdated
match self { | ||
/// New `Color` from `[f32; 4]` in sRGB colorspace. | ||
#[inline] | ||
pub fn rgba_from_array([r, g, b, a]: [f32; 4]) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense for these methods to take two parameters, with the second being the target color space? Or are we trying to keep the function signature fn(Vec3) -> Color
?
I like that the color space is explicit but the input type name could be elided using custom conversion traits, which would look like |
I like the way that looks, but it will make the docs harder to read and require importing a trait 🤔 Mildly in favor of it still I think? |
So what we are going to do? Conversion traits or leave this as it is? |
@mockersf you're good at having opinions; want to make a call? |
We could make the functions accept both arrays and glam's vector types like this: fn rgba_from(values: impl Into<[f32; 4]>) |
I'm sorry for starting a bikeshed 😅
This only works for Vec4 |
The |
Once the docs and PR description are cleaned up this LGTM :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd still skip the _array
suffix and keep the from_
prefix (from_rgba_linear
instead rgba_linear_from_array
) but maybe I've cause enough bike shedding 😅
I'm still wondering if it would make sense to introduce a I suppose it would then make more sense to call it So something like: let color = Color::new(my_vec4, ColorSpace::Rgba); |
@MrGVSV I like the idea with the enum, but we can't have the only |
Right. In my head I was thinking I’m not saying we have to do this, just putting the idea out there in case we want to do something like it 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I highly approve of this PR. Given that Color
supports different color spaces, we should have API coverage of each. APIs should be explicit, not have implicit assumptions on RGB. I've seen plenty of bugs in Bevy projects due to various things just assuming RGB. The "Rusty" thing to do is to have explicit APIs so you can always tell what you are working with.
Looked through the code, did not notice anything wrong.
@st0rmbtw the CI errors are real: can you fix up the broken doc tests? |
# Objective Closes bevyengine#10319 ## Changelog * Added a new `Color::rgba_from_array([f32; 4]) -> Color` method. * Added a new `Color::rgb_from_array([f32; 3]) -> Color` method. * Added a new `Color::rgba_linear_from_array([f32; 4]) -> Color` method. * Added a new `Color::rgb_linear_from_array([f32; 3]) -> Color` method. * Added a new `Color::hsla_from_array([f32; 4]) -> Color` method. * Added a new `Color::hsl_from_array([f32; 3]) -> Color` method. * Added a new `Color::lcha_from_array([f32; 4]) -> Color` method. * Added a new `Color::lch_from_array([f32; 3]) -> Color` method. * Added a new `Color::rgba_to_vec4(&self) -> Vec4` method. * Added a new `Color::rgba_to_array(&self) -> [f32; 4]` method. * Added a new `Color::rgb_to_vec3(&self) -> Vec3` method. * Added a new `Color::rgb_to_array(&self) -> [f32; 3]` method. * Added a new `Color::rgba_linear_to_vec4(&self) -> Vec4` method. * Added a new `Color::rgba_linear_to_array(&self) -> [f32; 4]` method. * Added a new `Color::rgb_linear_to_vec3(&self) -> Vec3` method. * Added a new `Color::rgb_linear_to_array(&self) -> [f32; 3]` method. * Added a new `Color::hsla_to_vec4(&self) -> Vec4` method. * Added a new `Color::hsla_to_array(&self) -> [f32; 4]` method. * Added a new `Color::hsl_to_vec3(&self) -> Vec3` method. * Added a new `Color::hsl_to_array(&self) -> [f32; 3]` method. * Added a new `Color::lcha_to_vec4(&self) -> Vec4` method. * Added a new `Color::lcha_to_array(&self) -> [f32; 4]` method. * Added a new `Color::lch_to_vec3(&self) -> Vec3` method. * Added a new `Color::lch_to_array(&self) -> [f32; 3]` method. ## Migration Guide `Color::from(Vec4)` is now `Color::rgba_from_array(impl Into<[f32; 4]>)` `Vec4::from(Color)` is now `Color::rgba_to_vec4(&self)` Before: ```rust let color_vec4 = Vec4::new(0.5, 0.5, 0.5); let color_from_vec4 = Color::from(color_vec4); let color_array = [0.5, 0.5, 0.5]; let color_from_array = Color::from(color_array); ``` After: ```rust let color_vec4 = Vec4::new(0.5, 0.5, 0.5); let color_from_vec4 = Color::rgba_from_array(color_vec4); let color_array = [0.5, 0.5, 0.5]; let color_from_array = Color::rgba_from_array(color_array); ```
Objective
Closes #10319
Changelog
Color::rgba_from_array([f32; 4]) -> Color
method.Color::rgb_from_array([f32; 3]) -> Color
method.Color::rgba_linear_from_array([f32; 4]) -> Color
method.Color::rgb_linear_from_array([f32; 3]) -> Color
method.Color::hsla_from_array([f32; 4]) -> Color
method.Color::hsl_from_array([f32; 3]) -> Color
method.Color::lcha_from_array([f32; 4]) -> Color
method.Color::lch_from_array([f32; 3]) -> Color
method.Color::rgba_to_vec4(&self) -> Vec4
method.Color::rgba_to_array(&self) -> [f32; 4]
method.Color::rgb_to_vec3(&self) -> Vec3
method.Color::rgb_to_array(&self) -> [f32; 3]
method.Color::rgba_linear_to_vec4(&self) -> Vec4
method.Color::rgba_linear_to_array(&self) -> [f32; 4]
method.Color::rgb_linear_to_vec3(&self) -> Vec3
method.Color::rgb_linear_to_array(&self) -> [f32; 3]
method.Color::hsla_to_vec4(&self) -> Vec4
method.Color::hsla_to_array(&self) -> [f32; 4]
method.Color::hsl_to_vec3(&self) -> Vec3
method.Color::hsl_to_array(&self) -> [f32; 3]
method.Color::lcha_to_vec4(&self) -> Vec4
method.Color::lcha_to_array(&self) -> [f32; 4]
method.Color::lch_to_vec3(&self) -> Vec3
method.Color::lch_to_array(&self) -> [f32; 3]
method.Migration Guide
Color::from(Vec4)
is nowColor::rgba_from_array(impl Into<[f32; 4]>)
Vec4::from(Color)
is nowColor::rgba_to_vec4(&self)
Before:
After: