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

Explicit color conversion methods #10321

Merged
merged 9 commits into from
Nov 15, 2023

Conversation

st0rmbtw
Copy link
Contributor

@st0rmbtw st0rmbtw commented Oct 30, 2023

Objective

Closes #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:

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:

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);

@st0rmbtw st0rmbtw changed the title Add color conversion methods Explicit color conversion methods Oct 30, 2023
@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide labels Oct 30, 2023
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.

Thanks for tackling this! I really like the diff on the existing code: it's much more explicit about what exactly is going on.

match self {
/// New `Color` from `[f32; 4]` in sRGB colorspace.
#[inline]
pub fn rgba_from_array([r, g, b, a]: [f32; 4]) -> Self {
Copy link
Member

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?

@killercup
Copy link
Contributor

killercup commented Oct 31, 2023

I like that the color space is explicit but the input type name could be elided using custom conversion traits, which would look like Color::rgba_from(Vec4), Color::rgba_from(Vec3), and Color::rgba_from([f32; 4]). Is that something we want? Or do we have other APIs that explicitly contain the type name?

@alice-i-cecile
Copy link
Member

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?

@st0rmbtw
Copy link
Contributor Author

So what we are going to do? Conversion traits or leave this as it is?

@alice-i-cecile
Copy link
Member

@mockersf you're good at having opinions; want to make a call?

@tim-blackbird
Copy link
Contributor

We could make the functions accept both arrays and glam's vector types like this:

fn rgba_from(values: impl Into<[f32; 4]>)

@killercup
Copy link
Contributor

I'm sorry for starting a bikeshed 😅

We could make the functions accept both arrays and glam's vector types like this:

fn rgba_from(values: impl Into<[f32; 4]>)

This only works for Vec4

@tim-blackbird
Copy link
Contributor

This only works for Vec4

The Vec3 case would be a separate method, from_rgb like the existing constructors.

@alice-i-cecile
Copy link
Member

Once the docs and PR description are cleaned up this LGTM :)

Copy link
Contributor

@killercup killercup left a 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 😅

@MrGVSV
Copy link
Member

MrGVSV commented Nov 1, 2023

I'm still wondering if it would make sense to introduce a ColorSpace enum and just specify that as a second parameter so we don't have to introduce a bunch of new methods for each color space.

I suppose it would then make more sense to call it new or something in that case since from methods tend to only take a single parameter.

So something like:

let color = Color::new(my_vec4, ColorSpace::Rgba);

@st0rmbtw
Copy link
Contributor Author

st0rmbtw commented Nov 2, 2023

@MrGVSV I like the idea with the enum, but we can't have the only new method, because we want to create a Color from different types like: Vec4, [f32; 4], Vec3, [f32; 3] and Rust doesn't allow to overload functions.

@MrGVSV
Copy link
Member

MrGVSV commented Nov 2, 2023

@MrGVSV I like the idea with the enum, but we can't have the only new method, because we want to create a Color from different types like: Vec4, [f32; 4], Vec3, [f32; 3] and Rust doesn't allow to overload functions.

Right. In my head I was thinking new_vec4 and such but thinking on this now I feel like keeping the from_vec4 should be okay despite the second argument. It reads clearer at least.

I’m not saying we have to do this, just putting the idea out there in case we want to do something like it 😄

Copy link
Contributor

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

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Nov 6, 2023
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Nov 14, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 14, 2023
@alice-i-cecile
Copy link
Member

@st0rmbtw the CI errors are real: can you fix up the broken doc tests?

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Nov 15, 2023
Merged via the queue into bevyengine:main with commit cbcd826 Nov 15, 2023
21 checks passed
rdrpenguin04 pushed a commit to rdrpenguin04/bevy that referenced this pull request Jan 9, 2024
# 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);
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide 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.

Trait-based color conversions to/from Vec4 are too implicit and should be removed
6 participants