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

Problem with transparent fadings in cmyk colour model #1057

Open
samcarter opened this issue Oct 8, 2021 · 12 comments
Open

Problem with transparent fadings in cmyk colour model #1057

samcarter opened this issue Oct 8, 2021 · 12 comments

Comments

@samcarter
Copy link
Member

Brief outline of the bug

Since commit 65da52f there seems to be a problem with transparent fadings if the cmyk colour model is used. In other colour models the following mwe would result in a transparent rectangle, but in cmyk it gives a baby pink shape.

Minimal working example (MWE)

\documentclass{standalone}

\usepackage{pgf}
\selectcolormodel{cmyk}

\pgfdeclareverticalshading{myshade}{3cm}{%
  color(0ex)=(pgftransparent!100);%
  color(1ex)=(pgftransparent!100)
}
\pgfdeclarefading{mymask}{\pgfuseshading{myshade}}

\begin{document}

\fbox{\begin{pgfpicture}
  \pgfsetfillcolor{red}%
  \pgfpathrectangle{\pgfpoint{0pt}{-1ex}}{\pgfpoint{3cm}{1.2ex}}%
  \pgfsetfading{mymask}{\pgftransformscale{2}}%
  \pgfusepath{fill}%
\end{pgfpicture}}

\end{document}

document

(related issue from beamer josephwright/beamer#718 )

@hmenke
Copy link
Member

hmenke commented Oct 8, 2021

Before 65da52f there were no shadings in the CMYK colorspace. They were all RGB.

@hmenke
Copy link
Member

hmenke commented Oct 8, 2021

@dcpurton

@zauguin
Copy link
Contributor

zauguin commented Oct 8, 2021

At least for pdf.js and Poppler's cairo backend the underlying issue is that Luminosity softmasks are always calculated in RGB (independent of the colorspace specified for the transparency group) and the used CMYK to RGB conversion does not preserve black.

I fear that this might not be fixable from PGF.

@samcarter
Copy link
Member Author

@zauguin Thanks for all this information! This does indeed not sound very promising :(

@hmenke
Copy link
Member

hmenke commented Oct 8, 2021

As a workaround you can switch to rgb before declaring the shading and switch back to cmyk after, but that will of course put an RGB shading in your document and break the output intent if you are trying to generate PDF/A for example.

@samcarter
Copy link
Member Author

@hmenke Thanks for the suggestion. I'll look into this and see how far this gets me.

Shall I close this issue as it is essentially a viewer issue or do you want to keep this around in case other users stumble over this as well?

@dcpurton
Copy link
Contributor

dcpurton commented Oct 9, 2021

I remain very unconvinced about the tikz fading code. If I'm printing I flatten any transparent fadings.

The above MWE still exhibits the same problems as issue #824 for me. And RGB does not help.

I do admit that I made no attempt to deal with transparency in my CMYK and grayscale shading code.

FWIW, these viewers display a transparent rectangle: Adobe*, mupdf, GhostScript.

And in addition to @zauguin's comments, these viewers/libraries have trouble: chromium/pdfium, Scribus PDF library

@dcpurton
Copy link
Contributor

dcpurton commented Oct 9, 2021

I reckon it would be useful to compare analogous PDF produced by Adobe Illustrator and whether this works across common viewers.

Attached is a fading produced by Scribus that runs from 90% transparent Magenta through to 100% transparent Magenta. This displays correctly in all viewers (i.e., right hand side really is completely transparent) and doesn't crash Adobe Reader when converting to PostScript.

If pgf can produce PDF like this, then I think many problems will go away.

fading_test.pdf

@samcarter
Copy link
Member Author

@dcpurton Thanks for investigating!

I can confirm that your fading_test.pdf file works fine in mac preview.app, skim and the internal texstudio viewer (these are my 3 viewers with which I had the problem reported above)

@dcpurton
Copy link
Contributor

dcpurton commented Oct 9, 2021

Perhaps at the very least, the pre-defined fadings should be declared in grayscale and a note added to the manual that any new fadings should be defined in grayscale. Even if RGB works, I think grayscale is probably a better choice.

The colour model could be switched to gray at the beginning of pgflibraryfadings.code.tex and returned to previously saved colour model at the end and similarly for tikzfadingfrompicture and tikzfading in tikzlibraryfadings.code.tex.

e.g.,

diff --git a/tex/generic/pgf/frontendlayer/tikz/libraries/tikzlibraryfadings.code.tex b/tex/generic/pgf/frontendlayer/tikz/libraries/tikzlibraryfadings.code.tex
index bddc83cb..2b3dfde4 100644
--- a/tex/generic/pgf/frontendlayer/tikz/libraries/tikzlibraryfadings.code.tex
+++ b/tex/generic/pgf/frontendlayer/tikz/libraries/tikzlibraryfadings.code.tex
@@ -18,11 +18,14 @@
 \def\tikzfadingfrompicture{%
   \begingroup%
     \setbox\pgfpic=\hbox\bgroup%
+      \XC@sdef\pgf@saved@color@model{\XC@tgt@mod{natural}}%
+      \selectcolormodel{gray}
       \tikzpicture%
 }%
 \def\endtikzfadingfrompicture{%
         \global\let\tikz@smuggle=\tikz@fig@name%
       \endtikzpicture
+      \selectcolormodel{\pgf@saved@color@model}%
     \egroup%
     \pgfdeclarefading{\tikz@smuggle}{\box\pgfpic}%
   \endgroup%
@@ -37,11 +40,14 @@
     \setbox\pgfpic=\hbox{%
       \tikzset{#1}%
       \global\let\tikz@smuggle=\tikz@fig@name%
+      \XC@sdef\pgf@saved@color@model{\XC@tgt@mod{natural}}%
+      \selectcolormodel{gray}
       \tikz{%
         \useasboundingbox(0,0) (100bp,100bp);
         \node at (50bp,50bp) [rotate=\tikz@shade@angle]
         {\pgfuseshading{\tikz@shading}};
       }%
+      \selectcolormodel{\pgf@saved@color@model}%
     }%
     \pgfdeclarefading{\tikz@smuggle}{\box\pgfpic}%
   }%
diff --git a/tex/generic/pgf/libraries/pgflibraryfadings.code.tex b/tex/generic/pgf/libraries/pgflibraryfadings.code.tex
index c8ce556b..ba08a46b 100644
--- a/tex/generic/pgf/libraries/pgflibraryfadings.code.tex
+++ b/tex/generic/pgf/libraries/pgflibraryfadings.code.tex
@@ -10,6 +10,11 @@
 \ProvidesFileRCS{pgflibraryfadings.code.tex}
 
 
+% Fadings must be defined in grayscale
+
+\XC@sdef\pgf@saved@color@model{\XC@tgt@mod{natural}}%
+\selectcolormodel{gray}%
+
 % Axial fadings
 
 \pgfdeclarehorizontalshading{pgf@lib@fade@east}{100bp}
@@ -55,3 +60,5 @@
   color(0pt)=(pgftransparent!100); color(21.25bp)=(pgftransparent!100); color(23.125bp)=(pgftransparent!0);
   color(25bp)=(pgftransparent!100); color(50bp)=(pgftransparent!100)}%
 \pgfdeclarefading{fuzzy ring 15 percent}{\pgfuseshading{tikz@lib@fade@fuzzy@15}}%
+
+\selectcolormodel{\pgf@saved@color@model}%

This doesn't fix everything (in particular Adobe Reader still crashes for me and it won't help if you build your fading using the pgf primitives), but at least it allows the standard tikz interface to work in an expected fashion with whatever viewer:

\documentclass{article}
\pagestyle{empty}
\usepackage[cmyk]{xcolor}
\usepackage{tikz}
\usetikzlibrary{fadings,patterns}

\tikzfading[name=mymask,
  left color=transparent!90,
  right color=transparent!100]

\begin{document}
\begin{tikzpicture}
  \path[fill=magenta, path fading=mymask] (0,0) rectangle (5,5);
\end{tikzpicture}

\begin{tikzpicture}
  \path[fill=magenta, path fading=east] (0,0) rectangle (5,5);
\end{tikzpicture}

\begin{tikzfadingfrompicture}[name=fade right with circle]
  \shade[left color=transparent!0,
    right color=transparent!100] (0,0) rectangle (2,2);
  \fill[transparent!50] (1,1) circle (0.7);
\end{tikzfadingfrompicture}
% Now we use the fading in another picture:
\begin{tikzpicture}
  % Background
  \fill [black!20] (-1.2,-1.2) rectangle (1.2,1.2);
  \pattern [pattern=checkerboard,pattern color=black!30]
    (-1.2,-1.2) rectangle (1.2,1.2);
  \fill [path fading=fade right with circle,cyan] (-1,-1) rectangle (1,1);
\end{tikzpicture}
\end{document}

tex279

@dcpurton
Copy link
Contributor

dcpurton commented Oct 9, 2021

OK, slight lie. Scribus and Inkscape (internal and Poppler/Cairo) still have problems importing this PDF. (Inkscape does not have problems importing the fadings_test.pdf file from Scribus attached above.)

But everything else I test views it OK (Adobe*, Chromium, Firefox, GhostScript, mupdf, poppler based viewers).

@samcarter
Copy link
Member Author

samcarter commented Oct 9, 2021

Not very surprised that inkscapes misbehaves :) I love the program, but it often does not cope well with transparency/fadings/shadings. I've had problems with pdfs from all kind of sources...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants