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 From implementations for changing Rgb component types between u8, f32 and f64 #390

Merged
merged 1 commit into from
Apr 20, 2024
Merged
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
91 changes: 89 additions & 2 deletions palette/src/rgb/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ pub type Rgba<S = Srgb, T = f32> = Alpha<Rgb<S, T>, T>;
/// // `new` is also `const`:
/// const RGB_U8: Srgb<u8> = Srgb::new(171, 193, 35);
///
/// // Converting from one number format to another is as simple as this:
/// let rgb_u8_from_f32 = Srgb::new(0.3f32, 0.8, 0.1).into_format::<u8>();
/// // Converting from one number format to another can be as simple as this:
/// let rgb_u8_from_f32_1: Srgb<u8> = Srgb::new(0.3f32, 0.8, 0.1).into();
///
/// // ...or more explicitly like this:
/// let rgb_u8_from_f32_2 = Srgb::new(0.3f32, 0.8, 0.1).into_format::<u8>();
///
/// // Hexadecimal is also supported, with or without the #:
/// let rgb_from_hex1: Srgb<u8> = "#f034e6".parse().unwrap();
Expand Down Expand Up @@ -1212,6 +1215,90 @@ impl<S> From<Rgba<S, u8>> for u32 {
}
}

impl<S> From<Rgb<S, u8>> for Rgb<S, f32> {
#[inline]
fn from(color: Rgb<S, u8>) -> Self {
color.into_format()
}
}

impl<S> From<Rgba<S, u8>> for Rgba<S, f32> {
#[inline]
fn from(color: Rgba<S, u8>) -> Self {
color.into_format()
}
}

impl<S> From<Rgb<S, f32>> for Rgb<S, u8> {
#[inline]
fn from(color: Rgb<S, f32>) -> Self {
color.into_format()
}
}

impl<S> From<Rgba<S, f32>> for Rgba<S, u8> {
#[inline]
fn from(color: Rgba<S, f32>) -> Self {
color.into_format()
}
}

impl<S> From<Rgb<S, u8>> for Rgb<S, f64> {
#[inline]
fn from(color: Rgb<S, u8>) -> Self {
color.into_format()
}
}

impl<S> From<Rgba<S, u8>> for Rgba<S, f64> {
#[inline]
fn from(color: Rgba<S, u8>) -> Self {
color.into_format()
}
}

impl<S> From<Rgb<S, f64>> for Rgb<S, u8> {
#[inline]
fn from(color: Rgb<S, f64>) -> Self {
color.into_format()
}
}

impl<S> From<Rgba<S, f64>> for Rgba<S, u8> {
#[inline]
fn from(color: Rgba<S, f64>) -> Self {
color.into_format()
}
}

impl<S> From<Rgb<S, f32>> for Rgb<S, f64> {
#[inline]
fn from(color: Rgb<S, f32>) -> Self {
color.into_format()
}
}

impl<S> From<Rgba<S, f32>> for Rgba<S, f64> {
#[inline]
fn from(color: Rgba<S, f32>) -> Self {
color.into_format()
}
}

impl<S> From<Rgb<S, f64>> for Rgb<S, f32> {
#[inline]
fn from(color: Rgb<S, f64>) -> Self {
color.into_format()
}
}

impl<S> From<Rgba<S, f64>> for Rgba<S, f32> {
#[inline]
fn from(color: Rgba<S, f64>) -> Self {
color.into_format()
}
}

#[allow(deprecated)]
impl<S, T> crate::RelativeContrast for Rgb<S, T>
where
Expand Down
Loading