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 
17 package android.systemui.cts;
18 
19 /**
20  * Copies of non-public {@link android.graphics.Color} APIs
21  */
22 public class ColorUtils {
23 
brightness(int argb)24     public static float brightness(int argb) {
25         int r = (argb >> 16) & 0xFF;
26         int g = (argb >> 8) & 0xFF;
27         int b = argb & 0xFF;
28 
29         int V = Math.max(b, Math.max(r, g));
30 
31         return (V / 255.f);
32     }
33 
hue(int argb)34     public static float hue(int argb) {
35         int r = (argb >> 16) & 0xFF;
36         int g = (argb >> 8) & 0xFF;
37         int b = argb & 0xFF;
38 
39         int V = Math.max(b, Math.max(r, g));
40         int temp = Math.min(b, Math.min(r, g));
41 
42         float H;
43 
44         if (V == temp) {
45             H = 0;
46         } else {
47             final float vtemp = (float) (V - temp);
48             final float cr = (V - r) / vtemp;
49             final float cg = (V - g) / vtemp;
50             final float cb = (V - b) / vtemp;
51 
52             if (r == V) {
53                 H = cb - cg;
54             } else if (g == V) {
55                 H = 2 + cr - cb;
56             } else {
57                 H = 4 + cg - cr;
58             }
59 
60             H /= 6.f;
61             if (H < 0) {
62                 H++;
63             }
64         }
65 
66         return H;
67     }
68 }
69