1

This is related to:

Converting (any) PDF to black (K)-only CMYK

Hi

first please sorry for my english.

This related link have 50% of the solution to my problem. The only thing which is left is I also need the magenta color to be 100% Magenta.

Here the scenario:

I have a html like this:

<font color="magenta">Hello </font>
<font color="#000000"> World </font>

1- I convert it with:

/usr/bin/wkhtmltopdf file.html output1.pdf

2 - convert the black text to 100% k:

gs \
   -dNOPAUSE \
   -dBATCH \
   -sDEVICE=ps2write \
   -sOutputFile=output1.ps \
    output1.pdf

# PS to PDF using replacement function in HackRGB-cmyk-inv.ps
gs \
   -dNOPAUSE \
   -dBATCH \
   -sDEVICE=pdfwrite \
   -sOutputFile=output2.pdf \
    /HackRGB-cmyk-inv.ps \
    output1.ps

Now I have a output2.pdf where the black text is 100% K but the magenta is not 100% M...

Here are the content of the HackRGB-cmyk-ink.ps(postscript) for reference:

%!
/oldsetrgbcolor /setrgbcolor load def
/setrgbcolor {
(in replacement setrgbcolor\n) print
                                %% R G B
  1 index 1 index       %% R G B G B
  eq {                  %%
     2 index 1 index    %% R G B R B
     eq {
                        %% Here if R = G = B
      pop pop           %% remove two values
      % setgray % "replace the 'setgray' with":
      0 0 0 4 -1 roll % setcmykcolor
      -1 mul          %% obtain -R on top of stack
      1 add           %% obtain 1-R on top of stack
      setcmykcolor    %% now set(cmykcolor) K (as 1-R)
     } {
       oldsetrgbcolor   %% set the RGB values
     } ifelse
  }{
    oldsetrgbcolor      %% Set the RGB values
  }ifelse

} bind def
/oldsetgray /setgray load def
/setgray {
(in replacement setgray\n) print
  % == % debug: pop last element and print it
  % here we're at a gray value;
  % http://www.tailrecursive.org/postscript/operators.html#setcymkcolor
  % setgray: "gray-value must be a number from 0 (black) to 1 (white)."
  % setcymkcolor: "The components must be between 0 (none) to 1 (full)."
  % so convert here again:
  0 0 0 4 -1 roll % push CMY:000 after Gray and roll down,
                  % so top of stack becomes
                  % ...:C:M:Y:Gray
  -1 mul          %% obtain -Gray on top of stack
  1 add           %% obtain 1-Gray on top of stack
  setcmykcolor    %% now set(cmykcolor) K (as 1-Gray)
} bind def


%~ # test: rgb2gray
%~ gs -dNOPAUSE -dBATCH -sDEVICE=ps2write -sOutputFile=./blah-slide-hackRGB-gray.ps ./HackRGB.ps ./blah-slide-gsps2w.ps
%~ # gray2cmyk
%~ gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=./blah-slide-hackRGB-gray-ci.pdf ./HackRGB-cmyk-inv.ps ./blah-slide-hackRGB-gray.ps
%~ # check separations - looks OK
%~ gs -sDEVICE=tiffsep -dNOPAUSE -dBATCH -dSAFER -dFirstPage=1 -dLastPage=1 -sOutputFile=p%02d.tif blah-slide-hackRGB-gray-ci.pdf && eog p01.tif 2>/dev/null

Some ideia on how to do it?

Regards.

Community
  • 1
  • 1
ricardo
  • 1,221
  • 2
  • 21
  • 39
  • It probably depends on what's in 'HackRGB-cmyk-inv.ps – KenS Feb 27 '13 at 16:20
  • yep I am now trying to learn postscript to edit the hackRGB-cmyk-ink.ps I will put the content of the hackRGB-cmyk-ink.ps file here for reference – ricardo Feb 27 '13 at 16:43
  • Small matter: “-1 mul” is more elegantly done as “neg”. So “neg 1 add” or, for one more character, “1 exch sub”. The answers will be identical, even though PostScript’s numeric precision is only single, as is confirmed in my Distiller by “(Start)= 32768 {rand 2147483647.0 div dup dup 1 exch sub exch neg 1 add eq {pop} {=} ifelse} repeat (Finish) =”. – jdaw1 Aug 18 '18 at 09:49

1 Answers1

1

Here is a quick hack to the hack. right after the oldsetrgbcolor you could check the cmyk color and modify it. Maybe tonight i can make a more general module, cut this will check for a 50% magenta and change it to 100%. The (cmyk-) print pstack line will display the cmyk colors found, you may need it if the calculated color isn't exactly .5, like it might be .49, so once you see the values, remove the line.

old

  }{
    oldsetrgbcolor      %% Set the RGB values
  }ifelse

new

  }{
    oldsetrgbcolor      %% Set the RGB values
    currentcmykcolor    %puts 4 numbers on the stack
    (cmyk-) print pstack %display the colors (remove when things work correctly)
    3 -1 roll           %put magenta on top of stack
    dup                 %make copy of magenta value
    .5                  %put magenta test value on stack (then may not be exactly .5, see pstack)
    eq                  %see of magenta is equal to test value (.5)
    {pop 1}if           %if it is equal, pop off the .5 and put a 1 onto the stack
    3 1 roll            %put magenta back where it belongs in the stack
    setcmykcolor        %reset the cmyk to have new magenta value
  }ifelse
Fred F
  • 1,027
  • 1
  • 9
  • 18
  • I think you need `3 -1 roll` to put magenta on top, and `3 1 roll` to put it back. Or you could use `4 2 roll` for both. – luser droog Feb 27 '13 at 18:37
  • Dang, your right! Sorry, my dyslexia was kicking in. Thanks, I'll correct it. – Fred F Feb 27 '13 at 23:40
  • No problem. I'm a bit of a stack wiz, though I do say so myself. :) For tricksy stuff, I try to write in 3 columns: code % stack-picture ... explanation. That's where you can give names to all the unnamed stack data, and count out (visibly) all your `roll`s and `index`es. Eg. the xpose procedure [here](http://stackoverflow.com/a/14600918/733077) – luser droog Feb 28 '13 at 09:12
  • Yes, you sure have done your stack exercises! But let me encourage you, COMMENT YOUR CODE!!!! For code which performs such complex functions, if you want anyone else besides you to understand it, you really need to put comments on each function describing the purpose of the function and the inputs and outputs. It would also be nice to have a description of the various dictionaries and their contents. Other than that, VERY COOL!!!!! – Fred F Feb 28 '13 at 17:38
  • I did program in other languages like c++/ruby/applescript/php but this language (postscript) is totally crypt to me. Thanks you for the answers! I think I will learn postcript – ricardo Feb 28 '13 at 18:02
  • Unless you have a specific need to learn postscript, don't bother. There is VERY VERY VERY little demand for postscript and as you said, it is cryptic. For areas where there is a need, postscript is the only choice and you will be forced to learn it. HTML5 would be much more useful to learn as a graphics language. – Fred F Feb 28 '13 at 18:59
  • @FredF You're right. I should comment a lot more, especially when I post things. I just get so excited when something works that I want to share it right away. I'll definitely work on it. – luser droog Mar 02 '13 at 21:08
  • Hi Fred, you're real expert in gs, can you please look at my question(i need to keep black color) here http://stackoverflow.com/questions/35596346/ghostscript-convert-pdf-into-cmyk-with-black-point-compensation – Ivan Gusev Feb 26 '16 at 06:16