1 /* 2 * Copyright 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.core.content.res; 18 19 20 import static org.junit.Assert.assertArrayEquals; 21 import static org.junit.Assert.assertEquals; 22 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 import org.junit.runners.JUnit4; 26 27 @RunWith(JUnit4.class) 28 public final class CamTest { 29 static final int BLACK = 0xff000000; 30 static final int WHITE = 0xffffffff; 31 static final int MIDGRAY = 0xff777777; 32 33 static final int RED = 0xffff0000; 34 static final int GREEN = 0xff00ff00; 35 static final int BLUE = 0xff0000ff; 36 37 @Test xyzFromRgb()38 public void xyzFromRgb() { 39 float[] xyz = new float[3]; 40 CamUtils.xyzFromInt(RED, xyz); 41 float[] expected = new float[]{41.233f, 21.260f, 1.932f}; 42 assertArrayEquals(expected, xyz, 0.001f); 43 } 44 45 @Test intFrom50LStar()46 public void intFrom50LStar() { 47 int color = CamUtils.intFromLStar(50.0f); 48 assertEquals(MIDGRAY, color); 49 } 50 51 @Test intFrom0LStar()52 public void intFrom0LStar() { 53 int color = CamUtils.intFromLStar(0.0f); 54 assertEquals(BLACK, color); 55 } 56 57 @Test intFrom100LStar()58 public void intFrom100LStar() { 59 int color = CamUtils.intFromLStar(100.0f); 60 assertEquals(WHITE, color); 61 } 62 63 @Test camFromIntToInt()64 public void camFromIntToInt() { 65 CamColor cam = CamColor.fromColor(RED); 66 int color = cam.viewed(ViewingConditions.DEFAULT); 67 assertEquals(color, RED); 68 } 69 70 @Test yFromMidgray()71 public void yFromMidgray() { 72 assertEquals(18.418f, CamUtils.yFromLStar(50.0f), 0.001); 73 } 74 75 @Test yFromBlack()76 public void yFromBlack() { 77 assertEquals(0.0f, CamUtils.yFromLStar(0.0f), 0.001); 78 } 79 80 @Test yFromWhite()81 public void yFromWhite() { 82 assertEquals(100.0f, CamUtils.yFromLStar(100.0f), 0.001); 83 } 84 85 @Test camFromRed()86 public void camFromRed() { 87 CamColor cam = CamColor.fromColor(RED); 88 assertEquals(46.445f, cam.getJ(), 0.001f); 89 assertEquals(113.357f, cam.getChroma(), 0.001f); 90 assertEquals(27.408f, cam.getHue(), 0.001f); 91 assertEquals(89.494f, cam.getM(), 0.001f); 92 assertEquals(91.889f, cam.getS(), 0.001f); 93 assertEquals(105.988f, cam.getQ(), 0.001f); 94 } 95 96 @Test camFromGreen()97 public void camFromGreen() { 98 CamColor cam = CamColor.fromColor(GREEN); 99 assertEquals(79.331f, cam.getJ(), 0.001f); 100 assertEquals(108.409f, cam.getChroma(), 0.001f); 101 assertEquals(142.139f, cam.getHue(), 0.001f); 102 assertEquals(85.587f, cam.getM(), 0.001f); 103 assertEquals(78.604f, cam.getS(), 0.001f); 104 assertEquals(138.520, cam.getQ(), 0.001f); 105 } 106 107 @Test camFromBlue()108 public void camFromBlue() { 109 CamColor cam = CamColor.fromColor(BLUE); 110 assertEquals(25.465f, cam.getJ(), 0.001f); 111 assertEquals(87.230f, cam.getChroma(), 0.001f); 112 assertEquals(282.788f, cam.getHue(), 0.001f); 113 assertEquals(68.867f, cam.getM(), 0.001f); 114 assertEquals(93.674f, cam.getS(), 0.001f); 115 assertEquals(78.481f, cam.getQ(), 0.001f); 116 } 117 118 @Test camFromBlack()119 public void camFromBlack() { 120 CamColor cam = CamColor.fromColor(BLACK); 121 assertEquals(0.0f, cam.getJ(), 0.001f); 122 assertEquals(0.0f, cam.getChroma(), 0.001f); 123 assertEquals(0.0f, cam.getHue(), 0.001f); 124 assertEquals(0.0f, cam.getM(), 0.001f); 125 assertEquals(0.0f, cam.getS(), 0.001f); 126 assertEquals(0.0f, cam.getQ(), 0.001f); 127 } 128 129 @Test camFromWhite()130 public void camFromWhite() { 131 CamColor cam = CamColor.fromColor(WHITE); 132 assertEquals(100.0f, cam.getJ(), 0.001f); 133 assertEquals(2.869f, cam.getChroma(), 0.001f); 134 assertEquals(209.492f, cam.getHue(), 0.001f); 135 assertEquals(2.265f, cam.getM(), 0.001f); 136 assertEquals(12.068f, cam.getS(), 0.001f); 137 assertEquals(155.521, cam.getQ(), 0.001f); 138 } 139 140 @Test getRedFromGamutMap()141 public void getRedFromGamutMap() { 142 int colorToTest = RED; 143 CamColor cam = CamColor.fromColor(colorToTest); 144 int color = CamColor.toColor(cam.getHue(), cam.getChroma(), 145 CamUtils.lStarFromInt(colorToTest)); 146 assertEquals(colorToTest, color); 147 } 148 149 @Test getGreenFromGamutMap()150 public void getGreenFromGamutMap() { 151 int colorToTest = GREEN; 152 CamColor cam = CamColor.fromColor(colorToTest); 153 int color = CamColor.toColor(cam.getHue(), cam.getChroma(), 154 CamUtils.lStarFromInt(colorToTest)); 155 assertEquals(colorToTest, color); 156 } 157 158 @Test getBlueFromGamutMap()159 public void getBlueFromGamutMap() { 160 int colorToTest = BLUE; 161 CamColor cam = CamColor.fromColor(colorToTest); 162 int color = CamColor.toColor(cam.getHue(), cam.getChroma(), 163 CamUtils.lStarFromInt(colorToTest)); 164 assertEquals(colorToTest, color); 165 } 166 167 @Test getWhiteFromGamutMap()168 public void getWhiteFromGamutMap() { 169 int colorToTest = WHITE; 170 CamColor cam = CamColor.fromColor(colorToTest); 171 int color = CamColor.toColor(cam.getHue(), cam.getChroma(), 172 CamUtils.lStarFromInt(colorToTest)); 173 assertEquals(colorToTest, color); 174 } 175 176 @Test getBlackFromGamutMap()177 public void getBlackFromGamutMap() { 178 int colorToTest = BLACK; 179 CamColor cam = CamColor.fromColor(colorToTest); 180 int color = CamColor.toColor(cam.getHue(), cam.getChroma(), 181 CamUtils.lStarFromInt(colorToTest)); 182 assertEquals(colorToTest, color); 183 } 184 185 @Test getMidgrayFromGamutMap()186 public void getMidgrayFromGamutMap() { 187 int colorToTest = MIDGRAY; 188 CamColor cam = CamColor.fromColor(colorToTest); 189 int color = CamColor.toColor(cam.getHue(), cam.getChroma(), 190 CamUtils.lStarFromInt(colorToTest)); 191 assertEquals(colorToTest, color); 192 } 193 194 @Test getMidredFromRedAndGamutMap()195 public void getMidredFromRedAndGamutMap() { 196 CamColor cam = CamColor.fromColor(RED); 197 int color = CamColor.toColor(cam.getHue(), cam.getChroma(), 50.0f); 198 assertEquals(0xfff00000, color); 199 } 200 201 @Test getRandomGreenFromGamutMap()202 public void getRandomGreenFromGamutMap() { 203 int colorToTest = 0xff009200; 204 CamColor cam = CamColor.fromColor(colorToTest); 205 int color = CamColor.toColor(cam.getHue(), cam.getChroma(), 206 CamUtils.lStarFromInt(colorToTest)); 207 assertEquals(colorToTest, color); 208 } 209 210 @Test gamutMapArbitraryHCL()211 public void gamutMapArbitraryHCL() { 212 int color = CamColor.toColor(309.0f, 40.0f, 70.0f); 213 CamColor cam = CamColor.fromColor(color); 214 215 assertEquals(308.759f, cam.getHue(), 0.001); 216 assertEquals(40.148f, cam.getChroma(), 0.001); 217 assertEquals(70.029f, CamUtils.lStarFromInt(color), 0.001f); 218 } 219 220 @Test ucsCoordinates()221 public void ucsCoordinates() { 222 CamColor cam = CamColor.fromColor(RED); 223 224 assertEquals(59.584f, cam.getJStar(), 0.001f); 225 assertEquals(43.297f, cam.getAStar(), 0.001f); 226 assertEquals(22.451f, cam.getBStar(), 0.001f); 227 } 228 229 @Test deltaEWhiteToBlack()230 public void deltaEWhiteToBlack() { 231 assertEquals(25.661f, CamColor.fromColor(WHITE).distance(CamColor.fromColor(BLACK)), 232 0.001f); 233 } 234 235 @Test deltaERedToBlue()236 public void deltaERedToBlue() { 237 assertEquals(21.415f, CamColor.fromColor(RED).distance(CamColor.fromColor(BLUE)), 0.001f); 238 } 239 } 240