iamaamir

Convert RGB to CMYK and CMYK to RGB

Sep 30th, 2014
1,332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. /**
  2.  *<author>
  3.  * @author Aamir khan
  4.  * @URL    aamir-4u.blogspot.com
  5.  *</author>
  6.  * <b>RGB to CMYK conversion formula</b>
  7.  *
  8.  * <p>The R,G,B values are divided by 255 to change the range from 0..255 to 0..1:
  9.  *
  10.  * R1 = R/255
  11.  *
  12.  * G1 = G/255
  13.  *
  14.  * B1 = B/255
  15.  *
  16.  * The black key (K) color is calculated from the red (R1,G1,B1)
  17.  *
  18.  * K = 1-max(R1, G1, B1)
  19.  *
  20.  * The cyan color (C) is calculated from the red (R1) and black (K) colors:
  21.  *
  22.  * C = (1-R1-K) / (1-K)
  23.  *
  24.  * The magenta color (M) is calculated from the green (G1) and black (K) colors:
  25.  *
  26.  * M = (1-G1-K) / (1-K)
  27.  *
  28.  * The yellow color (Y) is calculated from the blue (B1) and black (K) colors:
  29.  *
  30.  * Y = (1-B1-K) / (1-K)<p>
  31.  */
  32. public class RGBtoCMYK {
  33.    
  34.  
  35.     public static void main(String argu[]) {
  36.         double r = 2, g = 25, b = 255;
  37.         double r1 = r / 255, g1 = g / 255, b1 = b / 255;
  38.         double c, m, y, k;
  39.         c = m = y = k = 0;//just initialize
  40.  
  41.         /*the black key Color (k) is Calculated from r1,g1,b1
  42.          so k = 1- max(r1,g1,b1)
  43.          */
  44.         k = 1 - max(r1, g1, b1);
  45. //The Cyan
  46.         c = (1 - r1 - k) / (1 - k);
  47. //The Magento
  48.         m = (1 - g1 - k) / (1 - k);
  49. //The Yellow
  50.         y = (1 - b1 - k) / (1 - k);
  51. //Format the Output
  52.         java.text.DecimalFormat p = new java.text.DecimalFormat("#.###");
  53. //Printing the Values
  54.         System.out.println("C = "+p.format(c));
  55.         System.out.println("M = "+p.format(m));
  56.         System.out.println("Y = "+p.format(y));
  57.         System.out.println("K = "+p.format(k));
  58. //Check the Values
  59.         System.out.println("Check______________________");
  60.         toRGB(c, m, y, k);
  61.  
  62.     }//main end
  63.  
  64.     public static double max(double a, double b, double c) {
  65.  
  66.         double max = a;
  67.         if (b > max) {
  68.             max = b;
  69.         }
  70.         if (c > max) {
  71.             max = c;
  72.         }
  73.         return max;
  74.  
  75.     }//max end
  76.  
  77.     public static void toRGB(double c, double m, double y, double k) {
  78.  
  79.         /*
  80.          <b>CMYK to RGB conversion formula</b>
  81.  
  82.          <p>The R,G,B values are given in the range of 0..255.
  83.  
  84.          The red (R) color is calculated from the cyan (C) and black (K) colors:
  85.  
  86.          R = 255 × (1-C) × (1-K)
  87.  
  88.          The green color (G) is calculated from the magenta (M) and black (K) colors:
  89.  
  90.          G = 255 × (1-M) × (1-K)
  91.  
  92.          The blue color (B) is calculated from the yellow (Y) and black (K) colors:
  93.  
  94.          B = 255 × (1-Y) × (1-K)
  95.         </p>
  96.          */
  97.        
  98.         double r = 255 * (1-c) * (1-k);
  99.         double g = 255 * (1-m) * (1-k);
  100.         double b = 255 * (1-y) * (1-k);
  101.         System.out.printf("R = %1.0f\n", r);
  102.         System.out.printf("G = %1.0f\n", g);
  103.         System.out.printf("B = %1.0f\n", b);
  104.     }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment