Roll android_tools support library to 25.1.0
[android_tools.git] / sdk / sources / android-23 / android / support / v4 / graphics / ColorUtilsTest.java
blob855cdba3a5e9c7254ddd0fa4ddc03c4c861d938e
1 /*
2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.support.v4.graphics;
19 import android.graphics.Color;
20 import android.support.v4.graphics.ColorUtils;
21 import android.test.AndroidTestCase;
23 import java.util.ArrayList;
25 /**
26 * @hide
28 public class ColorUtilsTest extends AndroidTestCase {
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;
35 private static final int ALLOWED_OFFSET_RGB_COMPONENT = 2;
37 private static final ArrayList<TestEntry> sEntryList = new ArrayList<>();
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));
51 public void testToHSL() {
52 for (TestEntry entry : sEntryList) {
53 testColorToHSL(entry.rgb, entry.hsl);
57 public void testFromHSL() {
58 for (TestEntry entry : sEntryList) {
59 testHSLToColor(entry.hsl, entry.rgb);
63 public void testToHslLimits() {
64 final float[] hsl = new float[3];
66 for (TestEntry entry : sEntryList) {
67 ColorUtils.colorToHSL(entry.rgb, hsl);
69 assertTrue(hsl[0] >= 0f && hsl[0] <= 360f);
70 assertTrue(hsl[1] >= 0f && hsl[1] <= 1f);
71 assertTrue(hsl[2] >= 0f && hsl[2] <= 1f);
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);
80 assertTrue(sb.toString(), Math.abs(expected - actual) <= allowedOffset);
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);
88 assertTrue(sb.toString(), Math.abs(expected - actual) <= allowedOffset);
91 private static void testColorToHSL(int color, float[] expectedHsl) {
92 float[] actualHSL = new float[3];
93 ColorUtils.colorToHSL(color, actualHSL);
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);
103 private static void testHSLToColor(float[] hsl, int expectedRgb) {
104 final int actualRgb = ColorUtils.HSLToColor(hsl);
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);
114 private static class TestEntry {
115 final int rgb;
116 final float[] hsl = new float[3];
118 TestEntry(int rgb) {
119 this.rgb = rgb;
122 TestEntry setHsl(float h, float s, float l) {
123 hsl[0] = h;
124 hsl[1] = s;
125 hsl[2] = l;
126 return this;