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 17 package com.android.launcher3.util; 18 19 import android.content.Context; 20 import android.content.res.TypedArray; 21 import android.graphics.Color; 22 import android.graphics.ColorMatrix; 23 import android.graphics.drawable.Drawable; 24 25 /** 26 * Various utility methods associated with theming. 27 */ 28 public class Themes { 29 getColorAccent(Context context)30 public static int getColorAccent(Context context) { 31 return getAttrColor(context, android.R.attr.colorAccent); 32 } 33 getAttrColor(Context context, int attr)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 getAttrBoolean(Context context, int attr)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 getAttrDrawable(Context context, int attr)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 /** 56 * Returns the alpha corresponding to the theme attribute {@param attr}, in the range [0, 255]. 57 */ getAlpha(Context context, int attr)58 public static int getAlpha(Context context, int attr) { 59 TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); 60 float alpha = ta.getFloat(0, 0); 61 ta.recycle(); 62 return (int) (255 * alpha + 0.5f); 63 } 64 65 /** 66 * Scales a color matrix such that, when applied to color R G B A, it produces R' G' B' A' where 67 * R' = r * R 68 * G' = g * G 69 * B' = b * B 70 * A' = a * A 71 * 72 * The matrix will, for instance, turn white into r g b a, and black will remain black. 73 * 74 * @param color The color r g b a 75 * @param target The ColorMatrix to scale 76 */ setColorScaleOnMatrix(int color, ColorMatrix target)77 public static void setColorScaleOnMatrix(int color, ColorMatrix target) { 78 target.setScale(Color.red(color) / 255f, Color.green(color) / 255f, 79 Color.blue(color) / 255f, Color.alpha(color) / 255f); 80 } 81 } 82