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 color conversions #13224 #13276

Merged
merged 22 commits into from May 9, 2024

Conversation

moonlightaria
Copy link
Contributor

@moonlightaria moonlightaria commented May 7, 2024

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

@moonlightaria moonlightaria changed the title Add color conversions Add color conversions #13224 May 7, 2024
Copy link
Contributor

@tychedelia tychedelia left a 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.

@TimJentzsch
Copy link
Contributor

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 From implementation might require more use of turbofish syntax.
I will reach out to the color working group on Discord to gather more opinions, I won't block on either way :)

@pablo-lua pablo-lua added A-Rendering Drawing game state to the screen C-Usability A simple quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels May 8, 2024
Copy link
Member

@alice-i-cecile alice-i-cecile left a 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 :)

@alice-i-cecile alice-i-cecile removed the A-Rendering Drawing game state to the screen label May 8, 2024
@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged A-Color Color spaces and color math and removed S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels May 8, 2024
Copy link
Contributor

@bushrat011899 bushrat011899 left a 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!

Comment on lines 104 to 119
/// 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;
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
/// 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).

Copy link
Contributor

Choose a reason for hiding this comment

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

_no_alpha?

Copy link
Contributor Author

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

https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels May 9, 2024
@alice-i-cecile alice-i-cecile added this to the 0.14 milestone May 9, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue May 9, 2024
Merged via the queue into bevyengine:main with commit 3f2cc24 May 9, 2024
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Color Color spaces and color math C-Usability A simple quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[f32; 4] conversions missing from bevy_color
7 participants