1 /* 2 * Copyright (C) 2018 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 com.android.systemui.shared.recents.utilities; 18 19 import android.graphics.Color; 20 import android.os.Handler; 21 import android.os.Message; 22 23 /* Common code */ 24 public class Utilities { 25 26 /** 27 * Posts a runnable on a handler at the front of the queue ignoring any sync barriers. 28 */ postAtFrontOfQueueAsynchronously(Handler h, Runnable r)29 public static void postAtFrontOfQueueAsynchronously(Handler h, Runnable r) { 30 Message msg = h.obtainMessage().setCallback(r); 31 h.sendMessageAtFrontOfQueue(msg); 32 } 33 34 /** Calculates the constrast between two colors, using the algorithm provided by the WCAG v2. */ computeContrastBetweenColors(int bg, int fg)35 public static float computeContrastBetweenColors(int bg, int fg) { 36 float bgR = Color.red(bg) / 255f; 37 float bgG = Color.green(bg) / 255f; 38 float bgB = Color.blue(bg) / 255f; 39 bgR = (bgR < 0.03928f) ? bgR / 12.92f : (float) Math.pow((bgR + 0.055f) / 1.055f, 2.4f); 40 bgG = (bgG < 0.03928f) ? bgG / 12.92f : (float) Math.pow((bgG + 0.055f) / 1.055f, 2.4f); 41 bgB = (bgB < 0.03928f) ? bgB / 12.92f : (float) Math.pow((bgB + 0.055f) / 1.055f, 2.4f); 42 float bgL = 0.2126f * bgR + 0.7152f * bgG + 0.0722f * bgB; 43 44 float fgR = Color.red(fg) / 255f; 45 float fgG = Color.green(fg) / 255f; 46 float fgB = Color.blue(fg) / 255f; 47 fgR = (fgR < 0.03928f) ? fgR / 12.92f : (float) Math.pow((fgR + 0.055f) / 1.055f, 2.4f); 48 fgG = (fgG < 0.03928f) ? fgG / 12.92f : (float) Math.pow((fgG + 0.055f) / 1.055f, 2.4f); 49 fgB = (fgB < 0.03928f) ? fgB / 12.92f : (float) Math.pow((fgB + 0.055f) / 1.055f, 2.4f); 50 float fgL = 0.2126f * fgR + 0.7152f * fgG + 0.0722f * fgB; 51 52 return Math.abs((fgL + 0.05f) / (bgL + 0.05f)); 53 } 54 55 /** 56 * @return the clamped {@param value} between the provided {@param min} and {@param max}. 57 */ clamp(float value, float min, float max)58 public static float clamp(float value, float min, float max) { 59 return Math.max(min, Math.min(max, value)); 60 } 61 } 62