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
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f8f364c
Add conversion trait
Veykril 9eeced6
removed Limited trait bounds
Veykril 277263d
Add missing error description
Veykril 1a6e53f
Changed convert to ConvertFrom/ConvertInto pair
Veykril 79c848c
Make ConvertInto wrap ConvertFrom for convert_unclamped
Veykril File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
use num_traits::Float; | ||
|
||
use {Component, Hsl, Hsv, Hwb, Lab, Lch, Xyz, Yxy}; | ||
use std::error::Error; | ||
use std::fmt::{self, Debug, Display, Formatter}; | ||
use {Component, Limited, Hsl, Hsv, Hwb, Lab, Lch, Xyz, Yxy}; | ||
use white_point::{D65, WhitePoint}; | ||
use rgb::{Rgb, RgbSpace}; | ||
use luma::Luma; | ||
|
@@ -499,6 +501,182 @@ where | |
} | ||
} | ||
|
||
///The error type for a color conversion that converted a color into a color with invalid values. | ||
#[derive(Debug)] | ||
pub struct OutOfBounds<T> { | ||
color: T, | ||
} | ||
|
||
impl<T> OutOfBounds<T> { | ||
///Create a new error wrapping a color | ||
#[inline] | ||
fn new(color: T) -> Self { | ||
OutOfBounds { color } | ||
} | ||
|
||
///Consume this error and return the wrapped color | ||
#[inline] | ||
pub fn color(self) -> T { | ||
self.color | ||
} | ||
} | ||
|
||
impl<T: Debug> Error for OutOfBounds<T> { | ||
fn description(&self) -> &str { | ||
"Color conversion is out of bounds" | ||
} | ||
} | ||
|
||
impl<T> Display for OutOfBounds<T> { | ||
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { | ||
write!(fmt, "Color conversion is out of bounds") | ||
} | ||
} | ||
|
||
///A trait for converting a color into another. | ||
/// | ||
///`convert_unclamped` currently wraps the underlying `Into` implementation. | ||
pub trait ConvertInto<T>: Into<T> { | ||
///Convert into T with values clamped to the color defined bounds | ||
/// | ||
///``` | ||
///use palette::ConvertInto; | ||
///use palette::Limited; | ||
///use palette::{Srgb, Lch}; | ||
/// | ||
/// | ||
///let rgb: Srgb = Lch::new(50.0, 100.0, -175.0).convert_into(); | ||
///assert!(rgb.is_valid()); | ||
///``` | ||
fn convert_into(self) -> T; | ||
|
||
///Convert into T. The resulting color might be invalid in its color space | ||
/// | ||
///``` | ||
///use palette::ConvertInto; | ||
///use palette::Limited; | ||
///use palette::{Srgb, Lch}; | ||
/// | ||
///let rgb: Srgb = Lch::new(50.0, 100.0, -175.0).convert_unclamped_into(); | ||
///assert!(!rgb.is_valid()); | ||
///``` | ||
#[inline] | ||
fn convert_unclamped_into(self) -> T { | ||
self.into() | ||
} | ||
|
||
///Convert into T, returning ok if the color is inside of its defined range, | ||
///otherwise an `OutOfBounds` error is returned which contains the unclamped color. | ||
/// | ||
///``` | ||
///use palette::ConvertInto; | ||
///use palette::{Srgb, Hsl}; | ||
/// | ||
///let rgb: Srgb = match Hsl::new(150.0, 1.0, 1.1).try_convert_into() { | ||
/// Ok(color) => color, | ||
/// Err(err) => { | ||
/// println!("Color is out of bounds"); | ||
/// err.color() | ||
/// }, | ||
///}; | ||
///``` | ||
fn try_convert_into(self) -> Result<T, OutOfBounds<T>>; | ||
} | ||
|
||
///A trait for converting one color from another. | ||
/// | ||
///`convert_unclamped` currently wraps the underlying `Into` implementation. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this was meant to say "From". |
||
pub trait ConvertFrom<T>: From<T> { | ||
///Convert from T with values clamped to the color defined bounds | ||
/// | ||
///``` | ||
///use palette::ConvertFrom; | ||
///use palette::Limited; | ||
///use palette::{Srgb, Lch}; | ||
/// | ||
/// | ||
///let rgb = Srgb::convert_from(Lch::new(50.0, 100.0, -175.0)); | ||
///assert!(rgb.is_valid()); | ||
///``` | ||
fn convert_from(_: T) -> Self; | ||
|
||
///Convert from T. The resulting color might be invalid in its color space | ||
/// | ||
///``` | ||
///use palette::ConvertFrom; | ||
///use palette::Limited; | ||
///use palette::{Srgb, Lch}; | ||
/// | ||
///let rgb = Srgb::convert_unclamped_from(Lch::new(50.0, 100.0, -175.0)); | ||
///assert!(!rgb.is_valid()); | ||
///``` | ||
#[inline] | ||
fn convert_unclamped_from(val: T) -> Self { | ||
Self::from(val) | ||
} | ||
|
||
///Convert from T, returning ok if the color is inside of its defined range, | ||
///otherwise an `OutOfBounds` error is returned which contains the unclamped color. | ||
/// | ||
///``` | ||
///use palette::ConvertFrom; | ||
///use palette::{Srgb, Hsl}; | ||
/// | ||
///let rgb = match Srgb::try_convert_from(Hsl::new(150.0, 1.0, 1.1)) { | ||
/// Ok(color) => color, | ||
/// Err(err) => { | ||
/// println!("Color is out of bounds"); | ||
/// err.color() | ||
/// }, | ||
///}; | ||
///``` | ||
fn try_convert_from(_: T) -> Result<Self, OutOfBounds<Self>>; | ||
} | ||
|
||
impl<T, U> ConvertFrom<T> for U where U: From<T> + Limited { | ||
fn convert_from(t: T) -> U { | ||
let mut this = U::from(t); | ||
if !this.is_valid() { | ||
this.clamp_self(); | ||
} | ||
this | ||
} | ||
|
||
fn try_convert_from(t: T) -> Result<U, OutOfBounds<U>> { | ||
let this = U::from(t); | ||
if this.is_valid() { | ||
Ok(this) | ||
} else { | ||
Err(OutOfBounds::new(this)) | ||
} | ||
} | ||
} | ||
|
||
// ConvertFrom implies ConvertInto | ||
impl<T, U> ConvertInto<U> for T where U: ConvertFrom<T> { | ||
#[inline] | ||
fn convert_into(self) -> U { | ||
U::convert_from(self) | ||
} | ||
|
||
#[inline] | ||
fn try_convert_into(self) -> Result<U, OutOfBounds<U>> { | ||
U::try_convert_from(self) | ||
} | ||
} | ||
|
||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also remove these. 🙂 |
||
impl_convert!(impl<S: RgbStandard> Rgb<S>); | ||
impl_convert!(impl<S: LumaStandard> Luma<S>); | ||
impl_convert!(float_component impl<S: RgbSpace> Hsl<S>); | ||
impl_convert!(float_component impl<S: RgbSpace> Hsv<S>); | ||
impl_convert!(float_component impl<S: RgbSpace> Hwb<S>); | ||
impl_convert!(float_component impl<Wp: WhitePoint> Lab<Wp>); | ||
impl_convert!(float_component impl<Wp: WhitePoint> Lch<Wp>); | ||
impl_convert!(float_component impl<Wp: WhitePoint> Xyz<Wp>); | ||
impl_convert!(float_component impl<Wp: WhitePoint> Yxy<Wp>); | ||
*/ | ||
|
||
macro_rules! impl_into_color { | ||
($self_ty: ident, $from_fn: ident) => { | ||
impl<Wp, T> IntoColor<Wp, T> for $self_ty<Wp, T> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could reuse the description here:
self.description().fmt(fmt)
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't implement description at all at first because it is
soft deprecated
as its docs say, hence I didnt use it there either, but I might as well. No need to duplicate the literal there.