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 java.lang.Integer.toHexString;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import org.apache.commons.imaging.internal.Debug;
23  import org.junit.jupiter.api.Test;
24  
25  public class ColorConversionsTest {
26      private static final int[] SAMPLE_RGBS = { 0xffffffff, 0xff000000, 0xffff0000, 0xff00ff00, 0xff0000ff, 0xffff00ff, 0xfff0ff00, 0xff00ffff, 0x00000000,
27              0xff7f7f7f, };
28  
29      @Test
30      public void testRgbToCmyk() {
31          for (final int rgb : SAMPLE_RGBS) {
32              final ColorCmy cmy = ColorConversions.convertRgbToCmy(rgb);
33              final ColorCmyk cmyk = ColorConversions.convertCmyToCmyk(cmy);
34              final ColorCmy cmykCmy = ColorConversions.convertCmykToCmy(cmyk);
35              final int cmykCmyRgb = ColorConversions.convertCmyToRgb(cmykCmy);
36  
37              Debug.debug("cmy: " + cmy);
38              Debug.debug("cmyk: " + cmyk);
39              Debug.debug("cmykCmy: " + cmykCmy);
40              Debug.debug("cmykCmyRgb: " + cmykCmyRgb + " (" + Integer.toHexString(cmykCmyRgb) + ")");
41  
42              assertEquals(toHexString(0xffffff & rgb), toHexString(0xffffff & cmykCmyRgb));
43          }
44      }
45  
46      @Test
47      public void testRgbToDin99b() {
48          for (final int rgb : SAMPLE_RGBS) {
49  
50              final ColorXyz xyz = ColorConversions.convertRgbToXyz(rgb);
51              final ColorCieLab cielab = ColorConversions.convertXyzToCieLab(xyz);
52              final ColorDin99Lab din99b = ColorConversions.convertCieLabToDin99bLab(cielab);
53  
54              final ColorCieLab din99Cielab = ColorConversions.convertDin99bLabToCieLab(din99b);
55              final ColorXyz din99CielabXyz = ColorConversions.convertCieLabToXyz(din99Cielab);
56              final int din99CielabXyzRgb = ColorConversions.convertXyzToRgb(din99CielabXyz);
57  
58              assertEquals(toHexString(0xffffff & rgb), toHexString(0xffffff & din99CielabXyzRgb));
59          }
60      }
61  
62      @Test
63      public void testRgbToDin99o() {
64          for (final int rgb : SAMPLE_RGBS) {
65  
66              final ColorXyz xyz = ColorConversions.convertRgbToXyz(rgb);
67              final ColorCieLab cielab = ColorConversions.convertXyzToCieLab(xyz);
68              final ColorDin99Lab din99o = ColorConversions.convertCieLabToDin99oLab(cielab);
69  
70              final ColorCieLab din99Cielab = ColorConversions.convertDin99oLabToCieLab(din99o);
71              final ColorXyz din99CielabXyz = ColorConversions.convertCieLabToXyz(din99Cielab);
72              final int din99CielabXyzRgb = ColorConversions.convertXyzToRgb(din99CielabXyz);
73  
74              assertEquals(toHexString(0xffffff & rgb), toHexString(0xffffff & din99CielabXyzRgb));
75          }
76      }
77  
78      @Test
79      public void testRgbToHsl() {
80          for (final int rgb : SAMPLE_RGBS) {
81              final ColorHsl hsl = ColorConversions.convertRgbToHsl(rgb);
82              final int hslRgb = ColorConversions.convertHslToRgb(hsl);
83  
84              Debug.debug("hsl: " + hsl);
85              Debug.debug("hslRgb: " + hslRgb + " (" + Integer.toHexString(hslRgb) + ")");
86  
87              assertEquals(toHexString(0xffffff & rgb), toHexString(0xffffff & hslRgb));
88          }
89      }
90  
91      @Test
92      public void testRgbToHsv() {
93          for (final int rgb : SAMPLE_RGBS) {
94              final ColorHsv hsv = ColorConversions.convertRgbToHsv(rgb);
95              final int hsvRgb = ColorConversions.convertHsvToRgb(hsv);
96  
97              Debug.debug("hsv: " + hsv);
98              Debug.debug("hsvRgb: " + hsvRgb + " (" + Integer.toHexString(hsvRgb) + ")");
99  
100             assertEquals(toHexString(0xffffff & rgb), toHexString(0xffffff & hsvRgb));
101         }
102     }
103 
104     @Test
105     public void testXyz() {
106         for (final int rgb : SAMPLE_RGBS) {
107             final ColorXyz xyz = ColorConversions.convertRgbToXyz(rgb);
108             final int xyzRgb = ColorConversions.convertXyzToRgb(xyz);
109 
110             Debug.debug();
111             Debug.debug("rgb: " + rgb + " (" + Integer.toHexString(rgb) + ")");
112             Debug.debug("xyz: " + xyz);
113             Debug.debug("xyzRgb: " + xyzRgb + " (" + Integer.toHexString(xyzRgb) + ")");
114 
115             assertEquals(toHexString(0xffffff & rgb), toHexString(0xffffff & xyzRgb), "rgb " + toHexString(rgb) + ", " + xyz);
116 
117             final ColorCieLab cielab = ColorConversions.convertXyzToCieLab(xyz);
118             final ColorXyz cielabXyz = ColorConversions.convertCieLabToXyz(cielab);
119             final int cielabXyzRgb = ColorConversions.convertXyzToRgb(cielabXyz);
120 
121             Debug.debug("cielab: " + cielab);
122             Debug.debug("cielabXyz: " + cielabXyz);
123             Debug.debug("cielabXyzRgb: " + cielabXyzRgb + " (" + Integer.toHexString(cielabXyzRgb) + ")");
124 
125             assertEquals(toHexString(0xffffff & rgb), toHexString(0xffffff & cielabXyzRgb));
126 
127             final ColorHunterLab hunterlab = ColorConversions.convertXyzToHunterLab(xyz);
128             final ColorXyz hunterlabXyz = ColorConversions.convertHunterLabToXyz(hunterlab);
129             final int hunterlabXyzRgb = ColorConversions.convertXyzToRgb(hunterlabXyz);
130 
131             Debug.debug("hunterlab: " + hunterlab);
132             Debug.debug("hunterlabXyz: " + hunterlabXyz);
133             Debug.debug("hunterlabXyzRgb: " + hunterlabXyzRgb + " (" + Integer.toHexString(hunterlabXyzRgb) + ")");
134 
135             assertEquals(toHexString(0xffffff & rgb), toHexString(0xffffff & hunterlabXyzRgb));
136 
137             final ColorCieLch cielch = ColorConversions.convertCieLabToCieLch(cielab);
138             final ColorCieLab cielchCielab = ColorConversions.convertCieLchToCieLab(cielch);
139 
140             Debug.debug("cielch", cielch);
141             Debug.debug("cielchCielab", cielchCielab);
142 
143             final ColorCieLuv cieluv = ColorConversions.convertXyzToCieLuv(xyz);
144             final ColorXyz cieluvXyz = ColorConversions.convertCieLuvToXyz(cieluv);
145 
146             Debug.debug("cieluv", cieluv);
147             Debug.debug("cieluvXyz", cieluvXyz);
148         }
149     }
150 }