diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-06-30 22:32:24 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-30 22:32:24 +0200 |
| commit | 17e89468847735df10381c47c46c7d82d33cc463 (patch) | |
| tree | b0ce55f7d62aac399717aac3ab5a76c981c66f65 /src/library | |
| parent | 911b5818344e85a58da9db895a333d22484b7ae7 (diff) | |
Remove color literals (#39)
Diffstat (limited to 'src/library')
| -rw-r--r-- | src/library/utility.rs | 43 |
1 files changed, 25 insertions, 18 deletions
diff --git a/src/library/utility.rs b/src/library/utility.rs index 146fce9c..272183aa 100644 --- a/src/library/utility.rs +++ b/src/library/utility.rs @@ -1,4 +1,5 @@ use std::cmp::Ordering; +use std::str::FromStr; use crate::color::{Color, RgbaColor}; use crate::pretty::pretty; @@ -37,26 +38,32 @@ pub fn len(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { /// `rgb`: Create an RGB(A) color. pub fn rgb(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { - let r = args.expect(ctx, "red component"); - let g = args.expect(ctx, "green component"); - let b = args.expect(ctx, "blue component"); - let a = args.eat(ctx); - - let mut clamp = |component: Option<Spanned<f64>>, default| { - component.map_or(default, |c| { - if c.v < 0.0 || c.v > 1.0 { - ctx.diag(warning!(c.span, "should be between 0.0 and 1.0")); + Value::Color(Color::Rgba( + if let Some(string) = args.eat::<Spanned<String>>(ctx) { + match RgbaColor::from_str(&string.v) { + Ok(color) => color, + Err(_) => { + ctx.diag(error!(string.span, "invalid color")); + return Value::Error; + } } - (c.v.max(0.0).min(1.0) * 255.0).round() as u8 - }) - }; + } else { + let r = args.expect(ctx, "red component"); + let g = args.expect(ctx, "green component"); + let b = args.expect(ctx, "blue component"); + let a = args.eat(ctx); + let mut clamp = |component: Option<Spanned<f64>>, default| { + component.map_or(default, |c| { + if c.v < 0.0 || c.v > 1.0 { + ctx.diag(warning!(c.span, "should be between 0.0 and 1.0")); + } + (c.v.max(0.0).min(1.0) * 255.0).round() as u8 + }) + }; - Value::Color(Color::Rgba(RgbaColor::new( - clamp(r, 0), - clamp(g, 0), - clamp(b, 0), - clamp(a, 255), - ))) + RgbaColor::new(clamp(r, 0), clamp(g, 0), clamp(b, 0), clamp(a, 255)) + }, + )) } /// `min`: The minimum of a sequence of values. |
