1 /* 2 * Copyright (C) 2017 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 package android.graphics.cts; 17 18 import static android.graphics.Color.alpha; 19 import static android.graphics.Color.blue; 20 import static android.graphics.Color.colorSpace; 21 import static android.graphics.Color.convert; 22 import static android.graphics.Color.green; 23 import static android.graphics.Color.luminance; 24 import static android.graphics.Color.pack; 25 import static android.graphics.Color.red; 26 import static android.graphics.Color.toArgb; 27 import static android.graphics.Color.valueOf; 28 29 import static org.junit.Assert.assertEquals; 30 import static org.junit.Assert.assertFalse; 31 import static org.junit.Assert.assertNotEquals; 32 import static org.junit.Assert.assertNotNull; 33 import static org.junit.Assert.assertSame; 34 import static org.junit.Assert.assertTrue; 35 36 import android.graphics.Color; 37 import android.graphics.ColorSpace; 38 import android.graphics.ColorSpace.Named; 39 40 import androidx.test.filters.SmallTest; 41 import androidx.test.runner.AndroidJUnit4; 42 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 46 @SmallTest 47 @RunWith(AndroidJUnit4.class) 48 public class Color_ColorLongTest { 49 @Test 50 public void testRed() { 51 ColorSpace p3 = ColorSpace.get(Named.DISPLAY_P3); 52 53 assertEquals(0.5f, red(pack(0.5f, 0.0f, 1.0f)), 0.01f); 54 assertEquals(0.5f, red(pack(0.5f, 0.0f, 1.0f, 1.0f, p3)), 0.01f); 55 } 56 57 @Test 58 public void testGreen() { 59 ColorSpace p3 = ColorSpace.get(Named.DISPLAY_P3); 60 61 assertEquals(0.7f, green(pack(0.5f, 0.7f, 1.0f)), 0.01f); 62 assertEquals(0.7f, green(pack(0.5f, 0.7f, 1.0f, 1.0f, p3)), 0.01f); 63 } 64 65 @Test 66 public void testBlue() { 67 ColorSpace p3 = ColorSpace.get(Named.DISPLAY_P3); 68 69 assertEquals(1.0f, blue(pack(0.5f, 0.7f, 1.0f)), 0.01f); 70 assertEquals(1.0f, blue(pack(0.5f, 0.7f, 1.0f, 1.0f, p3)), 0.01f); 71 } 72 73 @Test 74 public void testAlpha() { 75 ColorSpace p3 = ColorSpace.get(Named.DISPLAY_P3); 76 77 assertEquals(0.25f, alpha(pack(0.5f, 0.7f, 1.0f, 0.25f)), 0.01f); 78 assertEquals(0.25f, alpha(pack(0.5f, 0.7f, 1.0f, 0.25f, p3)), 0.01f); 79 } 80 81 @Test 82 public void testColorSpace() { 83 ColorSpace srgb = ColorSpace.get(Named.SRGB); 84 ColorSpace p3 = ColorSpace.get(Named.DISPLAY_P3); 85 86 assertEquals(srgb, colorSpace(pack(0.5f, 0.7f, 1.0f))); 87 assertEquals(p3, colorSpace(pack(0.5f, 0.7f, 1.0f, 1.0f, p3))); 88 } 89 90 @Test(expected = IllegalArgumentException.class) 91 public void testInvalidColorSpace() { 92 colorSpace(0xffffffffffffffffL); 93 } 94 95 @Test(expected = IllegalArgumentException.class) 96 public void testInvalidColorSpace2() { 97 colorSpace((long) ColorSpace.Named.values().length); 98 } 99 100 @Test 101 public void testIsSrgb() { 102 ColorSpace p3 = ColorSpace.get(Named.DISPLAY_P3); 103 104 assertTrue(Color.isSrgb(pack(0.5f, 0.7f, 1.0f))); 105 assertFalse(Color.isSrgb(pack(0.5f, 0.7f, 1.0f, 1.0f, p3))); 106 107 assertTrue(Color.valueOf(0.5f, 0.7f, 1.0f).isSrgb()); 108 assertFalse(Color.valueOf(0.5f, 0.7f, 1.0f, 1.0f, p3).isSrgb()); 109 } 110 111 @Test 112 public void testIsWideGamut() { 113 ColorSpace p3 = ColorSpace.get(Named.DISPLAY_P3); 114 115 assertFalse(Color.isWideGamut(pack(0.5f, 0.7f, 1.0f))); 116 assertTrue(Color.isWideGamut(pack(0.5f, 0.7f, 1.0f, 1.0f, p3))); 117 118 assertFalse(Color.valueOf(0.5f, 0.7f, 1.0f).isWideGamut()); 119 assertTrue(Color.valueOf(0.5f, 0.7f, 1.0f, 1.0f, p3).isWideGamut()); 120 } 121 122 @Test 123 public void testIsInColorSpace() { 124 ColorSpace p3 = ColorSpace.get(Named.DISPLAY_P3); 125 126 assertFalse(Color.isInColorSpace(pack(0.5f, 0.7f, 1.0f), p3)); 127 assertTrue(Color.isInColorSpace(pack(0.5f, 0.7f, 1.0f, 1.0f, p3), p3)); 128 } 129 130 @Test 131 public void testValueOf() { 132 ColorSpace p3 = ColorSpace.get(Named.DISPLAY_P3); 133 134 Color color1 = valueOf(0x7fff00ff); 135 assertEquals(1.0f, color1.red(), 0.01f); 136 assertEquals(0.0f, color1.green(), 0.01f); 137 assertEquals(1.0f, color1.blue(), 0.01f); 138 assertEquals(0.5f, color1.alpha(), 0.01f); 139 assertTrue(color1.getColorSpace().isSrgb()); 140 141 Color color2 = valueOf(0.5f, 0.7f, 1.0f); 142 assertEquals(0.5f, color2.red(), 0.01f); 143 assertEquals(0.7f, color2.green(), 0.01f); 144 assertEquals(1.0f, color2.blue(), 0.01f); 145 assertEquals(1.0f, color2.alpha(), 0.01f); 146 assertTrue(color2.getColorSpace().isSrgb()); 147 148 Color color3 = valueOf(0.5f, 0.5f, 1.0f, 0.25f); 149 assertEquals(0.5f, color3.red(), 0.01f); 150 assertEquals(0.5f, color3.green(), 0.01f); 151 assertEquals(1.0f, color3.blue(), 0.01f); 152 assertEquals(0.25f, color3.alpha(), 0.01f); 153 assertTrue(color3.getColorSpace().isSrgb()); 154 155 Color color4 = valueOf(0.5f, 0.5f, 1.0f, 0.25f, p3); 156 assertEquals(0.5f, color4.red(), 0.01f); 157 assertEquals(0.5f, color4.green(), 0.01f); 158 assertEquals(1.0f, color4.blue(), 0.01f); 159 assertEquals(0.25f, color4.alpha(), 0.01f); 160 assertFalse(color4.getColorSpace().isSrgb()); 161 assertEquals(p3, color4.getColorSpace()); 162 163 Color color5 = valueOf(pack(0.5f, 0.5f, 1.0f, 0.25f, p3)); 164 assertEquals(0.5f, color5.red(), 0.01f); 165 assertEquals(0.5f, color5.green(), 0.01f); 166 assertEquals(1.0f, color5.blue(), 0.01f); 167 assertEquals(0.25f, color5.alpha(), 0.01f); 168 assertFalse(color5.getColorSpace().isSrgb()); 169 assertEquals(p3, color5.getColorSpace()); 170 171 Color color6 = valueOf(pack(0.5f, 0.5f, 1.0f, 0.25f)); 172 assertEquals(0.5f, color6.red(), 0.01f); 173 assertEquals(0.5f, color6.green(), 0.01f); 174 assertEquals(1.0f, color6.blue(), 0.01f); 175 assertEquals(0.25f, color6.alpha(), 0.01f); 176 assertTrue(color6.getColorSpace().isSrgb()); 177 178 Color color7 = valueOf(new float[] { 0.5f, 0.5f, 1.0f, 0.25f }, ColorSpace.get(Named.SRGB)); 179 assertEquals(0.5f, color7.red(), 0.01f); 180 assertEquals(0.5f, color7.green(), 0.01f); 181 assertEquals(1.0f, color7.blue(), 0.01f); 182 assertEquals(0.25f, color7.alpha(), 0.01f); 183 assertTrue(color7.getColorSpace().isSrgb()); 184 185 float[] components = { 0.5f, 0.5f, 1.0f, 0.25f, 0.5f, 0.8f }; 186 Color color8 = valueOf(components, ColorSpace.get(Named.SRGB)); 187 assertEquals(0.5f, color8.red(), 0.01f); 188 assertEquals(0.5f, color8.green(), 0.01f); 189 assertEquals(1.0f, color8.blue(), 0.01f); 190 assertEquals(0.25f, color8.alpha(), 0.01f); 191 assertTrue(color8.getColorSpace().isSrgb()); 192 // Make sure we received a copy 193 components[0] = 127.0f; 194 assertNotEquals(color8.red(), components[0]); 195 } 196 197 @Test(expected = IllegalArgumentException.class) 198 public void testValueOfFailure() { 199 valueOf(new float[] { 0.5f, 0.5f, 1.0f }, ColorSpace.get(Named.SRGB)); 200 } 201 202 @Test 203 public void testModel() { 204 Color c = valueOf(new float[] { 0.5f, 0.5f, 1.0f, 1.0f }, ColorSpace.get(Named.CIE_XYZ)); 205 assertEquals(ColorSpace.Model.XYZ, c.getModel()); 206 } 207 208 @Test 209 public void testComponents() { 210 Color c = valueOf(0.1f, 0.2f, 0.3f, 0.4f); 211 212 assertEquals(4, c.getComponentCount()); 213 assertEquals(c.red(), c.getComponent(0), 0.0f); 214 assertEquals(c.green(), c.getComponent(1), 0.0f); 215 assertEquals(c.blue(), c.getComponent(2), 0.0f); 216 assertEquals(c.alpha(), c.getComponent(3), 0.0f); 217 218 float[] components = c.getComponents(); 219 assertEquals(c.getComponentCount(), components.length); 220 assertEquals(c.red(), components[0], 0.0f); 221 assertEquals(c.green(), components[1], 0.0f); 222 assertEquals(c.blue(), components[2], 0.0f); 223 assertEquals(c.alpha(), components[3], 0.0f); 224 225 // Make sure we received a copy 226 components[0] = 127.0f; 227 assertNotEquals(c.red(), components[0]); 228 229 float[] componentsRet = c.getComponents(components); 230 assertSame(components, componentsRet); 231 assertEquals(c.getComponentCount(), componentsRet.length); 232 assertEquals(c.red(), componentsRet[0], 0.0f); 233 assertEquals(c.green(), componentsRet[1], 0.0f); 234 assertEquals(c.blue(), componentsRet[2], 0.0f); 235 assertEquals(c.alpha(), componentsRet[3], 0.0f); 236 237 componentsRet = c.getComponents(null); 238 assertNotNull(componentsRet); 239 assertEquals(c.getComponentCount(), componentsRet.length); 240 assertEquals(c.red(), componentsRet[0], 0.0f); 241 assertEquals(c.green(), componentsRet[1], 0.0f); 242 assertEquals(c.blue(), componentsRet[2], 0.0f); 243 assertEquals(c.alpha(), componentsRet[3], 0.0f); 244 } 245 246 @Test(expected = ArrayIndexOutOfBoundsException.class) 247 public void testComponentOutOfBounds() { 248 valueOf(0.1f, 0.2f, 0.3f, 0.4f).getComponent(4); 249 } 250 251 @Test(expected = IllegalArgumentException.class) 252 public void testGetComponentOutOfBounds() { 253 valueOf(0.1f, 0.2f, 0.3f, 0.4f).getComponents(new float[3]); 254 } 255 256 @Test 257 public void testToArgb() { 258 assertEquals(0xff8000ff, toArgb(pack(0.5f, 0.0f, 1.0f))); 259 260 long color = pack(0.8912f, 0.4962f, 0.1164f, 1.0f, ColorSpace.get(Named.ADOBE_RGB)); 261 // Red if 0x7f instead of 0x80 because the rounding error caused by the 262 // intermediate fp16 representation 263 assertEquals(0xffff7f00, toArgb(color)); 264 265 assertEquals(0xff8000ff, valueOf(0.5f, 0.0f, 1.0f).toArgb()); 266 assertEquals(0xffff7f00, 267 valueOf(0.8912f, 0.4962f, 0.1164f, 1.0f, ColorSpace.get(Named.ADOBE_RGB)).toArgb()); 268 } 269 270 @Test 271 public void testPackSrgb() { 272 ColorSpace srgb = ColorSpace.get(Named.SRGB); 273 274 long color1 = pack(0.5f, 0.0f, 1.0f); 275 long color2 = pack(0.5f, 0.0f, 1.0f, 1.0f); 276 long color3 = pack(0.5f, 0.0f, 1.0f, 1.0f, srgb); 277 278 assertEquals(color1, color2); 279 assertEquals(color1, color3); 280 281 assertEquals(0xff8000ff, (int) (color1 >>> 32)); 282 283 long color4 = pack(0.5f, 0.0f, 1.0f); 284 285 assertEquals(0.5f, red(color4), 0.01f); 286 assertEquals(0.0f, green(color4), 0.01f); 287 assertEquals(1.0f, blue(color4), 0.01f); 288 assertEquals(1.0f, alpha(color4), 0.01f); 289 290 long color5 = pack(0xff8000ff); 291 292 assertEquals(color1, color5); 293 assertEquals(0xff8000ff, (int) (color5 >>> 32)); 294 295 assertEquals(color1, valueOf(0.5f, 0.0f, 1.0f).pack()); 296 } 297 298 @Test 299 public void testPack() { 300 ColorSpace p3 = ColorSpace.get(Named.DISPLAY_P3); 301 long color = pack(0.5f, 0.0f, 1.0f, 0.25f, p3); 302 303 assertEquals(0.5f, red(color), 0.01f); 304 assertEquals(0.0f, green(color), 0.01f); 305 assertEquals(1.0f, blue(color), 0.01f); 306 assertEquals(0.25f, alpha(color), 0.01f); 307 308 assertEquals(color, valueOf(0.5f, 0.0f, 1.0f, 0.25f, p3).pack()); 309 } 310 311 @Test(expected = IllegalArgumentException.class) 312 public void testPackFailure() { 313 ColorSpace colorSpace = new ColorSpace.Rgb("Fake", 314 new float[] { 0.7347f, 0.2653f, 0.1596f, 0.8404f, 0.0366f, 0.0001f }, 315 new float[] { 0.34567f, 0.35850f }, x -> x * 2.0f, x -> x / 2.0f, 0.0f, 1.0f); 316 pack(0.5f, 0.0f, 1.0f, 0.25f, colorSpace); 317 } 318 319 @Test 320 public void testLuminanceSrgb() { 321 assertEquals(0.0722f, luminance(pack(0.0f, 0.0f, 1.0f)), 0.000001f); 322 assertEquals(0.2126f, luminance(pack(1.0f, 0.0f, 0.0f)), 0.000001f); 323 assertEquals(0.7152f, luminance(pack(0.0f, 1.0f, 0.0f)), 0.000001f); 324 assertEquals(1.0f, luminance(pack(1.0f, 1.0f, 1.0f)), 0.0f); 325 assertEquals(0.0f, luminance(pack(0.0f, 0.0f, 0.0f)), 0.0f); 326 327 assertEquals(0.0722f, valueOf(0.0f, 0.0f, 1.0f).luminance(), 0.000001f); 328 assertEquals(0.2126f, valueOf(1.0f, 0.0f, 0.0f).luminance(), 0.000001f); 329 assertEquals(0.7152f, valueOf(0.0f, 1.0f, 0.0f).luminance(), 0.000001f); 330 assertEquals(1.0f, valueOf(1.0f, 1.0f, 1.0f).luminance(), 0.0f); 331 assertEquals(0.0f, valueOf(0.0f, 0.0f, 0.0f).luminance(), 0.0f); 332 } 333 334 @Test 335 public void testLuminance() { 336 ColorSpace p3 = ColorSpace.get(Named.DISPLAY_P3); 337 assertEquals(0.0722f, luminance(pack(0.0f, 0.0f, 1.0f, 1.0f, p3)), 0.000001f); 338 assertEquals(0.2126f, luminance(pack(1.0f, 0.0f, 0.0f, 1.0f, p3)), 0.000001f); 339 assertEquals(0.7152f, luminance(pack(0.0f, 1.0f, 0.0f, 1.0f, p3)), 0.000001f); 340 assertEquals(1.0f, luminance(pack(1.0f, 1.0f, 1.0f, 1.0f, p3)), 0.0f); 341 assertEquals(0.0f, luminance(pack(0.0f, 0.0f, 0.0f, 1.0f, p3)), 0.0f); 342 343 assertEquals(0.0722f, valueOf(0.0f, 0.0f, 1.0f, 1.0f, p3).luminance(), 0.000001f); 344 assertEquals(0.2126f, valueOf(1.0f, 0.0f, 0.0f, 1.0f, p3).luminance(), 0.000001f); 345 assertEquals(0.7152f, valueOf(0.0f, 1.0f, 0.0f, 1.0f, p3).luminance(), 0.000001f); 346 assertEquals(1.0f, valueOf(1.0f, 1.0f, 1.0f, 1.0f, p3).luminance(), 0.0f); 347 assertEquals(0.0f, valueOf(0.0f, 0.0f, 0.0f, 1.0f, p3).luminance(), 0.0f); 348 } 349 350 @Test(expected = IllegalArgumentException.class) 351 public void testLuminanceFunctionFailure() { 352 luminance(pack(1.0f, 1.0f, 1.0f, 1.0f, ColorSpace.get(Named.CIE_LAB))); 353 } 354 355 @Test(expected = IllegalArgumentException.class) 356 public void testLuminanceMethodFailure() { 357 valueOf(1.0f, 1.0f, 1.0f, 1.0f, ColorSpace.get(Named.CIE_LAB)).luminance(); 358 } 359 360 @Test 361 public void testConvertMethod() { 362 Color sRgb = Color.valueOf(1.0f, 0.5f, 0.0f); 363 Color adobeRgb = sRgb.convert(ColorSpace.get(Named.ADOBE_RGB)); 364 365 assertEquals(ColorSpace.get(Named.ADOBE_RGB), adobeRgb.getColorSpace()); 366 assertEquals(0.8912f, adobeRgb.red(), 0.001f); 367 assertEquals(0.4962f, adobeRgb.green(), 0.001f); 368 assertEquals(0.1164f, adobeRgb.blue(), 0.001f); 369 } 370 371 @Test 372 public void testConvertToBT709() { 373 int sRgb = Color.argb(1.0f, 0.5f, 0.5f, 0.5f); 374 long bt709 = Color.convert(sRgb, ColorSpace.get(Named.BT709)); 375 376 assertEquals(ColorSpace.get(Named.BT709), Color.colorSpace(bt709)); 377 } 378 379 @Test 380 public void testConvertColorInt() { 381 long color = convert(0xffff8000, ColorSpace.get(Named.ADOBE_RGB)); 382 383 assertEquals(ColorSpace.get(Named.ADOBE_RGB), colorSpace(color)); 384 assertEquals(0.8912f, red(color), 0.01f); 385 assertEquals(0.4962f, green(color), 0.01f); 386 assertEquals(0.1164f, blue(color), 0.01f); 387 } 388 389 @Test 390 public void testConvertColorLong() { 391 long color = convert(pack(1.0f, 0.5f, 0.0f, 1.0f, ColorSpace.get(Named.DISPLAY_P3)), 392 ColorSpace.get(Named.ADOBE_RGB)); 393 394 assertEquals(0.9499f, red(color), 0.01f); 395 assertEquals(0.4597f, green(color), 0.01f); 396 assertEquals(0.0000f, blue(color), 0.01f); 397 } 398 399 @Test 400 public void testConvertConnector() { 401 ColorSpace p3 = ColorSpace.get(Named.DISPLAY_P3); 402 ColorSpace.Connector connector = ColorSpace.connect(p3, ColorSpace.get(Named.ADOBE_RGB)); 403 long color = convert(pack(1.0f, 0.5f, 0.0f, 1.0f, p3), connector); 404 405 assertEquals(0.9499f, red(color), 0.01f); 406 assertEquals(0.4597f, green(color), 0.01f); 407 assertEquals(0.0000f, blue(color), 0.01f); 408 409 color = convert(1.0f, 0.5f, 0.0f, 1.0f, connector); 410 411 assertEquals(0.9499f, red(color), 0.01f); 412 assertEquals(0.4597f, green(color), 0.01f); 413 assertEquals(0.0000f, blue(color), 0.01f); 414 } 415 416 @Test 417 public void testConvert() { 418 long color = convert(1.0f, 0.5f, 0.0f, 1.0f, 419 ColorSpace.get(Named.DISPLAY_P3), ColorSpace.get(Named.ADOBE_RGB)); 420 421 assertEquals(0.9499f, red(color), 0.01f); 422 assertEquals(0.4597f, green(color), 0.01f); 423 assertEquals(0.0000f, blue(color), 0.01f); 424 } 425 426 @Test(expected = IllegalArgumentException.class) 427 public void testConvertFailureUnnamedColorSpace() { 428 float[] primaries = new float[]{ .7f, .6f, .5f, .4f, .3f, .2f }; 429 ColorSpace cs = new ColorSpace.Rgb("mock", primaries, ColorSpace.ILLUMINANT_A, .7); 430 Color.convert(Color.BLUE, cs); 431 } 432 } 433