• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.util
18 
19 import android.content.res.TypedArray
20 import android.graphics.Color
21 import android.view.ContextThemeWrapper
22 
23 /** Returns an ARGB color version of [color] at the given [alpha]. */
getColorWithAlphanull24 fun getColorWithAlpha(color: Int, alpha: Float): Int =
25     Color.argb(
26         (alpha * 255).toInt(),
27         Color.red(color),
28         Color.green(color),
29         Color.blue(color)
30     )
31 
32 
33 /**
34  * Returns the color provided at the specified {@param attrIndex} in {@param a} if it exists,
35  * otherwise, returns the color from the private attribute {@param privAttrId}.
36  */
37 fun getPrivateAttrColorIfUnset(
38     ctw: ContextThemeWrapper, attrArray: TypedArray,
39     attrIndex: Int, defColor: Int, privAttrId: Int
40 ): Int {
41     // If the index is specified, use that value
42     var a = attrArray
43     if (a.hasValue(attrIndex)) {
44         return a.getColor(attrIndex, defColor)
45     }
46 
47     // Otherwise fallback to the value of the private attribute
48     val customAttrs = intArrayOf(privAttrId)
49     a = ctw.obtainStyledAttributes(customAttrs)
50     val color = a.getColor(0, defColor)
51     a.recycle()
52     return color
53 }
54