diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-05-27 16:09:43 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-05-27 16:09:43 +0200 |
| commit | 1a7ce3da02a25900dcdc09c110fe00229fd193d4 (patch) | |
| tree | 0987ff06d52f5eb6a2aaa2d9cf8a91fb1aa2a076 /src/library | |
| parent | d4e59d4be1f4c0c673c5e22828348fc99a75facd (diff) | |
Luma color
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/mod.rs | 1 | ||||
| -rw-r--r-- | src/library/utility/color.rs | 76 |
2 files changed, 43 insertions, 34 deletions
diff --git a/src/library/mod.rs b/src/library/mod.rs index 27658189..9708475b 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -85,6 +85,7 @@ pub fn new() -> Scope { std.def_fn("odd", utility::odd); std.def_fn("mod", utility::mod_); std.def_fn("range", utility::range); + std.def_fn("luma", utility::luma); std.def_fn("rgb", utility::rgb); std.def_fn("cmyk", utility::cmyk); std.def_fn("repr", utility::repr); diff --git a/src/library/utility/color.rs b/src/library/utility/color.rs index 5857c4c7..7c6ed873 100644 --- a/src/library/utility/color.rs +++ b/src/library/utility/color.rs @@ -2,57 +2,65 @@ use std::str::FromStr; use crate::library::prelude::*; +/// Create a grayscale color. +pub fn luma(_: &mut Machine, args: &mut Args) -> TypResult<Value> { + let Component(luma) = args.expect("gray component")?; + Ok(Value::Color(LumaColor::new(luma).into())) +} + /// Create an RGB(A) color. pub fn rgb(_: &mut Machine, args: &mut Args) -> TypResult<Value> { - Ok(Value::from( + Ok(Value::Color( if let Some(string) = args.find::<Spanned<EcoString>>()? { match RgbaColor::from_str(&string.v) { - Ok(color) => color, + Ok(color) => color.into(), Err(msg) => bail!(string.span, msg), } } else { - struct Component(u8); - - castable! { - Component, - Expected: "integer or ratio", - Value::Int(v) => match v { - 0 ..= 255 => Self(v as u8), - _ => Err("must be between 0 and 255")?, - }, - Value::Ratio(v) => if (0.0 ..= 1.0).contains(&v.get()) { - Self((v.get() * 255.0).round() as u8) - } else { - Err("must be between 0% and 100%")? - }, - } - let Component(r) = args.expect("red component")?; let Component(g) = args.expect("green component")?; let Component(b) = args.expect("blue component")?; let Component(a) = args.eat()?.unwrap_or(Component(255)); - RgbaColor::new(r, g, b, a) + RgbaColor::new(r, g, b, a).into() }, )) } /// Create a CMYK color. pub fn cmyk(_: &mut Machine, args: &mut Args) -> TypResult<Value> { - struct Component(u8); + let RatioComponent(c) = args.expect("cyan component")?; + let RatioComponent(m) = args.expect("magenta component")?; + let RatioComponent(y) = args.expect("yellow component")?; + let RatioComponent(k) = args.expect("key component")?; + Ok(Value::Color(CmykColor::new(c, m, y, k).into())) +} - castable! { - Component, - Expected: "ratio", - Value::Ratio(v) => if (0.0 ..= 1.0).contains(&v.get()) { - Self((v.get() * 255.0).round() as u8) - } else { - Err("must be between 0% and 100%")? - }, - } +/// An integer or ratio component. +struct Component(u8); - let Component(c) = args.expect("cyan component")?; - let Component(m) = args.expect("magenta component")?; - let Component(y) = args.expect("yellow component")?; - let Component(k) = args.expect("key component")?; - Ok(Value::Color(CmykColor::new(c, m, y, k).into())) +castable! { + Component, + Expected: "integer or ratio", + Value::Int(v) => match v { + 0 ..= 255 => Self(v as u8), + _ => Err("must be between 0 and 255")?, + }, + Value::Ratio(v) => if (0.0 ..= 1.0).contains(&v.get()) { + Self((v.get() * 255.0).round() as u8) + } else { + Err("must be between 0% and 100%")? + }, +} + +/// A component that must be a ratio. +struct RatioComponent(u8); + +castable! { + RatioComponent, + Expected: "ratio", + Value::Ratio(v) => if (0.0 ..= 1.0).contains(&v.get()) { + Self((v.get() * 255.0).round() as u8) + } else { + Err("must be between 0% and 100%")? + }, } |
