1/* 2 * Copyright (C) 2017 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 17package com.android.launcher3.util; 18 19import android.content.Context; 20import android.content.res.TypedArray; 21import android.graphics.Color; 22import android.graphics.ColorMatrix; 23import android.graphics.drawable.Drawable; 24 25/** 26 * Various utility methods associated with theming. 27 */ 28public class Themes { 29 30 public static int getColorAccent(Context context) { 31 return getAttrColor(context, android.R.attr.colorAccent); 32 } 33 34 public static int getAttrColor(Context context, int attr) { 35 TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); 36 int colorAccent = ta.getColor(0, 0); 37 ta.recycle(); 38 return colorAccent; 39 } 40 41 public static boolean getAttrBoolean(Context context, int attr) { 42 TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); 43 boolean value = ta.getBoolean(0, false); 44 ta.recycle(); 45 return value; 46 } 47 48 public static Drawable getAttrDrawable(Context context, int attr) { 49 TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); 50 Drawable value = ta.getDrawable(0); 51 ta.recycle(); 52 return value; 53 } 54 55 public static int getAttrInteger(Context context, int attr) { 56 TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); 57 int value = ta.getInteger(0, 0); 58 ta.recycle(); 59 return value; 60 } 61 62 /** 63 * Returns the alpha corresponding to the theme attribute {@param attr}, in the range [0, 255]. 64 */ 65 public static int getAlpha(Context context, int attr) { 66 TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); 67 float alpha = ta.getFloat(0, 0); 68 ta.recycle(); 69 return (int) (255 * alpha + 0.5f); 70 } 71 72 /** 73 * Scales a color matrix such that, when applied to color R G B A, it produces R' G' B' A' where 74 * R' = r * R 75 * G' = g * G 76 * B' = b * B 77 * A' = a * A 78 * 79 * The matrix will, for instance, turn white into r g b a, and black will remain black. 80 * 81 * @param color The color r g b a 82 * @param target The ColorMatrix to scale 83 */ 84 public static void setColorScaleOnMatrix(int color, ColorMatrix target) { 85 target.setScale(Color.red(color) / 255f, Color.green(color) / 255f, 86 Color.blue(color) / 255f, Color.alpha(color) / 255f); 87 } 88 89 /** 90 * Changes a color matrix such that, when applied to srcColor, it produces dstColor. 91 * 92 * Note that values on the last column of target ColorMatrix can be negative, and may result in 93 * negative values when applied on a color. Such negative values will be automatically shifted 94 * up to 0 by the framework. 95 * 96 * @param srcColor The color to start from 97 * @param dstColor The color to create by applying target on srcColor 98 * @param target The ColorMatrix to transform the color 99 */ 100 public static void setColorChangeOnMatrix(int srcColor, int dstColor, ColorMatrix target) { 101 target.reset(); 102 target.getArray()[4] = Color.red(dstColor) - Color.red(srcColor); 103 target.getArray()[9] = Color.green(dstColor) - Color.green(srcColor); 104 target.getArray()[14] = Color.blue(dstColor) - Color.blue(srcColor); 105 target.getArray()[19] = Color.alpha(dstColor) - Color.alpha(srcColor); 106 } 107} 108