Colour can be a complicated topic in PDF because not only can you have lots of different types of color in PDF (Lab, RGB, CMYK, etc), but you can even have different types of RGB and CMYK. This article is part explanation of how color can work in a PDF file and part tutorial on some tweaks you can use to improve display. Thank-you to Random House USA for permission to use their file.
Color can generally be defined using an ICC colorspace file. This is a standard format devised by the ICC who have a website at the appropriately named site http://www.color.org/. If you are seriously interested in Color, they have lots of resources.
In Java you can load an ICC colorspace very easily with the code. This code example shows how you could setup a Colorspace using an ICC example. ICC colorspaces can match different types of Colorspace (so they are either compatible with CMYK or RGB or GRAY or Lab – you cannot create an RGB colorspace with a CMYK ICC profile). You can also use a Color profile( ends .pf) to create a Colorspace with the same code.
String profile="path to RGB icc file"; ColorSpace rgbCS=null; if(profile!=null) rgbCS=new ICC_ColorSpace(ICC_Profile.getInstance(new FileInputStream(profile)); else rgbCS=ColorSpace.getInstance(ColorSpace.CS_sRGB);
Now let us have a look at an example file…
Here is a PDF page in Adobe Acrobat 9.0
This is where Color gets complicated because there are different ways to convert YCCK to CMYK – a fast approximate formula and a more accurate, slower way using a YCC color profile and a CMY color profile (K is unchanged so does not need translation). By default we us the fast approximate way in our Java PDF viewer. Here is the same page.
Not bad but the colors are a little brighter than in the original. So lets use a profile conversion (in our PDF viewer we have a JVM option to set the 2 profiles needed for the conversion (-Dorg.jpedal.useICC=”YCC=/Users/markee/colorprofiles/YCC601.pf, CMY=/Users/markee/colorprofiles/CMY.pf” ).
It is closer but still clear differences. This is because we convert images into sRGB in Java (and there are different versions of RGB). It turns out Adobe is using a variation of RGB and a close match is CIERGB (the icc file is available to download from their site). So let us use CIERGB instead or sRGB. In our viewer we set the flag -Dorg.jpedal.RGBprofile=/Users/markee/Downloads/CIERGB.icc
That looks better! Let’s see a montage of sRGB (left), Adobe (middle), and CIERGB (right).
This post is part of our “Understanding the PDF File Format” series. In each article, we discuss a PDF feature, bug, gotcha or tip. If you wish to learn more about PDF, we have 13 years worth of PDF knowledge and tips, so click here to visit our series index!
Latest posts by Mark Stephens (see all)
- 4 ways Companies can make remote working successful - December 21, 2017
- My experience of a Turkish bath (visiting Istanbul for DevFest) - November 24, 2017
- My 5 key takeaways from JavaOne 2017 - October 6, 2017
- My notes and pictures from thursday JavaOne 2017 - October 5, 2017
- My notes and pictures from Wednesday JavaOne 2017 - October 5, 2017