3

Hello. I have a screen like above. By using the sliders, I get red, green, blue. Also, I calculate cyan, magenta, yellow and from red, green, blue for CMYK. My question is that is there any way to show CMYK colour in java like light purple in the picture.

private void stateChanged() {
      red= sliderRed.getValue();
      green= sliderGreen.getValue();
      blue= sliderBlue.getValue();
      txt_background.setBackground(new Color(red, green, blue));
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
Ms.Sahin
  • 101
  • 8
  • Your question is unclear. All monitors are RGB. There is not a distinct 1-to-1 mapping to CMYK as that mapping depends on many factors such as rendering dot size, ink absorption spectrum, and a whole host of other factors that change rendering. Even then there can be more than one way to map RGB to CMYK, and then there's gamut considerations. It's not even clear what you want to do. If by _"show CMYK colour in java like light purple"_ you mean "show what the CMYK color would look like, but on the screen", this is completely impossible to do with any hope of accuracy. – Jim Garrison Mar 27 '17 at 19:29
  • @JimGarrison but my task is allowing color space conversion from RGB to CMYK and CMYK to RGB. Additionally implement the presentation of the user-selected color. – Ms.Sahin Mar 27 '17 at 19:32
  • Essentially a duplicate of http://stackoverflow.com/questions/4858131/rgb-to-cmyk-and-back-algorithm The answer would be to convert back and forth, as done in the accepted answer to that linked question (which is also in the list to the right, under "Related"). However, take notice of the comment from @JimGarrison and consider that carefully. – Loduwijk Mar 27 '17 at 19:35

2 Answers2

3

It looks to me like the java color class, has a constructor for making a color object in cmyk

https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#Color(java.awt.color.ColorSpace,%20float[],%20float)

and

https://docs.oracle.com/javase/7/docs/api/java/awt/color/ColorSpace.html

So you would end up with something like

Color cmykColorValue = new Color(TYPE_CMYK, [cValue, mValue, yValue, kValue], alpha)

Where alpha is form 0 to 1, and cValue, mValue, yValue, kValue are the corresponding cmyk values.

That should make a new CMYK color object that can be used anywhere a color object can be used.

Derek Hunter
  • 171
  • 3
  • But don't expect the rendered "CMYK" color on the screen to look anything like the printed color. CMYK is meaningless in an RGB display and is at best only somewhat close to the real color. – Jim Garrison Mar 27 '17 at 19:41
  • @JimGarrison You're right ultimately its all going to be converted down into RGB for rendering to the screen. It sounded to me like that task was to convert between the two, and he's mostly just drawing it back to the screen to make sure the conversion was correct. – Derek Hunter Mar 27 '17 at 19:45
  • I'm sure you're right, but _"drawing it back to the screen to make sure the conversion was correct"_ is pointless due to the mapping and gamut issues. It can look "correct" on screen but awfully bad on reflective media. – Jim Garrison Mar 27 '17 at 19:47
  • I mean.. it should be close enough for a quick visual inspection. If he has a blue color in rgb from the sliders, and he runs that through the conversion he shouldn't get a hot pink color when drawing the CMYK version of it. Like, you're correct that there are inaccuracies in the conversion but its likely that it will be 'close enough' – Derek Hunter Mar 27 '17 at 19:55
  • Is the `TYPE_CMYK` an instance of `ColorSpace`? – Jin Kwon Apr 17 '23 at 10:09
0

The correct usage of the Color constructor for CMYK is as follows:

java.awt.Color cmyk = new Color(ColorSpace.getInstance(ColorSpace.TYPE_CMYK), new float [] {cyan,magenta,yellow}, key/alpha);

Update: As Jin correctly mentioned there is no support in the ColorSpace.getInstance(ColorSpace.TYPE_CMYK) method in Java. (throws an Illegal argument Exception. I see three possibilities:

  1. Use the CMYKColorSpace from the jcgm project

  2. Apache XML Graphics Commons also has an implementation: org.apache.xmlgraphics.java2d.color.DeviceCMYKColorSpace

    ColorSpace cs = new DeviceCMYKColorSpace();
    float[] cmyk = cs.fromRGB(new float[] {0.1f, 0.5f, 0.9f});
    
  3. Use the CMYK color space by loading a CMYK ICC profile which then can be used to create a ColorSpace object.

    ICC_Profile cmykProfile = ICC_Profile.getInstance("path/to/cmyk/profile.icc");
    ColorSpace cmykColorSpace = new ICC_ColorSpace(cmykProfile);
    Color cmykColor = new Color(cmykColorSpace, new float[]{0.5f, 0.25f, 0.75f, 0.1f}, 1.0f);
    
Lonzak
  • 9,334
  • 5
  • 57
  • 88