Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong color encoding in PDF gradients #4422

Closed
1 task done
EpicEricEE opened this issue Jun 20, 2024 · 2 comments
Closed
1 task done

Wrong color encoding in PDF gradients #4422

EpicEricEE opened this issue Jun 20, 2024 · 2 comments
Labels
bug Something isn't working pdf Related to PDF export visualize Related to drawing and visualizations.

Comments

@EpicEricEE
Copy link
Contributor

Description

Gradients using the CMYK color space don't produce the same results in PDF as in the webapp preview. The behaviour is also dependent on the viewer or printer.

Example Code
#rect(fill: cmyk(red))

#rect(fill: gradient.linear(
  cmyk(red),
  cmyk(red).darken(70%),
  space: cmyk
))

#rect(fill: gradient.linear(
  cmyk(red),
  cmyk(red).darken(100%),
  space: cmyk
))

#rect(fill: gradient.linear(
  red,
  red.darken(70%),
  space: cmyk
))

#rect(fill: gradient.linear(
  red,
  red.darken(100%),
  space: cmyk
))

#rect(fill: gradient.linear(
  red,
  blue,
  space: cmyk
))
Webapp Chrome Edge vscode-pdf
image image image image

Reproduction URL

No response

Operating system

Web app, Windows

Typst version

  • I am using the latest version of Typst
@EpicEricEE EpicEricEE added the bug Something isn't working label Jun 20, 2024
@EpicEricEE
Copy link
Contributor Author

The problem seems to be that only the first three components are stored for each color in the shading function as we assume that the last component is always the alpha value, but in CMYK it's the black value:

fn convert<U: QuantizedColor>(self, color: Color) -> [U; 3] {
let range = self.range();
let [x, y, z, _] = self.encode(color);
[
U::quantize(x, [range[0], range[1]]),
U::quantize(y, [range[2], range[3]]),
U::quantize(z, [range[4], range[5]]),
]
}

If I manually add a fourth component in the PDF file, the gradients look more correct (and show up in edge).

@laurmaedje laurmaedje added pdf Related to PDF export visualize Related to drawing and visualizations. labels Jun 21, 2024
@EpicEricEE
Copy link
Contributor Author

I just found out that a very similar issue also affects luma gradients. The colors are also stored with three components (where all are the same)

<<
  /FunctionType 2
  /Range [0 1 0 1 0 1]  <--- /Range [0 1]
  /C0 [0 0 0]           <--- /C0 [0]
  /C1 [1 1 1]           <--- /C1 [1]
  /Domain [0 1]
  /N 1
>>

but the D65Gray color space seems to only expect a single component, making Edge (and I suppose Adobe Acrobat) fail to render the gradient. When removing the additional numbers from /C0, /C1 and /Range, it works as expected.

@EpicEricEE EpicEricEE changed the title CMYK gradient issues with PDF Wrong color encoding in PDF gradients Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pdf Related to PDF export visualize Related to drawing and visualizations.
Projects
None yet
Development

No branches or pull requests

2 participants