11f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal/*
21f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal * Copyright (C) 2017 The Android Open Source Project
31f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal *
41f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal * Licensed under the Apache License, Version 2.0 (the "License");
51f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal * you may not use this file except in compliance with the License.
61f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal * You may obtain a copy of the License at
71f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal *
81f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal *      http://www.apache.org/licenses/LICENSE-2.0
91f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal *
101f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal * Unless required by applicable law or agreed to in writing, software
111f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal * distributed under the License is distributed on an "AS IS" BASIS,
121f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal * See the License for the specific language governing permissions and
141f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal * limitations under the License.
151f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal */
161f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal
171f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyalpackage com.android.launcher3.util;
181f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal
191f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyalimport android.content.Context;
201f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyalimport android.content.res.TypedArray;
216c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickhamimport android.graphics.Color;
226c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickhamimport android.graphics.ColorMatrix;
23a69369450281376cb207be554199bb4895843eb2Mario Bertschlerimport android.graphics.drawable.Drawable;
241f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal
251f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal/**
261f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal * Various utility methods associated with theming.
271f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal */
281f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyalpublic class Themes {
291f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal
301f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal    public static int getColorAccent(Context context) {
311f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal        return getAttrColor(context, android.R.attr.colorAccent);
321f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal    }
331f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal
341f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal    public static int getAttrColor(Context context, int attr) {
351f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal        TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
361f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal        int colorAccent = ta.getColor(0, 0);
371f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal        ta.recycle();
381f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal        return colorAccent;
391f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal    }
401f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal
41368ae772018c12349d7bf1b27d8817e7644b08c2Sunny Goyal    public static boolean getAttrBoolean(Context context, int attr) {
42368ae772018c12349d7bf1b27d8817e7644b08c2Sunny Goyal        TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
43368ae772018c12349d7bf1b27d8817e7644b08c2Sunny Goyal        boolean value = ta.getBoolean(0, false);
44368ae772018c12349d7bf1b27d8817e7644b08c2Sunny Goyal        ta.recycle();
45a69369450281376cb207be554199bb4895843eb2Mario Bertschler        return value;
46a69369450281376cb207be554199bb4895843eb2Mario Bertschler    }
47a69369450281376cb207be554199bb4895843eb2Mario Bertschler
48a69369450281376cb207be554199bb4895843eb2Mario Bertschler    public static Drawable getAttrDrawable(Context context, int attr) {
49a69369450281376cb207be554199bb4895843eb2Mario Bertschler        TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
50a69369450281376cb207be554199bb4895843eb2Mario Bertschler        Drawable value = ta.getDrawable(0);
51a69369450281376cb207be554199bb4895843eb2Mario Bertschler        ta.recycle();
52368ae772018c12349d7bf1b27d8817e7644b08c2Sunny Goyal        return value;
53368ae772018c12349d7bf1b27d8817e7644b08c2Sunny Goyal    }
54368ae772018c12349d7bf1b27d8817e7644b08c2Sunny Goyal
551f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal    /**
561f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal     * Returns the alpha corresponding to the theme attribute {@param attr}, in the range [0, 255].
571f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal     */
581f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal    public static int getAlpha(Context context, int attr) {
591f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal        TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
601f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal        float alpha = ta.getFloat(0, 0);
611f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal        ta.recycle();
621f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal        return (int) (255 * alpha + 0.5f);
631f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal    }
646c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickham
656c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickham    /**
666c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickham     * Scales a color matrix such that, when applied to color R G B A, it produces R' G' B' A' where
676c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickham     * R' = r * R
686c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickham     * G' = g * G
696c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickham     * B' = b * B
706c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickham     * A' = a * A
716c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickham     *
726c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickham     * The matrix will, for instance, turn white into r g b a, and black will remain black.
736c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickham     *
746c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickham     * @param color The color r g b a
756c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickham     * @param target The ColorMatrix to scale
766c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickham     */
776c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickham    public static void setColorScaleOnMatrix(int color, ColorMatrix target) {
786c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickham        target.setScale(Color.red(color) / 255f, Color.green(color) / 255f,
796c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickham                Color.blue(color) / 255f, Color.alpha(color) / 255f);
806c82867a7ea3a95a19d6cec7d1fe164d234d2cb6Tony Wickham    }
811f3f07d47c29cba3b70bcd15ebb65a077f55a558Sunny Goyal}
82