Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 1 | /* |
| 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 Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 17 | package com.android.systemui.recents.misc; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 18 | |
Winson Chung | 353c0b9 | 2014-10-16 17:43:23 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
Winson Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 20 | import android.graphics.Color; |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame^] | 21 | import android.graphics.Rect; |
Winson | 3150e57 | 2015-10-23 15:07:24 -0700 | [diff] [blame] | 22 | import android.graphics.RectF; |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame^] | 23 | import android.graphics.drawable.Drawable; |
| 24 | import android.util.IntProperty; |
| 25 | import android.util.Property; |
Winson Chung | bf5dbf1 | 2014-09-16 00:58:25 +0200 | [diff] [blame] | 26 | import android.view.View; |
Winson | be7607a | 2015-10-01 17:24:51 -0700 | [diff] [blame] | 27 | import android.view.ViewParent; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 28 | |
| 29 | /* Common code */ |
| 30 | public class Utilities { |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 31 | |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame^] | 32 | 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 | |
Winson | be7607a | 2015-10-01 17:24:51 -0700 | [diff] [blame] | 58 | /** |
| 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 Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 74 | /** Scales a rect about its centroid */ |
Winson | 3150e57 | 2015-10-23 15:07:24 -0700 | [diff] [blame] | 75 | public static void scaleRectAboutCenter(RectF r, float scale) { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 76 | if (scale != 1.0f) { |
Winson | 3150e57 | 2015-10-23 15:07:24 -0700 | [diff] [blame] | 77 | float cx = r.centerX(); |
| 78 | float cy = r.centerY(); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 79 | r.offset(-cx, -cy); |
Winson | 3150e57 | 2015-10-23 15:07:24 -0700 | [diff] [blame] | 80 | r.left *= scale; |
| 81 | r.top *= scale; |
| 82 | r.right *= scale; |
| 83 | r.bottom *= scale; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 84 | r.offset(cx, cy); |
| 85 | } |
| 86 | } |
Winson Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 87 | |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 88 | /** 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 Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 105 | |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 106 | return Math.abs((fgL + 0.05f) / (bgL + 0.05f)); |
Winson Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 107 | } |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 108 | |
Winson Chung | a0e88b5 | 2014-08-11 19:25:42 -0700 | [diff] [blame] | 109 | /** 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 Chung | 353c0b9 | 2014-10-16 17:43:23 -0700 | [diff] [blame] | 120 | /** |
| 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 Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 130 | } |