C#: How to get the color value (in CMYK) at a position on PDF page?
I have a CMYK color PDF file (with one page and image only, no text). I want to get the CMYK color value of a 'pixel' in this PDF file (I call a position as 'pixel'), currently I do it as follow:
- Convert the PDF page to bitmap <br>
- Get the color value of this pixel:
Color cPixel = bmpImage.GetPixel(x, y);
Finally, I convert the cPixel (RGB) into CMYK value, however the value is not correct :( (in original file it's 100%K, but the result is very different). I think the problem comes from my processing: CMYK file => Processing in RGB (convert to bitmap, read color in RGB) => Convert result into CMYK. The formula is used to convert fr开发者_运维问答om RGB->CMYK is:
Black = minimum(1-Red,1-Green,1-Blue)
Cyan = (1-Red-Black)/(1-Black)
Magenta = (1-Green-Black)/(1-Black)
Yellow = (1-Blue-Black)/(1-Black)
Now, I want to read CMYK value directly from PDF, please help me if you have any ideas! Thanks in advance for reading and any supporting.
Thank you so much!!!
These formulas to convert from RGB to CMYK (and similar formulas for the opposite conversion) are pasted all over the internet but give very poor results because they assume that the color systems are linear systems. This is very far from reality. Not surprisingly, the resulting colors resemble the original colors only very remotely. For better results, you'll have to use color profiles (you can use device independent profiles). Color conversions are fully supported by Windows functions.
Unfortunately, the functions for the conversion from RGB to CMYK with a color profile aren't directly exposed in C#. But you can find a complete solution in this answer.
The conversion from CMYK to RGB with a color profile is easier to achieve in C#. The solution is in this answer. Before implementing it, you can try it online.
I don't know much about colour science but try checking the formulas in EasyRGB for reference: RGB to CMY and then CMY to CMYK. I think those formulas are only an approximation though because colour profiles affect how it looks visually. See this post about using those formulas.
Edit:
According to this, your formula looks correct. Just make sure that the values for RGB are normalised (i.e. they are between 0 and 1.0).
The best solution would be to execute the page content stream, determine where on the page is the image located, see what image pixel corresponds to your page position and then extract that pixel directly from the CMYK image. All the conversions you perform introduce small errors that add up and this is why your final result is different from what you expect. The formulas you used give you an approximate conversion. RGB and CMYK are device dependent colorspaces, a more accurate conversion goes through an intermediate device independent colorspace such as Lab. So CMYK -> RGB becomes CMYK -> Lab -> RGB and viceversa. More discussions on this you can read here: http://forums.adobe.com/thread/428899 and here: http://en.wikipedia.org/wiki/Lab_color_space
精彩评论