Add test and fixup color red/green/blue fields

This commit is contained in:
Olivier Goffart 2024-03-12 10:49:18 +01:00
parent 3cf663ec82
commit b844f76842
4 changed files with 25 additions and 5 deletions

View file

@ -3049,7 +3049,7 @@ fn compile_builtin_function_call(
format!("[](const auto &a){{ auto e1 = std::end(a); auto e2 = const_cast<char*>(e1); auto r = std::strtod(std::begin(a), &e2); return e1 == e2 ? r : 0; }}({})", a.next().unwrap())
}
BuiltinFunction::ColorRgbaStruct => {
format!("{}.to_argb_u8()", a.next().unwrap())
format!("{}.to_argb_uint()", a.next().unwrap())
}
BuiltinFunction::ColorBrighter => {
format!("{}.brighter({})", a.next().unwrap(), a.next().unwrap())

View file

@ -997,6 +997,11 @@ impl<'a> LookupObject for ColorExpression<'a> {
})
};
let field_access = |f: &str| {
let base = if self.0.ty() == Type::Brush {
Expression::Cast { from: Box::new(self.0.clone()), to: Type::Color }
} else {
self.0.clone()
};
LookupResult::from(Expression::StructFieldAccess {
base: Box::new(Expression::FunctionCall {
function: Box::new(Expression::BuiltinFunctionReference(
@ -1004,7 +1009,7 @@ impl<'a> LookupObject for ColorExpression<'a> {
ctx.current_token.as_ref().map(|t| t.to_source_location()),
)),
source_location: ctx.current_token.as_ref().map(|t| t.to_source_location()),
arguments: vec![self.0.clone()],
arguments: vec![base],
}),
name: f.into(),
})

View file

@ -28,8 +28,10 @@ Test := Rectangle {
&& background != colo;
}
property<bool> test: lighter == Colors.blue.brighter(50%) && r2_col == Colors.red.darker(50%) && conditional == r2.background && conditional != r2_col
&& test_circle.test && seethru == color_brush.with_alpha(70%) && (#abc2).transparentize(-100%) == #abc4;
out property <bool> test_rgb: rgb(color_brush.red, color_brush.green, color_brush.blue) == color_brush;
out property<bool> test: lighter == Colors.blue.brighter(50%) && r2_col == Colors.red.darker(50%) && conditional == r2.background && conditional != r2_col
&& test_circle.test && seethru == color_brush.with_alpha(70%) && (#abc2).transparentize(-100%) == #abc4 && test_rgb;
}
/*

View file

@ -29,7 +29,11 @@ Test := Rectangle {
// allow to use `with_alpha` on colors
property<brush> invisible: b1.with-alpha(0%);
property<bool> test: b1 == b2 && b2 == b5 && b3 == Colors.blue && Colors.red == r4 && y1 == Colors.rgba(255, 100%, 0, 100%);
out property <bool> test_rgb: Colors.blue.blue == 255 && Colors.blue.red == 0 && Colors.blue.green == 0 && Colors.blue.alpha == 255
&& Colors.rgb(45, 12, 78).red == 45 && Colors.rgb(45, 12, 78).green == 12 && Colors.rgba(45, 12, 78, 12/255).alpha == 12 && Colors.rgba(145, 112, 178, 85%).alpha == floor(85% * 255)
&& #abc.green == (11 * 16 + 11) && #abcd.alpha == (13 * 16 + 13) && #abcdef.red == (10 * 16 + 11);
property<bool> test: b1 == b2 && b2 == b5 && b3 == Colors.blue && Colors.red == r4 && y1 == Colors.rgba(255, 100%, 0, 100%) && test_rgb;
}
/*
@ -77,6 +81,9 @@ assert_eq(t.get_i1().alpha(), 0xff);
assert_eq(t.get_i1().red(), 0);
assert_eq(t.get_i1().green(), 255);
assert_eq(t.get_i1().blue(), 0);
assert(t.get_test_rgb());
assert(t.get_test());
```
@ -124,6 +131,9 @@ assert_eq!(t.get_i1().red(), 0);
assert_eq!(t.get_i1().green(), 255);
assert_eq!(t.get_i1().blue(), 0);
assert!(t.get_test_rgb());
assert!(t.get_test());
```
```js
@ -156,5 +166,8 @@ assert.equal(t.c2.toString(), "#64172aff");
assert.equal(t.c3.toString(), "#637f28ce");
assert.equal(t.i1.toString(), "#00ff00ff");
assert(t.test_rgb);
assert(t.test);
```
*/