summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-10-05 19:55:15 +0200
committerLaurenz <laurmaedje@gmail.com>2021-10-05 19:55:15 +0200
commit3d0dcbea182f6a81539c12c9d5156cf0f6863b67 (patch)
tree36a37fb5e39db8ca13f83ca362d01c0e0a9fdffa /src/library
parent25b053ed93a6cbffceb52b790d013d69dc2a31c2 (diff)
Error on out-of-range values in `rgb`
Diffstat (limited to 'src/library')
-rw-r--r--src/library/mod.rs2
-rw-r--r--src/library/utility.rs44
2 files changed, 26 insertions, 20 deletions
diff --git a/src/library/mod.rs b/src/library/mod.rs
index 1fcd0581..411755bb 100644
--- a/src/library/mod.rs
+++ b/src/library/mod.rs
@@ -22,7 +22,7 @@ use crate::eval::{Args, Array, EvalContext, Scope, State, Str, Template, Value};
use crate::font::{FontFamily, FontStretch, FontStyle, FontWeight, VerticalFontMetric};
use crate::geom::*;
use crate::layout::LayoutNode;
-use crate::syntax::Spanned;
+use crate::syntax::{Span, Spanned};
/// Construct a scope containing all standard library definitions.
pub fn new() -> Scope {
diff --git a/src/library/utility.rs b/src/library/utility.rs
index 3a2f49b7..ef7adff0 100644
--- a/src/library/utility.rs
+++ b/src/library/utility.rs
@@ -84,6 +84,31 @@ pub fn str(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
}))
}
+/// `rgb`: Create an RGB(A) color.
+pub fn rgb(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
+ Ok(Value::Color(Color::Rgba(
+ if let Some(string) = args.eat::<Spanned<Str>>() {
+ match RgbaColor::from_str(&string.v) {
+ Ok(color) => color,
+ Err(_) => bail!(string.span, "invalid color"),
+ }
+ } else {
+ let r = args.expect("red component")?;
+ let g = args.expect("green component")?;
+ let b = args.expect("blue component")?;
+ let a = args.eat().unwrap_or(Spanned::new(1.0, Span::detached()));
+ let f = |Spanned { v, span }: Spanned<f64>| {
+ if 0.0 <= v && v <= 1.0 {
+ Ok((v * 255.0).round() as u8)
+ } else {
+ bail!(span, "value must be between 0.0 and 1.0");
+ }
+ };
+ RgbaColor::new(f(r)?, f(g)?, f(b)?, f(a)?)
+ },
+ )))
+}
+
/// `abs`: The absolute value of a numeric value.
pub fn abs(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
let Spanned { v, span } = args.expect("numeric value")?;
@@ -130,25 +155,6 @@ fn minmax(args: &mut Args, goal: Ordering) -> TypResult<Value> {
Ok(extremum)
}
-/// `rgb`: Create an RGB(A) color.
-pub fn rgb(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
- Ok(Value::Color(Color::Rgba(
- if let Some(string) = args.eat::<Spanned<Str>>() {
- match RgbaColor::from_str(&string.v) {
- Ok(color) => color,
- Err(_) => bail!(string.span, "invalid color"),
- }
- } else {
- let r = args.expect("red component")?;
- let g = args.expect("green component")?;
- let b = args.expect("blue component")?;
- let a = args.eat().unwrap_or(1.0);
- let f = |v: f64| (v.clamp(0.0, 1.0) * 255.0).round() as u8;
- RgbaColor::new(f(r), f(g), f(b), f(a))
- },
- )))
-}
-
/// `lower`: Convert a string to lowercase.
pub fn lower(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
Ok(args.expect::<Str>("string")?.to_lowercase().into())