diff options
| author | Sébastien d'Herbais de Thun <sebastien.d.herbais@gmail.com> | 2023-09-19 15:48:33 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-19 15:48:33 +0200 |
| commit | 163c2e1aa27169c1eba946204096d3e8fdfd3c18 (patch) | |
| tree | 0f4c58bf41239865a6cdcaac77a6314dbbb5b8c8 /tests | |
| parent | bb59f0e2b21c42a796b5eb8d8882a1d2b2a0c35f (diff) | |
Gradient Part 1 - Color rework (#2171)
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ref/compiler/color.png | bin | 444 -> 21457 bytes | |||
| -rw-r--r-- | tests/ref/compiler/repr.png | bin | 31187 -> 51799 bytes | |||
| -rw-r--r-- | tests/src/tests.rs | 15 | ||||
| -rw-r--r-- | tests/typ/compiler/color.typ | 56 | ||||
| -rw-r--r-- | tests/typ/compiler/methods.typ | 51 | ||||
| -rw-r--r-- | tests/typ/compiler/repr.typ | 10 | ||||
| -rw-r--r-- | tests/typ/compute/construct.typ | 98 |
7 files changed, 182 insertions, 48 deletions
diff --git a/tests/ref/compiler/color.png b/tests/ref/compiler/color.png Binary files differindex 8f329fab..ff9514da 100644 --- a/tests/ref/compiler/color.png +++ b/tests/ref/compiler/color.png diff --git a/tests/ref/compiler/repr.png b/tests/ref/compiler/repr.png Binary files differindex 994a6a92..82ece777 100644 --- a/tests/ref/compiler/repr.png +++ b/tests/ref/compiler/repr.png diff --git a/tests/src/tests.rs b/tests/src/tests.rs index b575b4d4..e7595cf7 100644 --- a/tests/src/tests.rs +++ b/tests/src/tests.rs @@ -24,7 +24,7 @@ use typst::diag::{bail, FileError, FileResult, Severity, StrResult}; use typst::doc::{Document, Frame, FrameItem, Meta}; use typst::eval::{eco_format, func, Bytes, Datetime, Library, NoneValue, Tracer, Value}; use typst::font::{Font, FontBook}; -use typst::geom::{Abs, Color, RgbaColor, Smart}; +use typst::geom::{Abs, Color, Smart}; use typst::syntax::{FileId, PackageVersion, Source, Span, SyntaxNode, VirtualPath}; use typst::{World, WorldExt}; use typst_library::layout::{Margin, PageElem}; @@ -160,6 +160,14 @@ fn library() -> Library { } #[func] + fn test_repr(lhs: Value, rhs: Value) -> StrResult<NoneValue> { + if lhs.repr() != rhs.repr() { + bail!("Assertion failed: {lhs:?} != {rhs:?}"); + } + Ok(NoneValue) + } + + #[func] fn print(#[variadic] values: Vec<Value>) -> NoneValue { let mut stdout = io::stdout().lock(); write!(stdout, "> ").unwrap(); @@ -188,13 +196,14 @@ fn library() -> Library { // Hook up helpers into the global scope. lib.global.scope_mut().define_func::<test>(); + lib.global.scope_mut().define_func::<test_repr>(); lib.global.scope_mut().define_func::<print>(); lib.global .scope_mut() - .define("conifer", RgbaColor::new(0x9f, 0xEB, 0x52, 0xFF)); + .define("conifer", Color::from_u8(0x9f, 0xEB, 0x52, 0xFF)); lib.global .scope_mut() - .define("forest", RgbaColor::new(0x43, 0xA1, 0x27, 0xFF)); + .define("forest", Color::from_u8(0x43, 0xA1, 0x27, 0xFF)); lib } diff --git a/tests/typ/compiler/color.typ b/tests/typ/compiler/color.typ index fbb1749b..71fce1b1 100644 --- a/tests/typ/compiler/color.typ +++ b/tests/typ/compiler/color.typ @@ -19,8 +19,58 @@ } --- +// The the different color spaces +#let col = rgb(50%, 64%, 16%) +#box(square(size: 9pt, fill: col)) +#box(square(size: 9pt, fill: rgb(col))) +#box(square(size: 9pt, fill: oklab(col))) +#box(square(size: 9pt, fill: luma(col))) +#box(square(size: 9pt, fill: cmyk(col))) +#box(square(size: 9pt, fill: color.linear-rgb(col))) +#box(square(size: 9pt, fill: color.hsl(col))) +#box(square(size: 9pt, fill: color.hsv(col))) + +--- +// Test hue rotation +#let col = rgb(50%, 64%, 16%) + +#for x in range(0, 11) { + box(square(size: 9pt, fill: rgb(col).rotate(x * 36deg))) +} + +#for x in range(0, 11) { + box(square(size: 9pt, fill: color.hsv(col).rotate(x * 36deg))) +} + +#for x in range(0, 11) { + box(square(size: 9pt, fill: color.hsl(col).rotate(x * 36deg))) +} + +--- +// Test saturation +#let col = color.hsl(180deg, 0%, 50%) +#for x in range(0, 11) { + box(square(size: 9pt, fill: col.saturate(x * 10%))) +} + +#let col = color.hsl(180deg, 100%, 50%) +#for x in range(0, 11) { + box(square(size: 9pt, fill: col.desaturate(x * 10%))) +} + +#let col = color.hsv(180deg, 0%, 50%) +#for x in range(0, 11) { + box(square(size: 9pt, fill: col.saturate(x * 10%))) +} + +#let col = color.hsv(180deg, 100%, 50%) +#for x in range(0, 11) { + box(square(size: 9pt, fill: col.desaturate(x * 10%))) +} + +--- // Test gray color modification. // Ref: false -#test(luma(20%).lighten(50%), luma(60%)) -#test(luma(80%).darken(20%), luma(63.9%)) -#test(luma(80%).negate(), luma(20%)) +#test-repr(luma(20%).lighten(50%), luma(60%)) +#test-repr(luma(80%).darken(20%), luma(64%)) +#test-repr(luma(80%).negate(), luma(20%)) diff --git a/tests/typ/compiler/methods.typ b/tests/typ/compiler/methods.typ index e4c09ed2..8f70bd8c 100644 --- a/tests/typ/compiler/methods.typ +++ b/tests/typ/compiler/methods.typ @@ -97,39 +97,40 @@ --- // Test color kind method. -#test(rgb(1, 2, 3, 4).kind(), rgb) -#test(cmyk(4%, 5%, 6%, 7%).kind(), cmyk) -#test(luma(40).kind(), luma) -#test(rgb(1, 2, 3, 4).kind() != luma, true) +#test(rgb(1, 2, 3, 4).space(), rgb) +#test(cmyk(4%, 5%, 6%, 7%).space(), cmyk) +#test(luma(40).space(), luma) +#test(rgb(1, 2, 3, 4).space() != luma, true) --- -// Test color '.rgba()', '.cmyk()' and '.luma()' without conversions -#test(rgb(1, 2, 3, 4).to-rgba(), (1, 2, 3, 4)) -#test(rgb(1, 2, 3).to-rgba(), (1, 2, 3, 255)) -#test(cmyk(20%, 20%, 40%, 20%).to-cmyk(), (20%, 20%, 40%, 20%)) -#test(luma(40).to-luma(), 40) +// Test color '.components()' without conversions +#test-repr(rgb(1, 2, 3, 4).components(), (0.39%, 0.78%, 1.18%, 1.57%)) +#test-repr(luma(40).components(), (15.69%, )) +#test-repr(cmyk(4%, 5%, 6%, 7%).components(), (4%, 5%, 6%, 7%)) +#test-repr(oklab(10%, 0.2, 0.3).components(), (10%, 0.2, 0.3, 100%)) +#test-repr(color.linear-rgb(10%, 20%, 30%).components(), (10%, 20%, 30%, 100%)) +#test-repr(color.hsv(10deg, 20%, 30%).components(), (10deg, 20%, 30%, 100%)) +#test-repr(color.hsl(10deg, 20%, 30%).components(), (10deg, 20%, 30%, 100%)) --- // Test color conversions. #test(rgb(1, 2, 3).to-hex(), "#010203") #test(rgb(1, 2, 3, 4).to-hex(), "#01020304") -#test(cmyk(4%, 5%, 6%, 7%).to-rgba(), (228, 225, 223, 255)) -#test(cmyk(4%, 5%, 6%, 7%).to-hex(), "#e4e1df") -#test(luma(40).to-rgba(), (40, 40, 40, 255)) #test(luma(40).to-hex(), "#282828") -#test(repr(luma(40).to-cmyk()), repr((11.76%, 10.59%, 10.59%, 14.12%))) - ---- -// Error: 2-27 cannot obtain cmyk values from rgba color -#rgb(1, 2, 3, 4).to-cmyk() - ---- -// Error: 2-27 cannot obtain the luma value of rgba color -#rgb(1, 2, 3, 4).to-luma() - ---- -// Error: 2-32 cannot obtain the luma value of cmyk color -#cmyk(4%, 5%, 6%, 7%).to-luma() +#test-repr(cmyk(4%, 5%, 6%, 7%).to-hex(), "#e4e1df") +#test-repr(rgb(cmyk(4%, 5%, 6%, 7%)).components(), (89.28%, 88.35%, 87.42%, 100%)) +#test-repr(rgb(luma(40%)).components(false), (40%, 40%, 40%)) +#test-repr(cmyk(luma(40)).components(), (11.76%, 10.67%, 10.51%, 14.12%)) +#test-repr(cmyk(rgb(1, 2, 3)), cmyk(66.67%, 33.33%, 0%, 98.82%)) +#test-repr(luma(rgb(1, 2, 3)), luma(0.73%)) +#test-repr(color.hsl(luma(40)), color.hsl(0deg, 0%, 15.69%)) +#test-repr(color.hsv(luma(40)), color.hsv(0deg, 0%, 15.69%)) +#test-repr(color.linear-rgb(luma(40)), color.linear-rgb(2.12%, 2.12%, 2.12%)) +#test-repr(color.linear-rgb(rgb(1, 2, 3)), color.linear-rgb(0.03%, 0.06%, 0.09%)) +#test-repr(color.hsl(rgb(1, 2, 3)), color.hsl(-150deg, 50%, 0.78%)) +#test-repr(color.hsv(rgb(1, 2, 3)), color.hsv(-150deg, 66.67%, 1.18%)) +#test-repr(oklab(luma(40)).components(), (27.68%, 0.0, 0.0, 100%)) +#test-repr(oklab(rgb(1, 2, 3)).components(), (8.23%, -0.004, -0.007, 100%)) --- // Test alignment methods. diff --git a/tests/typ/compiler/repr.typ b/tests/typ/compiler/repr.typ index 13593a86..ce5b2975 100644 --- a/tests/typ/compiler/repr.typ +++ b/tests/typ/compiler/repr.typ @@ -47,3 +47,13 @@ #int \ #type("hi") \ #type((a: 1)) + +--- +#set text(0.8em) +#blue \ +#color.linear-rgb(blue) \ +#oklab(blue) \ +#cmyk(blue) \ +#color.hsl(blue) \ +#color.hsv(blue) \ +#luma(blue) diff --git a/tests/typ/compute/construct.typ b/tests/typ/compute/construct.typ index bef86fae..d3cea0b4 100644 --- a/tests/typ/compute/construct.typ +++ b/tests/typ/compute/construct.typ @@ -3,7 +3,7 @@ --- // Compare both ways. -#test(rgb(0%, 30%, 70%), rgb("004db3")) +#test-repr(rgb(0%, 30.2%, 70.2%), rgb("004db3")) // Alpha channel. #test(rgb(255, 0, 0, 50%), rgb("ff000080")) @@ -15,24 +15,80 @@ #test(white.lighten(100%), white) // Color mixing, in Oklab space by default. -#test(color.mix(rgb("#ff0000"), rgb("#00ff00")), rgb("#d0a800")) -#test(color.mix(rgb("#ff0000"), rgb("#00ff00"), space: "oklab"), rgb("#d0a800")) -#test(color.mix(rgb("#ff0000"), rgb("#00ff00"), space: "srgb"), rgb("#808000")) +#test(rgb(color.mix(rgb("#ff0000"), rgb("#00ff00"))), rgb("#d0a800")) +#test(rgb(color.mix(rgb("#ff0000"), rgb("#00ff00"), space: oklab)), rgb("#d0a800")) +#test(rgb(color.mix(rgb("#ff0000"), rgb("#00ff00"), space: rgb)), rgb("#808000")) -#test(color.mix(red, green, blue), rgb("#909282")) -#test(color.mix(red, blue, green), rgb("#909282")) -#test(color.mix(blue, red, green), rgb("#909282")) +#test(rgb(color.mix(red, green, blue)), rgb("#909282")) +#test(rgb(color.mix(red, blue, green)), rgb("#909282")) +#test(rgb(color.mix(blue, red, green)), rgb("#909282")) // Mix with weights. -#test(color.mix((red, 50%), (green, 50%)), rgb("#c0983b")) -#test(color.mix((red, 0.5), (green, 0.5)), rgb("#c0983b")) -#test(color.mix((red, 5), (green, 5)), rgb("#c0983b")) -#test(color.mix((green, 5), (white, 0), (red, 5)), rgb("#c0983b")) -#test(color.mix((red, 100%), (green, 0%)), red) -#test(color.mix((red, 0%), (green, 100%)), green) -#test(color.mix((rgb("#aaff00"), 25%), (rgb("#aa00ff"), 75%), space: "srgb"), rgb("#aa40bf")) -#test(color.mix((rgb("#aaff00"), 50%), (rgb("#aa00ff"), 50%), space: "srgb"), rgb("#aa8080")) -#test(color.mix((rgb("#aaff00"), 75%), (rgb("#aa00ff"), 25%), space: "srgb"), rgb("#aabf40")) +#test(rgb(color.mix((red, 50%), (green, 50%))), rgb("#c0983b")) +#test(rgb(color.mix((red, 0.5), (green, 0.5))), rgb("#c0983b")) +#test(rgb(color.mix((red, 5), (green, 5))), rgb("#c0983b")) +#test(rgb(color.mix((green, 5), (white, 0), (red, 5))), rgb("#c0983b")) +#test(color.mix((rgb("#aaff00"), 25%), (rgb("#aa00ff"), 75%), space: rgb), rgb("#aa40bf")) +#test(color.mix((rgb("#aaff00"), 50%), (rgb("#aa00ff"), 50%), space: rgb), rgb("#aa8080")) +#test(color.mix((rgb("#aaff00"), 75%), (rgb("#aa00ff"), 25%), space: rgb), rgb("#aabf40")) + +--- +// Test color conversion method kinds +#test(rgb(rgb(10, 20, 30)).space(), rgb) +#test(color.linear-rgb(rgb(10, 20, 30)).space(), color.linear-rgb) +#test(oklab(rgb(10, 20, 30)).space(), oklab) +#test(color.hsl(rgb(10, 20, 30)).space(), color.hsl) +#test(color.hsv(rgb(10, 20, 30)).space(), color.hsv) +#test(cmyk(rgb(10, 20, 30)).space(), cmyk) +#test(luma(rgb(10, 20, 30)).space(), luma) + +#test(rgb(color.linear-rgb(10, 20, 30)).space(), rgb) +#test(color.linear-rgb(color.linear-rgb(10, 20, 30)).space(), color.linear-rgb) +#test(oklab(color.linear-rgb(10, 20, 30)).space(), oklab) +#test(color.hsl(color.linear-rgb(10, 20, 30)).space(), color.hsl) +#test(color.hsv(color.linear-rgb(10, 20, 30)).space(), color.hsv) +#test(cmyk(color.linear-rgb(10, 20, 30)).space(), cmyk) +#test(luma(color.linear-rgb(10, 20, 30)).space(), luma) + +#test(rgb(oklab(10%, 20%, 30%)).space(), rgb) +#test(color.linear-rgb(oklab(10%, 20%, 30%)).space(), color.linear-rgb) +#test(oklab(oklab(10%, 20%, 30%)).space(), oklab) +#test(color.hsl(oklab(10%, 20%, 30%)).space(), color.hsl) +#test(color.hsv(oklab(10%, 20%, 30%)).space(), color.hsv) +#test(cmyk(oklab(10%, 20%, 30%)).space(), cmyk) +#test(luma(oklab(10%, 20%, 30%)).space(), luma) + +#test(rgb(color.hsl(10deg, 20%, 30%)).space(), rgb) +#test(color.linear-rgb(color.hsl(10deg, 20%, 30%)).space(), color.linear-rgb) +#test(oklab(color.hsl(10deg, 20%, 30%)).space(), oklab) +#test(color.hsl(color.hsl(10deg, 20%, 30%)).space(), color.hsl) +#test(color.hsv(color.hsl(10deg, 20%, 30%)).space(), color.hsv) +#test(cmyk(color.hsl(10deg, 20%, 30%)).space(), cmyk) +#test(luma(color.hsl(10deg, 20%, 30%)).space(), luma) + +#test(rgb(color.hsv(10deg, 20%, 30%)).space(), rgb) +#test(color.linear-rgb(color.hsv(10deg, 20%, 30%)).space(), color.linear-rgb) +#test(oklab(color.hsv(10deg, 20%, 30%)).space(), oklab) +#test(color.hsl(color.hsv(10deg, 20%, 30%)).space(), color.hsl) +#test(color.hsv(color.hsv(10deg, 20%, 30%)).space(), color.hsv) +#test(cmyk(color.hsv(10deg, 20%, 30%)).space(), cmyk) +#test(luma(color.hsv(10deg, 20%, 30%)).space(), luma) + +#test(rgb(cmyk(10%, 20%, 30%, 40%)).space(), rgb) +#test(color.linear-rgb(cmyk(10%, 20%, 30%, 40%)).space(), color.linear-rgb) +#test(oklab(cmyk(10%, 20%, 30%, 40%)).space(), oklab) +#test(color.hsl(cmyk(10%, 20%, 30%, 40%)).space(), color.hsl) +#test(color.hsv(cmyk(10%, 20%, 30%, 40%)).space(), color.hsv) +#test(cmyk(cmyk(10%, 20%, 30%, 40%)).space(), cmyk) +#test(luma(cmyk(10%, 20%, 30%, 40%)).space(), luma) + +#test(rgb(luma(10%)).space(), rgb) +#test(color.linear-rgb(luma(10%)).space(), color.linear-rgb) +#test(oklab(luma(10%)).space(), oklab) +#test(color.hsl(luma(10%)).space(), color.hsl) +#test(color.hsv(luma(10%)).space(), color.hsv) +#test(cmyk(luma(10%)).space(), cmyk) +#test(luma(luma(10%)).space(), luma) --- // Test gray color conversion. @@ -70,10 +126,18 @@ #color.mix((red, 1, 2)) --- -// Error: 31-38 expected "oklab" or "srgb" +// Error: 31-38 expected `rgb`, `luma`, `cmyk`, `oklab`, `color.linear-rgb`, `color.hsl`, or `color.hsv`, found string #color.mix(red, green, space: "cyber") --- +// Error: 31-36 expected `rgb`, `luma`, `cmyk`, `oklab`, `color.linear-rgb`, `color.hsl`, or `color.hsv` +#color.mix(red, green, space: image) + +--- +// Error: 31-41 expected `rgb`, `luma`, `cmyk`, `oklab`, `color.linear-rgb`, `color.hsl`, or `color.hsv` +#color.mix(red, green, space: calc.round) + +--- // Ref: true #let envelope = symbol( "🖂", |
