Add a `From` implementation to `FillStyle` and use it in `canvas` for brevity

This commit is contained in:
Patrick Walton 2020-03-30 21:31:18 -07:00
parent f1d5906c9e
commit e864536b31
1 changed files with 25 additions and 4 deletions

View File

@ -155,13 +155,13 @@ impl CanvasRenderingContext2D {
// Fill and stroke styles
#[inline]
pub fn set_fill_style(&mut self, new_fill_style: FillStyle) {
self.current_state.fill_paint = new_fill_style.into_paint();
pub fn set_fill_style<FS>(&mut self, new_fill_style: FS) where FS: Into<FillStyle> {
self.current_state.fill_paint = new_fill_style.into().into_paint();
}
#[inline]
pub fn set_stroke_style(&mut self, new_stroke_style: FillStyle) {
self.current_state.stroke_paint = new_stroke_style.into_paint();
pub fn set_stroke_style<FS>(&mut self, new_stroke_style: FS) where FS: Into<FillStyle> {
self.current_state.stroke_paint = new_stroke_style.into().into_paint();
}
// Shadows
@ -710,3 +710,24 @@ impl Debug for Path2D {
self.clone().into_outline().fmt(formatter)
}
}
impl From<ColorU> for FillStyle {
#[inline]
fn from(color: ColorU) -> FillStyle {
FillStyle::Color(color)
}
}
impl From<Gradient> for FillStyle {
#[inline]
fn from(gradient: Gradient) -> FillStyle {
FillStyle::Gradient(gradient)
}
}
impl From<Pattern> for FillStyle {
#[inline]
fn from(pattern: Pattern) -> FillStyle {
FillStyle::Pattern(pattern)
}
}