View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.color;
18  
19  import static org.junit.Assert.assertEquals;
20  
21  import org.apache.commons.imaging.util.Debug;
22  import org.junit.Test;
23  
24  public class ColorConversionsTest {
25      private static final int SAMPLE_RGBS[] = { 0xffffffff, 0xff000000,
26              0xffff0000, 0xff00ff00, 0xff0000ff, 0xffff00ff, 0xfff0ff00,
27              0xff00ffff, 0x00000000, 0xff7f7f7f, };
28  
29      @Test
30      public void testRGBtoCMYK() throws Exception {
31          for (final int rgb : SAMPLE_RGBS) {
32              final ColorCmy cmy = ColorConversions.convertRGBtoCMY(rgb);
33              final ColorCmyk cmyk = ColorConversions.convertCMYtoCMYK(cmy);
34              final ColorCmy cmyk_cmy = ColorConversions.convertCMYKtoCMY(cmyk);
35              final int cmyk_cmy_rgb = ColorConversions.convertCMYtoRGB(cmyk_cmy);
36  
37              Debug.debug("cmy: " + cmy);
38              Debug.debug("cmyk: " + cmyk);
39              Debug.debug("cmyk_cmy: " + cmyk_cmy);
40              Debug.debug("cmyk_cmy_rgb: " + cmyk_cmy_rgb + " (" + Integer.toHexString(cmyk_cmy_rgb) + ")");
41  
42              assertEquals((0xffffff & cmyk_cmy_rgb), (0xffffff & rgb));
43          }
44      }
45  
46      @Test
47      public void testRGBtoHSL() throws Exception {
48          for (final int rgb : SAMPLE_RGBS) {
49              final ColorHsl hsl = ColorConversions.convertRGBtoHSL(rgb);
50              final int hsl_rgb = ColorConversions.convertHSLtoRGB(hsl);
51  
52              Debug.debug("hsl: " + hsl);
53              Debug.debug("hsl_rgb: " + hsl_rgb + " (" + Integer.toHexString(hsl_rgb) + ")");
54  
55              assertEquals((0xffffff & hsl_rgb), (0xffffff & rgb));
56          }
57      }
58  
59      @Test
60      public void testRGBtoHSV() throws Exception {
61          for (final int rgb : SAMPLE_RGBS) {
62              final ColorHsv hsv = ColorConversions.convertRGBtoHSV(rgb);
63              final int hsv_rgb = ColorConversions.convertHSVtoRGB(hsv);
64  
65              Debug.debug("hsv: " + hsv);
66              Debug.debug("hsv_rgb: " + hsv_rgb + " (" + Integer.toHexString(hsv_rgb) + ")");
67  
68              assertEquals((0xffffff & hsv_rgb), (0xffffff & rgb));
69          }
70      }
71  
72      @Test
73      public void testXYZ() throws Exception {
74          for (final int rgb : SAMPLE_RGBS) {
75              final ColorXyz xyz = ColorConversions.convertRGBtoXYZ(rgb);
76              final int xyz_rgb = ColorConversions.convertXYZtoRGB(xyz);
77  
78              Debug.debug();
79              Debug.debug("rgb: " + rgb + " (" + Integer.toHexString(rgb) + ")");
80              Debug.debug("xyz: " + xyz);
81              Debug.debug("xyz_rgb: " + xyz_rgb + " (" + Integer.toHexString(xyz_rgb) + ")");
82  
83              assertEquals((0xffffff & xyz_rgb), (0xffffff & rgb));
84  
85              final ColorCieLab cielab = ColorConversions.convertXYZtoCIELab(xyz);
86              final ColorXyz cielab_xyz = ColorConversions.convertCIELabtoXYZ(cielab);
87              final int cielab_xyz_rgb = ColorConversions.convertXYZtoRGB(cielab_xyz);
88  
89              Debug.debug("cielab: " + cielab);
90              Debug.debug("cielab_xyz: " + cielab_xyz);
91              Debug.debug("cielab_xyz_rgb: " + cielab_xyz_rgb + " (" + Integer.toHexString(cielab_xyz_rgb) + ")");
92  
93              assertEquals((0xffffff & cielab_xyz_rgb), (0xffffff & rgb));
94  
95              final ColorHunterLab hunterlab = ColorConversions.convertXYZtoHunterLab(xyz);
96              final ColorXyz hunterlab_xyz = ColorConversions.convertHunterLabtoXYZ(hunterlab);
97              final int hunterlab_xyz_rgb = ColorConversions.convertXYZtoRGB(hunterlab_xyz);
98  
99              Debug.debug("hunterlab: " + hunterlab);
100             Debug.debug("hunterlab_xyz: " + hunterlab_xyz);
101             Debug.debug("hunterlab_xyz_rgb: " + hunterlab_xyz_rgb + " ("
102                             + Integer.toHexString(hunterlab_xyz_rgb) + ")");
103 
104             assertEquals((0xffffff & hunterlab_xyz_rgb), (0xffffff & rgb));
105 
106             final ColorCieLch cielch = ColorConversions.convertCIELabtoCIELCH(cielab);
107             final ColorCieLab cielch_cielab = ColorConversions.convertCIELCHtoCIELab(cielch);
108 
109             Debug.debug("cielch", cielch);
110             Debug.debug("cielch_cielab", cielch_cielab);
111 
112             final ColorCieLuv cieluv = ColorConversions.convertXYZtoCIELuv(xyz);
113             final ColorXyz cieluv_xyz = ColorConversions.convertCIELuvtoXYZ(cieluv);
114 
115             Debug.debug("cieluv", cieluv);
116             Debug.debug("cieluv_xyz", cieluv_xyz);
117         }
118     }
119 }