summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
Diffstat (limited to 'src/library')
-rw-r--r--src/library/color.rs26
-rw-r--r--src/library/mod.rs28
2 files changed, 31 insertions, 23 deletions
diff --git a/src/library/color.rs b/src/library/color.rs
new file mode 100644
index 00000000..eab84fd0
--- /dev/null
+++ b/src/library/color.rs
@@ -0,0 +1,26 @@
+use crate::color::RgbaColor;
+use super::*;
+
+/// `rgb`: Create an RGB(A) color.
+pub async fn rgb(span: Span, mut args: TableValue, _: LayoutContext<'_>) -> Pass<Value> {
+ let mut f = Feedback::new();
+
+ let color = RgbaColor::new(
+ clamp(args.expect::<Spanned<f64>>("red value", span, &mut f), &mut f),
+ clamp(args.expect::<Spanned<f64>>("green value", span, &mut f), &mut f),
+ clamp(args.expect::<Spanned<f64>>("blue value", span, &mut f), &mut f),
+ clamp(args.take::<Spanned<f64>>(), &mut f),
+ );
+
+ args.unexpected(&mut f);
+ Pass::new(Value::Color(color), f)
+}
+
+fn clamp(component: Option<Spanned<f64>>, f: &mut Feedback) -> u8 {
+ component.map(|c| {
+ if c.v < 0.0 || c.v > 255.0 {
+ error!(@f, c.span, "should be between 0 and 255")
+ }
+ c.v.min(255.0).max(0.0).round() as u8
+ }).unwrap_or_default()
+}
diff --git a/src/library/mod.rs b/src/library/mod.rs
index eaab72fc..7d266eb5 100644
--- a/src/library/mod.rs
+++ b/src/library/mod.rs
@@ -2,12 +2,14 @@
mod align;
mod boxed;
+mod color;
mod font;
mod page;
mod spacing;
pub use align::*;
pub use boxed::*;
+pub use color::*;
pub use font::*;
pub use page::*;
pub use spacing::*;
@@ -18,10 +20,10 @@ use crate::compute::scope::Scope;
use crate::prelude::*;
macro_rules! std {
- (fallback: $fallback:expr $(, $name:literal => $func:expr)* $(,)?) => {
+ ($($name:literal => $func:expr),* $(,)?) => {
/// Create a scope with all standard library functions.
pub fn _std() -> Scope {
- let mut std = Scope::new(wrap!(val));
+ let mut std = Scope::new();
$(std.insert($name, wrap!($func));)*
std
}
@@ -35,32 +37,12 @@ macro_rules! wrap {
}
std! {
- fallback: val,
"align" => align,
"box" => boxed,
- "dump" => dump,
"font" => font,
"h" => h,
"page" => page,
"pagebreak" => pagebreak,
+ "rgb" => rgb,
"v" => v,
- "val" => val,
-}
-
-/// `val`: Layouts its body flatly, ignoring other arguments.
-///
-/// This is also the fallback function, which is used when a function name
-/// cannot be resolved.
-pub async fn val(_: Span, mut args: TableValue, _: LayoutContext<'_>) -> Pass<Value> {
- let commands = match args.take::<SyntaxTree>() {
- Some(tree) => vec![LayoutSyntaxTree(tree)],
- None => vec![],
- };
-
- Pass::commands(commands, Feedback::new())
-}
-
-/// `dump`: Dumps its arguments into the document.
-pub async fn dump(_: Span, args: TableValue, _: LayoutContext<'_>) -> Pass<Value> {
- Pass::okay(Value::Table(args))
}