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

[Merged by Bors] - Add 'Color::as_lcha' function (#7757) #7766

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 59 additions & 3 deletions crates/bevy_render/src/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,61 @@ impl Color {
}
}

/// Converts a `Color` to variant `Color::Lcha`
pub fn as_lcha(self: &Color) -> Color {
match self {
Color::Rgba {
red,
green,
blue,
alpha,
} => {
let (lightness, chroma, hue) =
LchRepresentation::nonlinear_srgb_to_lch([*red, *green, *blue]);
Color::Lcha {
lightness,
chroma,
hue,
alpha: *alpha,
}
}
Color::RgbaLinear {
red,
green,
blue,
alpha,
} => {
let (lightness, chroma, hue) = LchRepresentation::nonlinear_srgb_to_lch([
red.linear_to_nonlinear_srgb(),
green.linear_to_nonlinear_srgb(),
blue.linear_to_nonlinear_srgb(),
]);
Color::Lcha {
lightness,
chroma,
hue,
alpha: *alpha,
}
}
Color::Hsla {
hue,
saturation,
lightness,
alpha,
} => {
let rgb = HslRepresentation::hsl_to_nonlinear_srgb(*hue, *saturation, *lightness);
let (lightness, chroma, hue) = LchRepresentation::nonlinear_srgb_to_lch(rgb);
Color::Lcha {
lightness,
chroma,
hue,
alpha: *alpha,
}
}
Color::Lcha { .. } => *self,
}
}

/// Converts a `Color` to a `[f32; 4]` from sRGB colorspace
pub fn as_rgba_f32(self: Color) -> [f32; 4] {
match self {
Expand Down Expand Up @@ -737,7 +792,7 @@ impl Color {
}

/// Converts a `Color` to a `[f32; 4]` from LCH colorspace
pub fn as_lch_f32(self: Color) -> [f32; 4] {
pub fn as_lcha_f32(self: Color) -> [f32; 4] {
match self {
Color::Rgba {
red,
Expand Down Expand Up @@ -958,7 +1013,7 @@ impl AddAssign<Color> for Color {
hue,
alpha,
} => {
let rhs = rhs.as_lch_f32();
let rhs = rhs.as_lcha_f32();
*lightness += rhs[0];
*chroma += rhs[1];
*hue += rhs[2];
Expand Down Expand Up @@ -1021,7 +1076,7 @@ impl Add<Color> for Color {
hue,
alpha,
} => {
let rhs = rhs.as_lch_f32();
let rhs = rhs.as_lcha_f32();

Color::Lcha {
lightness: lightness + rhs[0],
Expand Down Expand Up @@ -1617,6 +1672,7 @@ impl encase::private::ReadFrom for Color {
}
}
}

impl encase::private::CreateFrom for Color {
fn create_from<B>(reader: &mut encase::private::Reader<B>) -> Self
where
Expand Down