CMYK与RGB相互转换(java)

package util;

import java.awt.*;
import javax.swing.*;
import java.awt.color.*;

public class ColorTranslate {
  ICC_Profile ICC_pf;
  ICC_ColorSpace ICC_ClSpace;
//以下变量存储CMYK颜色值,取值为0到100
  int C = 9;
  int M = 9;
  int Y = 9;
  int K = 9;

//初始化ICC_Profile和ICC_ColorSpace类对象
  public ColorTranslate() {
    String Filename = "CMYK.pf";
    GetICCFrompfFile(Filename);
  }

  void GetICCFrompfFile(String Filename) {
    try {
      ICC_pf = ICC_Profile.getInstance(Filename);
    }
    catch (Exception e) {
      JOptionPane.showMessageDialog(null,
                                    "Can''t create ICC_Profile");
    }
    ICC_ClSpace = new ICC_ColorSpace(ICC_pf);
  }

  //由RGB色彩空间变换到CMYK
  public float[] RGBtoCMYK(Color RGBColor) {
    float[] CMYKfloat = ICC_ClSpace.fromRGB
        (RGBColor.getRGBComponents(null));
    C = (int) (CMYKfloat[0] * 100);
    M = (int) (CMYKfloat[1] * 100);
    Y = (int) (CMYKfloat[2] * 100);
    K = (int) (CMYKfloat[3] * 100);
    return CMYKfloat;
  }

//由CMYK色彩空间变换到RGB
  public Color CMYKtoRGB(float[] CMYKfloat) {
    Color RGBColor = new Color(ICC_ClSpace,
                               CMYKfloat, 1.0f);
    return RGBColor;
  }

  public Color CMYKtoRGB() {
    float[] CMYKfloat = new float[4];
    CMYKfloat[0] = 0.01f * (float) C;
    CMYKfloat[1] = 0.01f * (float) M;
    CMYKfloat[2] = 0.01f * (float) Y;
    CMYKfloat[3] = 0.01f * (float) K;
    Color RGBColor = new Color(ICC_ClSpace, CMYKfloat, 1.0f);
    return RGBColor;
  }
}

需要下载CMYK.pf文件 

//ColorTranslate CMYKtoRGB = new ColorTranslate();
//float[] CMYKfloat = new float[4];
//CMYKfloat[0] = 0.01f * (float) Integer.valueOf(strCMYK.substring(0,2),16).intValue();//C
//CMYKfloat[1] = 0.01f * (float) Integer.valueOf(strCMYK.substring(2,4),16).intValue();//M
//CMYKfloat[2] = 0.01f * (float) Integer.valueOf(strCMYK.substring(4,6),16).intValue();//Y
//CMYKfloat[3] = 0.01f * (float) Integer.valueOf(strCMYK.substring(6,8),16).intValue();//K
//Color color = CMYKtoRGB.CMYKtoRGB(CMYKfloat);
//color.getRED(); 

展开阅读全文

没有更多推荐了,返回首页