blob: 33f116ba4316d8d5d8de6003bab0e6173a744c17 [file] [log] [blame]
Winson Chung303e1ff2014-03-07 15:06:19 -08001/*
2 * Copyright (C) 2014 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
Winson Chungffa2ec62014-07-03 15:54:42 -070017package com.android.systemui.recents.misc;
Winson Chung303e1ff2014-03-07 15:06:19 -080018
Winson Chung353c0b92014-10-16 17:43:23 -070019import android.animation.Animator;
Winson Chungf5e22e72014-05-02 18:35:35 -070020import android.graphics.Color;
Winson3e874742016-01-07 10:08:17 -080021import android.graphics.Rect;
Winson3150e572015-10-23 15:07:24 -070022import android.graphics.RectF;
Winson3e874742016-01-07 10:08:17 -080023import android.graphics.drawable.Drawable;
24import android.util.IntProperty;
25import android.util.Property;
Winson Chungbf5dbf12014-09-16 00:58:25 +020026import android.view.View;
Winsonbe7607a2015-10-01 17:24:51 -070027import android.view.ViewParent;
Winson Chung303e1ff2014-03-07 15:06:19 -080028
29/* Common code */
30public class Utilities {
Winson Chungcdbbb7e2014-06-24 12:11:49 -070031
Winson3e874742016-01-07 10:08:17 -080032 public static final Property<Drawable, Integer> DRAWABLE_ALPHA =
33 new IntProperty<Drawable>("drawableAlpha") {
34 @Override
35 public void setValue(Drawable object, int alpha) {
36 object.setAlpha(alpha);
37 }
38
39 @Override
40 public Integer get(Drawable object) {
41 return object.getAlpha();
42 }
43 };
44
45 public static final Property<Drawable, Rect> DRAWABLE_RECT =
46 new Property<Drawable, Rect>(Rect.class, "drawableBounds") {
47 @Override
48 public void set(Drawable object, Rect bounds) {
49 object.setBounds(bounds);
50 }
51
52 @Override
53 public Rect get(Drawable object) {
54 return object.getBounds();
55 }
56 };
57
Winsonbe7607a2015-10-01 17:24:51 -070058 /**
59 * @return the first parent walking up the view hierarchy that has the given class type.
60 *
61 * @param parentClass must be a class derived from {@link View}
62 */
63 public static <T extends View> T findParent(View v, Class<T> parentClass) {
64 ViewParent parent = v.getParent();
65 while (parent != null) {
66 if (parent.getClass().equals(parentClass)) {
67 return (T) parent;
68 }
69 parent = parent.getParent();
70 }
71 return null;
72 }
73
Winson Chung303e1ff2014-03-07 15:06:19 -080074 /** Scales a rect about its centroid */
Winson3150e572015-10-23 15:07:24 -070075 public static void scaleRectAboutCenter(RectF r, float scale) {
Winson Chung303e1ff2014-03-07 15:06:19 -080076 if (scale != 1.0f) {
Winson3150e572015-10-23 15:07:24 -070077 float cx = r.centerX();
78 float cy = r.centerY();
Winson Chung303e1ff2014-03-07 15:06:19 -080079 r.offset(-cx, -cy);
Winson3150e572015-10-23 15:07:24 -070080 r.left *= scale;
81 r.top *= scale;
82 r.right *= scale;
83 r.bottom *= scale;
Winson Chung303e1ff2014-03-07 15:06:19 -080084 r.offset(cx, cy);
85 }
86 }
Winson Chungf5e22e72014-05-02 18:35:35 -070087
Winson Chung93748a12014-07-13 17:43:31 -070088 /** Calculates the constrast between two colors, using the algorithm provided by the WCAG v2. */
89 public static float computeContrastBetweenColors(int bg, int fg) {
90 float bgR = Color.red(bg) / 255f;
91 float bgG = Color.green(bg) / 255f;
92 float bgB = Color.blue(bg) / 255f;
93 bgR = (bgR < 0.03928f) ? bgR / 12.92f : (float) Math.pow((bgR + 0.055f) / 1.055f, 2.4f);
94 bgG = (bgG < 0.03928f) ? bgG / 12.92f : (float) Math.pow((bgG + 0.055f) / 1.055f, 2.4f);
95 bgB = (bgB < 0.03928f) ? bgB / 12.92f : (float) Math.pow((bgB + 0.055f) / 1.055f, 2.4f);
96 float bgL = 0.2126f * bgR + 0.7152f * bgG + 0.0722f * bgB;
97
98 float fgR = Color.red(fg) / 255f;
99 float fgG = Color.green(fg) / 255f;
100 float fgB = Color.blue(fg) / 255f;
101 fgR = (fgR < 0.03928f) ? fgR / 12.92f : (float) Math.pow((fgR + 0.055f) / 1.055f, 2.4f);
102 fgG = (fgG < 0.03928f) ? fgG / 12.92f : (float) Math.pow((fgG + 0.055f) / 1.055f, 2.4f);
103 fgB = (fgB < 0.03928f) ? fgB / 12.92f : (float) Math.pow((fgB + 0.055f) / 1.055f, 2.4f);
104 float fgL = 0.2126f * fgR + 0.7152f * fgG + 0.0722f * fgB;
Winson Chungf5e22e72014-05-02 18:35:35 -0700105
Winson Chung93748a12014-07-13 17:43:31 -0700106 return Math.abs((fgL + 0.05f) / (bgL + 0.05f));
Winson Chungf5e22e72014-05-02 18:35:35 -0700107 }
Winson Chungcdbbb7e2014-06-24 12:11:49 -0700108
Winson Chunga0e88b52014-08-11 19:25:42 -0700109 /** Returns the base color overlaid with another overlay color with a specified alpha. */
110 public static int getColorWithOverlay(int baseColor, int overlayColor, float overlayAlpha) {
111 return Color.rgb(
112 (int) (overlayAlpha * Color.red(baseColor) +
113 (1f - overlayAlpha) * Color.red(overlayColor)),
114 (int) (overlayAlpha * Color.green(baseColor) +
115 (1f - overlayAlpha) * Color.green(overlayColor)),
116 (int) (overlayAlpha * Color.blue(baseColor) +
117 (1f - overlayAlpha) * Color.blue(overlayColor)));
118 }
119
Winson Chung353c0b92014-10-16 17:43:23 -0700120 /**
121 * Cancels an animation ensuring that if it has listeners, onCancel and onEnd
122 * are not called.
123 */
124 public static void cancelAnimationWithoutCallbacks(Animator animator) {
125 if (animator != null) {
126 animator.removeAllListeners();
127 animator.cancel();
128 }
129 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800130}