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 color conversions #13224 #13276
Add color conversions #13224 #13276
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.
Personally, I kind of like having the explicit method for converting into the raw representation. I think for converting to a specific color, From
works great since you typically already know the target, but I'd usually be looking for the -> [f32;4]
version by pressing .
on a variable and scrolling through the methods. IME .into()
doesn't play well with tooling in this direction. Not sure what others think.
Fair point, I suppose the |
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.
Into/From is not what we want here. It needs to be more explicit. Currently you can do .into().into() to convert Srgb -> [f32; 4] Laba component-wise. I can see someone making this mistake.
My preference would be something like as_array, as_alpha_array, from_array, from_alpha_array, as_vec, as_alpha_vec ect.
From discussion in the bevy_color working group: https://discord.com/channels/691052431525675048/1236112007686652004/1237761532666970113
I'm in agreement with this. Too implicit in error-prone ways. Once this is swapped to explicit methods and away from traits I'll approve and merge this :)
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 like this trait as a way to keep channel-wise conversion standardised. My only comment is on the exact names of these methods. Otherwise, LGTM!
crates/bevy_color/src/color_ops.rs
Outdated
/// Convert to an f32 array | ||
fn to_f32_array(self) -> [f32; 4]; | ||
/// Convert to an f32 array without the alpha value | ||
fn to_alphaless_array(self) -> [f32; 3]; | ||
/// Convert to a Vec4 | ||
fn to_vec4(self) -> Vec4; | ||
/// Convert to a Vec3 | ||
fn to_vec3(self) -> Vec3; | ||
/// Convert from an f32 array | ||
fn from_array(color: [f32; 4]) -> Self; | ||
/// Convert from an f32 array without the alpha value | ||
fn from_alphaless_array(color: [f32; 3]) -> Self; | ||
/// Convert from a Vec4 | ||
fn from_vec4(color: Vec4) -> Self; | ||
/// Convert from a Vec3 | ||
fn from_vec3(color: Vec3) -> 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.
/// Convert to an f32 array | |
fn to_f32_array(self) -> [f32; 4]; | |
/// Convert to an f32 array without the alpha value | |
fn to_alphaless_array(self) -> [f32; 3]; | |
/// Convert to a Vec4 | |
fn to_vec4(self) -> Vec4; | |
/// Convert to a Vec3 | |
fn to_vec3(self) -> Vec3; | |
/// Convert from an f32 array | |
fn from_array(color: [f32; 4]) -> Self; | |
/// Convert from an f32 array without the alpha value | |
fn from_alphaless_array(color: [f32; 3]) -> Self; | |
/// Convert from a Vec4 | |
fn from_vec4(color: Vec4) -> Self; | |
/// Convert from a Vec3 | |
fn from_vec3(color: Vec3) -> Self; | |
/// Convert to an f32 array | |
fn as_array_f32(self) -> [f32; 4]; | |
/// Convert to an f32 array without the alpha value | |
fn as_array_f32_without_alpha(self) -> [f32; 3]; | |
/// Convert to a Vec4 | |
fn as_vec4(self) -> Vec4; | |
/// Convert to a Vec3 | |
fn as_vec3(self) -> Vec3; | |
/// Convert from an f32 array | |
fn from_array_f32(color: [f32; 4]) -> Self; | |
/// Convert from an f32 array without the alpha value | |
fn from_array_f32_without_alpha(color: [f32; 3]) -> Self; | |
/// Convert from a Vec4 | |
fn from_vec4(color: Vec4) -> Self; | |
/// Convert from a Vec3 | |
fn from_vec3(color: Vec3) -> Self; |
I personally don't like the word "alphaless", and I would prefer that these methods have the same prefix for the sake of nicer intellisense suggestions. Likewise for the associated from_x
methods too. Also, I believe as_x
is the more standard way of performing these conversions (as_slice
in the standard library, and in Bevy there are more definitions named as_x
than to_x
).
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.
_no_alpha
?
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.
agree with the rename except for the to as change. link below shows rusts naming conventions
Objective
fixes #13224
adds conversions for Vec3 and Vec4 since these appear so often
Solution
added Covert trait (couldn't think of good name) for [f32; 4], [f32, 3], Vec4, and Vec3 along with the symmetric implementation
Changelog
added conversions between arrays and vector to colors and vice versa
#migration
LinearRgba appears to have already had implicit conversions for [f32;4] and Vec4