halostatue / color

Color tools for Ruby.
Other
133 stars 41 forks source link

Add color profiles or types for the CMYK conversion #9

Closed acaron closed 5 years ago

acaron commented 10 years ago

The current RGB to CMYK conversion is using an undercolor removal to obtain a good ink representation. I need a different profile to match the values of this tool: https://kuler.adobe.com/create/color-wheel/ (the current values are far from this tool)

The following formula gives good results (not 100% accurate since no profile is specified, but an API or something is need to really match a profile): http://www.codeproject.com/Articles/4488/XCmyk-CMYK-to-RGB-Calculator-with-source-code

I can send a pull request if you tell me how do you want your code to handle this issue.

Here's a short snippet to represent the formula:

def to_cmyk
  c = 1.0 - @r.to_f
  m = 1.0 - @g.to_f
  y = 1.0 - @b.to_f

  k = [c, m, y].min
  reverse_k = 1.0 - k

  if reverse_k
    c = (c - 1.0) / reverse_k
    m = (m - 1.0) / reverse_k
    k = (k - 1.0) / reverse_k
  else
    c = m = y = 0.0
  end

  Color::CMYK.from_fraction(c, m, y, k)
end
halostatue commented 10 years ago

You raise a great question that I don't have an answer for immediately. As you say, the under colour removal functionality isn't wrong, just different (and I found—without looking much—a different CMYK formula, too).

I have another formula fix in process—for YIQ—that I'm targeting for a 2.0 release (permits breaking changes) that I tentatively set for end of July—but that may slip (and if it does, it slips until September, as I'm taking vacation in August). If you need this sooner than that, we can look at a 1.7 release that contains a Color::RGB#to_cmyk with a parameter (e.g., rgb.to_cmyk(:kuler) would call Color::RGB#to_cmyk_kuler as you describe above). No, I wouldn't call the formula kuler, but I don't have a better name off the top of my head.

I also plan on adding at least rudimentary colour space transformations—we have a basic transformation for sRGB to L_a_b* already in place, but it's not easily accessible and @daveheitzman has added more (that I have yet to review) based on some other work. I'm also, as inspired by the YIQ bug fix PR, to use more Matrix math, since that's more or less how all colour space translations work. That's all planned for 2.0 (I have two other OSS projects that need some love before I can focus on colour again).

Even when we do colour space transformations, the different CMYK approximations will still probably end up existing—we just have to figure out how to expose them well.

So yes, I'm interested…when do you need something by?

acaron commented 10 years ago

I will dig more on the color theory tonight, but I found a nice description on Wikipedia: "Since RGB and CMYK spaces are both device-dependent spaces, there is no simple or general conversion formula that converts between them. Conversions are generally done through color management systems, using color profiles that describe the spaces being converted. Nevertheless, the conversions cannot be exact, particularly where these spaces have different gamuts." (http://en.wikipedia.org/wiki/CMYK_color_model). On the ICC profile page, it talks about mappings, interpolation and transformations to obtain a profile connection space (PCS) so what you are currently implementing is useful!

Probably by setting a default profile and implementing 2 or 3 (the most popular like sRGB and Adobe RGB) can be good! I don't know if it is difficult or not to do it and how much it will add to the code (you probably want to keep the gem simple) but I will search for this tonight and keep you updated. I know you can download the .icc files and use them. Imagemagick can probably help too. This page has a lot of useful informations: http://dpbestflow.org/color/color-space-and-color-profiles and here: http://www.color.org/icc-book1.PDF

halostatue commented 10 years ago

One other place to look—and in my “research” folder for color is LCMS (little colour management system).

halostatue commented 10 years ago

I've started work toward color-2; if you want to contribute toward this to help improve the CMYK conversions by providing alternative algorithms, I would love to see them as a PR against the color-2 branch I have going.