2 * Copyright (C) 2014 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 com.android.systemui.recents.misc;
19 import android.animation.Animator;
20 import android.graphics.Color;
21 import android.graphics.Matrix;
22 import android.graphics.Rect;
23 import android.view.View;
25 import java.util.ArrayList;
28 public class Utilities {
30 /** Scales a rect about its centroid */
31 public static void scaleRectAboutCenter(Rect r, float scale) {
36 r.left = (int) (r.left * scale + 0.5f);
37 r.top = (int) (r.top * scale + 0.5f);
38 r.right = (int) (r.right * scale + 0.5f);
39 r.bottom = (int) (r.bottom * scale + 0.5f);
44 /** Maps a coorindate in a descendant view into the parent. */
45 public static float mapCoordInDescendentToSelf(View descendant, View root,
46 float[] coord, boolean includeRootScroll) {
47 ArrayList<View> ancestorChain = new ArrayList<View>();
49 float[] pt = {coord[0], coord[1]};
52 while(v != root && v != null) {
54 v = (View) v.getParent();
56 ancestorChain.add(root);
59 int count = ancestorChain.size();
60 for (int i = 0; i < count; i++) {
61 View v0 = ancestorChain.get(i);
62 // For TextViews, scroll has a meaning which relates to the text position
63 // which is very strange... ignore the scroll.
64 if (v0 != descendant || includeRootScroll) {
65 pt[0] -= v0.getScrollX();
66 pt[1] -= v0.getScrollY();
69 v0.getMatrix().mapPoints(pt);
70 pt[0] += v0.getLeft();
72 scale *= v0.getScaleX();
80 /** Maps a coordinate in the root to a descendent. */
81 public static float mapCoordInSelfToDescendent(View descendant, View root,
82 float[] coord, Matrix tmpInverseMatrix) {
83 ArrayList<View> ancestorChain = new ArrayList<View>();
85 float[] pt = {coord[0], coord[1]};
90 v = (View) v.getParent();
92 ancestorChain.add(root);
95 int count = ancestorChain.size();
96 tmpInverseMatrix.set(Matrix.IDENTITY_MATRIX);
97 for (int i = count - 1; i >= 0; i--) {
98 View ancestor = ancestorChain.get(i);
99 View next = i > 0 ? ancestorChain.get(i-1) : null;
101 pt[0] += ancestor.getScrollX();
102 pt[1] += ancestor.getScrollY();
105 pt[0] -= next.getLeft();
106 pt[1] -= next.getTop();
107 next.getMatrix().invert(tmpInverseMatrix);
108 tmpInverseMatrix.mapPoints(pt);
109 scale *= next.getScaleX();
118 /** Calculates the constrast between two colors, using the algorithm provided by the WCAG v2. */
119 public static float computeContrastBetweenColors(int bg, int fg) {
120 float bgR = Color.red(bg) / 255f;
121 float bgG = Color.green(bg) / 255f;
122 float bgB = Color.blue(bg) / 255f;
123 bgR = (bgR < 0.03928f) ? bgR / 12.92f : (float) Math.pow((bgR + 0.055f) / 1.055f, 2.4f);
124 bgG = (bgG < 0.03928f) ? bgG / 12.92f : (float) Math.pow((bgG + 0.055f) / 1.055f, 2.4f);
125 bgB = (bgB < 0.03928f) ? bgB / 12.92f : (float) Math.pow((bgB + 0.055f) / 1.055f, 2.4f);
126 float bgL = 0.2126f * bgR + 0.7152f * bgG + 0.0722f * bgB;
128 float fgR = Color.red(fg) / 255f;
129 float fgG = Color.green(fg) / 255f;
130 float fgB = Color.blue(fg) / 255f;
131 fgR = (fgR < 0.03928f) ? fgR / 12.92f : (float) Math.pow((fgR + 0.055f) / 1.055f, 2.4f);
132 fgG = (fgG < 0.03928f) ? fgG / 12.92f : (float) Math.pow((fgG + 0.055f) / 1.055f, 2.4f);
133 fgB = (fgB < 0.03928f) ? fgB / 12.92f : (float) Math.pow((fgB + 0.055f) / 1.055f, 2.4f);
134 float fgL = 0.2126f * fgR + 0.7152f * fgG + 0.0722f * fgB;
136 return Math.abs((fgL + 0.05f) / (bgL + 0.05f));
139 /** Returns the base color overlaid with another overlay color with a specified alpha. */
140 public static int getColorWithOverlay(int baseColor, int overlayColor, float overlayAlpha) {
142 (int) (overlayAlpha * Color.red(baseColor) +
143 (1f - overlayAlpha) * Color.red(overlayColor)),
144 (int) (overlayAlpha * Color.green(baseColor) +
145 (1f - overlayAlpha) * Color.green(overlayColor)),
146 (int) (overlayAlpha * Color.blue(baseColor) +
147 (1f - overlayAlpha) * Color.blue(overlayColor)));
151 * Cancels an animation ensuring that if it has listeners, onCancel and onEnd
154 public static void cancelAnimationWithoutCallbacks(Animator animator) {
155 if (animator != null) {
156 animator.removeAllListeners();