Ms.Sahin
Ms.Sahin

Reputation: 101

Draw CMYK Color from RGB

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));
}

Upvotes: 3

Views: 2478

Answers (2)

Lonzak
Lonzak

Reputation: 9816

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);
    

Upvotes: 0

Derek Hunter
Derek Hunter

Reputation: 191

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.

Upvotes: 3

Related Questions