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 Extended Conversion Trait #106

Merged
merged 5 commits into from Jun 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 57 additions & 1 deletion palette/src/convert.rs
@@ -1,9 +1,11 @@
use num_traits::Float;

use {Component, Hsl, Hsv, Hwb, Lab, Lch, Xyz, Yxy};
use Pixel;
use {Component, Limited, Hsl, Hsv, Hwb, Lab, Lch, Xyz, Yxy};
use white_point::{D65, WhitePoint};
use rgb::{Rgb, RgbSpace};
use luma::Luma;
use Alpha;
use encoding::Linear;

/// FromColor provides conversion from the colors.
Expand Down Expand Up @@ -499,6 +501,60 @@ where
}
}

///A trait for converting from one color to another.
///
///This trait wraps the underlying `From` implementation.
pub trait Convert: Sized {
///Lossless, same as calling T::from(self)
#[inline]
fn convert<T: From<Self>>(self) -> T {
T::from(self)
}

///Convert into T with values clamped to the color defined bounds
fn convert_clamped<T: From<Self> + Limited>(self) -> T {
let mut this: T = self.convert();
if !this.is_valid() {
this.clamp_self();
}
this
}

///Convert into Option<T>, returning None if the resulting Color is outside of its defined range
fn try_convert<T: From<Self> + Limited>(self) -> Option<T> {
let this: T = self.convert();
if this.is_valid() {
Some(this)
} else {
None
}
}
}

impl Convert for Rgb {}
impl Convert for Luma {}
impl Convert for Hsl {}
impl Convert for Hsv {}
impl Convert for Hwb {}
impl Convert for Lab {}
impl Convert for Lch {}
impl Convert for Xyz {}
impl Convert for Yxy {}
impl<C, T> Convert for Alpha<C, T> where C: Convert, T: Component {}

///A trait for converting from one color to an arbitrary type by handing a closure the color's raw components.
///
///This trait makes use of the `Pixel` trait which can be unsafe depending on the implementator.
pub trait ConvertWith<T>: Pixel<T> where T: Component
{
///Convert `self` to `T` by applying a function over its components
fn convert_with<F, Out>(self, f: F) -> Out where F: FnOnce(&[T]) -> Out, Out: Sized {
f(Pixel::into_raw_slice(&[self]))
Copy link
Owner

@Ogeon Ogeon Jun 3, 2018

Choose a reason for hiding this comment

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

This could be as_raw and convert_with could take &self. Could also use into_raw, but that becomes more complicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes, I had problems with coming up for a name for this function actually. as_raw should work here I suppose, and I didn't take self by reference because I thought of it being a conversion into something else so consuming self would've fit there. It's probably better to take it be ref nevertheless. Any Ideas on the trait name for that?

Copy link
Owner

Choose a reason for hiding this comment

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

I have no better name suggestion at the moment.

}
}

impl<T, U> ConvertWith<T> for U where U: Pixel<T>, T: Component {}

macro_rules! impl_into_color {
($self_ty: ident, $from_fn: ident) => {
impl<Wp, T> IntoColor<Wp, T> for $self_ty<Wp, T>
Expand Down
2 changes: 1 addition & 1 deletion palette/src/lib.rs
Expand Up @@ -182,7 +182,7 @@ pub use rgb::{GammaSrgb, GammaSrgba, LinSrgb, LinSrgba, Srgb, Srgba};
pub use xyz::{Xyz, Xyza};
pub use yxy::{Yxy, Yxya};

pub use convert::{FromColor, IntoColor};
pub use convert::{Convert, ConvertWith, FromColor, IntoColor};
pub use encoding::pixel::Pixel;
pub use hues::{LabHue, RgbHue};
pub use matrix::Mat3;
Expand Down