1/* 2 * Copyright (C) 2015 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 17package android.support.v4.graphics; 18 19import android.graphics.Color; 20import android.support.v4.graphics.ColorUtils; 21import android.test.AndroidTestCase; 22 23import java.util.ArrayList; 24 25/** 26 * @hide 27 */ 28public class ColorUtilsTest extends AndroidTestCase { 29 30 // 0.5% of the max value 31 private static final float ALLOWED_OFFSET_HUE = 360 * 0.005f; 32 private static final float ALLOWED_OFFSET_SATURATION = 0.005f; 33 private static final float ALLOWED_OFFSET_LIGHTNESS = 0.005f; 34 35 private static final int ALLOWED_OFFSET_RGB_COMPONENT = 2; 36 37 private static final ArrayList<TestEntry> sEntryList = new ArrayList<>(); 38 39 static { 40 sEntryList.add(new TestEntry(Color.BLACK).setHsl(0f, 0f, 0f)); 41 sEntryList.add(new TestEntry(Color.WHITE).setHsl(0f, 0f, 1f)); 42 sEntryList.add(new TestEntry(Color.BLUE).setHsl(240f, 1f, 0.5f)); 43 sEntryList.add(new TestEntry(Color.GREEN).setHsl(120f, 1f, 0.5f)); 44 sEntryList.add(new TestEntry(Color.RED).setHsl(0f, 1f, 0.5f)); 45 sEntryList.add(new TestEntry(Color.CYAN).setHsl(180f, 1f, 0.5f)); 46 sEntryList.add(new TestEntry(0x2196F3).setHsl(207f, 0.9f, 0.54f)); 47 sEntryList.add(new TestEntry(0xD1C4E9).setHsl(261f, 0.46f, 0.84f)); 48 sEntryList.add(new TestEntry(0x311B92).setHsl(251.09f, 0.687f, 0.339f)); 49 } 50 51 public void testToHSL() { 52 for (TestEntry entry : sEntryList) { 53 testColorToHSL(entry.rgb, entry.hsl); 54 } 55 } 56 57 public void testFromHSL() { 58 for (TestEntry entry : sEntryList) { 59 testHSLToColor(entry.hsl, entry.rgb); 60 } 61 } 62 63 public void testToHslLimits() { 64 final float[] hsl = new float[3]; 65 66 for (TestEntry entry : sEntryList) { 67 ColorUtils.colorToHSL(entry.rgb, hsl); 68 69 assertTrue(hsl[0] >= 0f && hsl[0] <= 360f); 70 assertTrue(hsl[1] >= 0f && hsl[1] <= 1f); 71 assertTrue(hsl[2] >= 0f && hsl[2] <= 1f); 72 } 73 } 74 75 private static void assertClose(String message, float expected, float actual, 76 float allowedOffset) { 77 StringBuilder sb = new StringBuilder(message); 78 sb.append(". Expected: ").append(expected).append(". Actual: ").append(actual); 79 80 assertTrue(sb.toString(), Math.abs(expected - actual) <= allowedOffset); 81 } 82 83 private static void assertClose(String message, int expected, int actual, 84 int allowedOffset) { 85 StringBuilder sb = new StringBuilder(message); 86 sb.append(". Expected: ").append(expected).append(". Actual: ").append(actual); 87 88 assertTrue(sb.toString(), Math.abs(expected - actual) <= allowedOffset); 89 } 90 91 private static void testColorToHSL(int color, float[] expectedHsl) { 92 float[] actualHSL = new float[3]; 93 ColorUtils.colorToHSL(color, actualHSL); 94 95 assertClose("Hue not within offset", expectedHsl[0], actualHSL[0], 96 ALLOWED_OFFSET_HUE); 97 assertClose("Saturation not within offset", expectedHsl[1], actualHSL[1], 98 ALLOWED_OFFSET_SATURATION); 99 assertClose("Lightness not within offset", expectedHsl[2], actualHSL[2], 100 ALLOWED_OFFSET_LIGHTNESS); 101 } 102 103 private static void testHSLToColor(float[] hsl, int expectedRgb) { 104 final int actualRgb = ColorUtils.HSLToColor(hsl); 105 106 assertClose("Red not within offset", 107 Color.red(expectedRgb), Color.red(actualRgb), ALLOWED_OFFSET_RGB_COMPONENT); 108 assertClose("Green not within offset", 109 Color.green(expectedRgb), Color.green(actualRgb), ALLOWED_OFFSET_RGB_COMPONENT); 110 assertClose("Blue not within offset", 111 Color.blue(expectedRgb), Color.blue(actualRgb), ALLOWED_OFFSET_RGB_COMPONENT); 112 } 113 114 private static class TestEntry { 115 final int rgb; 116 final float[] hsl = new float[3]; 117 118 TestEntry(int rgb) { 119 this.rgb = rgb; 120 } 121 122 TestEntry setHsl(float h, float s, float l) { 123 hsl[0] = h; 124 hsl[1] = s; 125 hsl[2] = l; 126 return this; 127 } 128 } 129}