summaryrefslogtreecommitdiff
path: root/crates/typst-pdf
diff options
context:
space:
mode:
authorfrozolotl <44589151+frozolotl@users.noreply.github.com>2023-11-17 10:41:45 +0100
committerGitHub <noreply@github.com>2023-11-17 10:41:45 +0100
commitb0e81d4b3fd2bd525955f1d95145cf62d64ac096 (patch)
treea67211d85ddc0920fe11e64ac25cc440974038ed /crates/typst-pdf
parentf6215cfdafdbbb63f85abeaf5e8948d8ab5717f3 (diff)
Remove restrictions to chroma and improve clamping (#2690)
This PR does a few small things: - Oklab's a*/b* and Oklch's chroma components can be as large as desired. - In PDF, when encoding Oklab, the range is widened from [-0.4,0.4] to [-0.5,0.5]. - In PDF, clamping is now performed on Oklch's chroma instead of a* and b*. This causes hue not to be distorted when clamping. SVG and PNG export remain unchanged: - SVG itself never had any restrictions on chroma. We directly use the `oklab` and `oklch` CSS colors, which should work fine for the most part. In the future, embedded ICC profiles might be nice. Further research is likely necessary. - While PNG does not support color spaces like Oklab or Oklch, certain useful features exist. One can define gamma (gAMA) and chromacities&whitepoint (cHRM) chunks and even embed ICC profiles. While `image` crate does not support these features for encoding, its backend crate `png` does support gAMA and cHRM. It does not allow embedding ICC profiles yet, though. As it stands, to fully support wide gamuts and more accurate colors, more work is necessary. This PR should help a bit though.
Diffstat (limited to 'crates/typst-pdf')
-rw-r--r--crates/typst-pdf/src/color.rs13
-rw-r--r--crates/typst-pdf/src/postscript/oklab.ps8
2 files changed, 13 insertions, 8 deletions
diff --git a/crates/typst-pdf/src/color.rs b/crates/typst-pdf/src/color.rs
index 4c2f6183..a9ad0100 100644
--- a/crates/typst-pdf/src/color.rs
+++ b/crates/typst-pdf/src/color.rs
@@ -240,10 +240,10 @@ fn minify(source: &str) -> String {
/// Ensures that the values are in the range [0.0, 1.0].
///
/// # Why?
-/// - Oklab: The a and b components are in the range [-0.4, 0.4] and the PDF
+/// - Oklab: The a and b components are in the range [-0.5, 0.5] and the PDF
/// specifies (and some readers enforce) that all color values be in the range
/// [0.0, 1.0]. This means that the PostScript function and the encoded color
-/// must be offset by 0.4.
+/// must be offset by 0.5.
/// - HSV/HSL: The hue component is in the range [0.0, 360.0] and the PDF format
/// specifies that it must be in the range [0.0, 1.0]. This means that the
/// PostScript function and the encoded color must be divided by 360.0.
@@ -256,8 +256,13 @@ impl ColorEncode for ColorSpace {
fn encode(&self, color: Color) -> [f32; 4] {
match self {
ColorSpace::Oklab => {
- let [l, a, b, alpha] = color.to_oklab().to_vec4();
- [l, (a + 0.4).clamp(0.0, 1.0), (b + 0.4).clamp(0.0, 1.0), alpha]
+ let [l, c, h, alpha] = color.to_oklch().to_vec4();
+ // Clamp on Oklch's chroma, not Oklab's a\* and b\* as to not distort hue.
+ let c = c.clamp(0.0, 0.5);
+ // Convert cylindrical coordinates back to rectangular ones.
+ let a = c * h.to_radians().cos();
+ let b = c * h.to_radians().sin();
+ [l, a + 0.5, b + 0.5, alpha]
}
ColorSpace::Hsl => {
let [h, s, l, _] = color.to_hsl().to_vec4();
diff --git a/crates/typst-pdf/src/postscript/oklab.ps b/crates/typst-pdf/src/postscript/oklab.ps
index 4d6e9ad5..e766bbd8 100644
--- a/crates/typst-pdf/src/postscript/oklab.ps
+++ b/crates/typst-pdf/src/postscript/oklab.ps
@@ -3,12 +3,12 @@
% /!\ WARNING: The A and B components **MUST** be encoded
% in the range [0, 1] before calling this function.
% This is because the function assumes that the
- % A and B components are offset by a factor of 0.4
+ % A and B components are offset by a factor of 0.5
% in order to meet the range requirements of the
% PDF specification.
- exch 0.4 sub
- exch 0.4 sub
+ exch 0.5 sub
+ exch 0.5 sub
% Load L a and b into the stack
2 index
@@ -75,4 +75,4 @@
% Discard f1, f2, and f3 by rolling the stack and popping
6 3 roll pop pop pop
-} \ No newline at end of file
+}