How to convert CMYK to RGB and RGB to CMYK in Java

RGB (Red Green Blue) are the colors i guess we all know most of the web developers works with rgb and hex colors.

CMYK (Cyan, Magenta, Yellow, Black) CMYK are the colors used in printing.

Most of the Graphic softwares such as Photoshop, gimp etc are actually able to work with both models.

The point of this post is simply tell you the process of transforming RGB to CMYK or reverse.

Let's start with RGB to CMYK.

RGB to CMYK conversion formula

The R,G,B values are divided by 255 to change the range from 0..255 to 0..1

R1 = R/255
G1 = G/255
B1 = B/255

The black key (K) color is calculated from the red (R1,G1,B1)
K = 1-max(R1, G1, B1)

The cyan color (C) is calculated from the red (R1) and black (K) colors:
C = (1-R1-K) / (1-K)

The magenta color (M) is calculated from the green (G1) and black (K) colors:
M = (1-G1-K) / (1-K)

The yellow color (Y) is calculated from the blue (B1) and black (K) colors:
Y = (1-B1-K) / (1-K)

lets implement this into code
public CMYK toCMYK() {

     double c, m, y, k;
     double r = 2, g = 25, b = 255;
     double r1 =  r / 255, g1 = g / 255, b1 = b / 255;
     
     k = 1 - max(r1, g1, b1);
     c = (1 - r1 - k) / (1 - k);
     m = (1 - g1 - k) / (1 - k);
     y = (1 - b1 - k) / (1 - k);
     return new CMYK(c, m, y, k);

}


so now lets look into CMYK to RGB

CMYK to RGB conversion formula

The R,G,B values are given in the range of 0 - 255.

The red (R) color is calculated from the cyan (C) and black (K)
colors:
R = 255 × (1-C) × (1-K)

The green color (G) is calculated from the magenta (M) and black
(K) colors:
G = 255 × (1-M) × (1-K)

The blue color (B) is calculated from the yellow (Y) and black
(K) colors:
B = 255 × (1-Y) × (1-K)

And here is the code.

public RGB toRGB() {

      double r = 255 * (1 - c) * (1 - k);
      double g = 255 * (1 - m) * (1 - k);
      double b = 255 * (1 - y) * (1 - k);

      return new RGB(r, g, b);
}


All right, so now hopefully we understand the conversion, let's see everything in action

Full code

package chap09;

/**
 *
 * @author Aamir khan @hacktw
 * @version 1.2
 *
 */
public class RGBtoCMYK {

    public static void main(String argu[]) {
        new RGBtoCMYK().run();
    }//main end

    private void run() {
        RGB rgb = new RGB(255, 0, 255);
        CMYK cmyk = new CMYK(0, 1, 0, 0);

        System.out.println(rgb.toCMYK());
        System.out.println(cmyk.toRGB());
    }

    class CMYK {

        double c, m, y, k;

        public CMYK(double c, double m, double y, double k) {
            if (c > 1 || m > 1 || y > 1 || k > 1) {
                System.err.print("make sure CMYK values are in range 0-1");
                System.exit(0);
            }

            this.c = c;
            this.m = m;
            this.y = y;
            this.k = k;
        }

        public RGB toRGB() {

            /**
             * <b>CMYK to RGB conversion formula</b>
             *
             * <p>
             * The R,G,B values are given in the range of 0..255.
             *
             * The red (R) color is calculated from the cyan (C) and black (K)
             * colors:
             *
             * R = 255 × (1-C) × (1-K)
             *
             * The green color (G) is calculated from the magenta (M) and black
             * (K) colors:
             *
             * G = 255 × (1-M) × (1-K)
             *
             * The blue color (B) is calculated from the yellow (Y) and black
             * (K) colors:
             *
             * B = 255 × (1-Y) × (1-K)
             * </p>
             *
             */
            double r = 255 * (1 - c) * (1 - k);
            double g = 255 * (1 - m) * (1 - k);
            double b = 255 * (1 - y) * (1 - k);

            return new RGB(r, g, b);
        }

        @Override
        public String toString() {
            //Format the Output
            java.text.DecimalFormat p = new java.text.DecimalFormat("#.###");
            return "Cyan " + p.format(c)
                    + "nMagenta " + p.format(m)
                    + "nYellow " + p.format(y)
                    + "nBlack " + p.format(k);
        }

    }

    class RGB {

        double r, g, b;

        public RGB(double r, double g, double b) {
            if (r > 255 || g > 255 || b > 255) {
                System.err.print("make sure RGB values are in range 0-255");
                System.exit(0);
            }
            this.r = r;
            this.g = g;
            this.b = b;
        }

        /** <b>RGB to CMYK conversion formula</b>
        *
        * <p>
        * The R,G,B values are divided by 255 to change the range from 0..255 to 0..1:
        *
        * R1 = R/255
        *
        * G1 = G/255
        *
        * B1 = B/255
        *
        * The black key (K) color is calculated from the red (R1,G1,B1)
        *
        * K = 1-max(R1, G1, B1)
        *
        * The cyan color (C) is calculated from the red (R1) and black (K) colors:
        *
        * C = (1-R1-K) / (1-K)
        *
        * The magenta color (M) is calculated from the green (G1) and black (K) colors:
        *
        * M = (1-G1-K) / (1-K)
        *
        * The yellow color (Y) is calculated from the blue (B1) and black (K) colors:
        *
        * Y = (1-B1-K) / (1-K)
        * </p>
        **/
        public CMYK toCMYK() {

            double c, m, y, k;

            double r1 = r / 255, g1 = g / 255, b1 = b / 255;

            /*the black key Color (k) is Calculated from r1,g1,b1
                so k = 1- max(r1,g1,b1)
             */
            k = 1 - max(r1, g1, b1);
            //The Cyan
            c = (1 - r1 - k) / (1 - k);
            //The Magento
            m = (1 - g1 - k) / (1 - k);
            //The Yellow
            y = (1 - b1 - k) / (1 - k);

            return new CMYK(c, m, y, k);

        }

        public double max(double a, double b, double c) {

            double max = a;
            if (b > max) {
                max = b;
            }
            if (c > max) {
                max = c;
            }
            return max;

        }//max end

        @Override
        public String toString() {
            return String.format("rgb(%1.0f, %1.0f, %1.0f)", r, g, b);
        }

    }

}


output:
Cyan 0
Magenta 1
Yellow 0
Black 0
rgb(255, 0, 255)

No comments :

Post a Comment