XeLaTeX and PDF color change

You may have noticed that sometimes when you create a PDF with XeLaTeX, the colors of your graphics look different when you open the PDF in Adobe Reader. What you see is graphics that were originally in the RGB color model, being displayed as if they were in CMYK instead. The reason for this is a bug (or feature?) in Adobe Reader that causes it to default to the CMYK color model when it encounters graphics with transparency, unless the PDF tells it explicitly to display the graphics in RGB.

I found the following fix for this unwanted color conversion here, which carries out a textual replacement in the PDF file to declare the color scheme as RGB  on every page:

#!/bin/sh
 
# Acrobat Reader shows transpant stuff in CMYK
# You have to tell it to render things in RGB
 
# for pdflatex
# put this in the preamble of your document:
#\pdfpageattr {/Group << /S /Transparency /I true /CS /DeviceRGB>>}
 
# for xelatex
# run this script on the pdf output
sed -i 's/^\/Type\/Page$/\/Type\/Page\n\/Group \<\< \/S \/Transparency \/I true \/CS \/DeviceRGB\>\>\n\/Trans \<\< \/S \/R \>\>\n/' $1

This is an easy fix under Linux, where sed is readily available, but can be cumbersome for Windows users who are not too familiar with a command line interface or regular expressions. Therefore I made a small (CLI) program that carries out this replacement, which can also be invoked by simply dragging and dropping a PDF onto the executable. It is open source and freely available at SourceForge.

I found another fix in a StackExchange thread. With this fix, you declare an RGB color scheme in your XeLaTeX file by including the following code snippet:

\makeatletter%
\special{pdf: put @thispage <</Group << /S /Transparency /I true /CS /DeviceRGB>> >>}%
\makeatother%

A disadvantage to this fix, however, is that this code only applies to the current page. Table 2 of dvipdfm‘s documentation does not suggest a quick solution for this. Perhaps addressing pages by their page number (for which the aforementioned table lists a variable @pagen) and then constructing a loop at the end of the document could work, but I was not able to access any such variable.