galkahana / HummusJS

Node.js module for high performance creation, modification and parsing of PDF files and streams
http://www.pdfhummus.com
Other
1.15k stars 169 forks source link

Usability issue while working in CMYK #312

Open AlexanderGalen opened 6 years ago

AlexanderGalen commented 6 years ago

The way that the options object expects color definitions works great for RGB, since it's just hex and that's pretty standard, however if you're using a CMYK color, it can be really cumbersome to get the value in a form that it accepts.

For example, if I have a simple CMYK color of cyan at 50%, and everything else at 0%, the equivalent RGB hex would be #6dcff6, but if you pass that to the options as is, you don't get the light blue color.

Instead you need to convert each individual color channel percentage value into a value between 0 and 255, then convert that value to hex, then use each of those one after another as your hex value, ending with a result of #80000000, which you then pass to the options as 0x80000000.

Using 0x80000000 as the value for color in the options object gives me the correct color, but as you can see it takes quite a bit of effort to get there.

Are there other options for passing a CMYK color to the options? Using hex doesn't fit really well with CMYK since the channel values are percentage based rather than a value between 0 and 255.

I think the simplest way to store a CMYK color would be an array of 4 integers, each < 100. Do you think there is a way to allow for a different type of value like that?

I could be interested in contributing if you can point me in the right direction.

AlexanderGalen commented 6 years ago

I just did a bit more digging after opening this and found something that is a pretty easy workaround for this:

k(inC,inM,inY,inK);

The color operator k accepts an individual parameter for each channel, and expect the values to be between 0 and 1, so .5 would be 50%. This does basically everything I was hoping for, except for the fact that it isn't just a part of the options and requires a separate context update. I'll use this workaround for the time being, though I still think it'd be good to have a nicer way to provide CMYK colors as part of the options.

Also, quick question because I couldn't seem to find any info on it, what's the difference between uppercase and lowercase operators k and K? Using uppercase K just seems to give me 100% black and no other channels no matter what I pass as arguments

AlexanderGalen commented 6 years ago

Never mind my question actually, I found my answer in the official PDF reference. Uppercase is for stroke, lowercase is for fill.