mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Fix default values for rgb-clamping 🩹
This commit is contained in:
parent
2a92428ff6
commit
8a45ec2875
@ -5,22 +5,27 @@ use super::*;
|
|||||||
pub async fn rgb(span: Span, mut args: TableValue, _: LayoutContext<'_>) -> Pass<Value> {
|
pub async fn rgb(span: Span, mut args: TableValue, _: LayoutContext<'_>) -> Pass<Value> {
|
||||||
let mut f = Feedback::new();
|
let mut f = Feedback::new();
|
||||||
|
|
||||||
let color = RgbaColor::new(
|
let r = args.expect::<Spanned<f64>>("red value", span, &mut f);
|
||||||
clamp(args.expect::<Spanned<f64>>("red value", span, &mut f), &mut f),
|
let g = args.expect::<Spanned<f64>>("green value", span, &mut f);
|
||||||
clamp(args.expect::<Spanned<f64>>("green value", span, &mut f), &mut f),
|
let b = args.expect::<Spanned<f64>>("blue value", span, &mut f);
|
||||||
clamp(args.expect::<Spanned<f64>>("blue value", span, &mut f), &mut f),
|
let a = args.take::<Spanned<f64>>();
|
||||||
clamp(args.take::<Spanned<f64>>(), &mut f),
|
|
||||||
);
|
|
||||||
|
|
||||||
args.unexpected(&mut f);
|
let mut clamp = |component: Option<Spanned<f64>>, default| {
|
||||||
Pass::new(Value::Color(color), f)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn clamp(component: Option<Spanned<f64>>, f: &mut Feedback) -> u8 {
|
|
||||||
component.map(|c| {
|
component.map(|c| {
|
||||||
if c.v < 0.0 || c.v > 255.0 {
|
if c.v < 0.0 || c.v > 255.0 {
|
||||||
error!(@f, c.span, "should be between 0 and 255")
|
error!(@f, c.span, "should be between 0 and 255")
|
||||||
}
|
}
|
||||||
c.v.min(255.0).max(0.0).round() as u8
|
c.v.min(255.0).max(0.0).round() as u8
|
||||||
}).unwrap_or_default()
|
}).unwrap_or(default)
|
||||||
|
};
|
||||||
|
|
||||||
|
let color = RgbaColor::new(
|
||||||
|
clamp(r, 0),
|
||||||
|
clamp(g, 0),
|
||||||
|
clamp(b, 0),
|
||||||
|
clamp(a, 255),
|
||||||
|
);
|
||||||
|
|
||||||
|
args.unexpected(&mut f);
|
||||||
|
Pass::new(Value::Color(color), f)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user