• 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.compose.theme
18 
19 import android.annotation.ColorInt
20 import android.content.Context
21 import androidx.compose.runtime.staticCompositionLocalOf
22 import androidx.compose.ui.graphics.Color
23 import com.android.internal.R
24 
25 /** CompositionLocal used to pass [AndroidColorScheme] down the tree. */
26 val LocalAndroidColorScheme =
<lambda>null27     staticCompositionLocalOf<AndroidColorScheme> {
28         throw IllegalStateException(
29             "No AndroidColorScheme configured. Make sure to use LocalAndroidColorScheme in a " +
30                 "Composable surrounded by a PlatformTheme {}."
31         )
32     }
33 
34 /**
35  * The Android color scheme.
36  *
37  * Important: Use M3 colors from MaterialTheme.colorScheme whenever possible instead. In the future,
38  * most of the colors in this class will be removed in favor of their M3 counterpart.
39  */
40 class AndroidColorScheme(context: Context) {
41     val onSecondaryFixedVariant = getColor(context, R.attr.materialColorOnSecondaryFixedVariant)
42     val onTertiaryFixedVariant = getColor(context, R.attr.materialColorOnTertiaryFixedVariant)
43     val surfaceContainerLowest = getColor(context, R.attr.materialColorSurfaceContainerLowest)
44     val onPrimaryFixedVariant = getColor(context, R.attr.materialColorOnPrimaryFixedVariant)
45     val onSecondaryContainer = getColor(context, R.attr.materialColorOnSecondaryContainer)
46     val onTertiaryContainer = getColor(context, R.attr.materialColorOnTertiaryContainer)
47     val surfaceContainerLow = getColor(context, R.attr.materialColorSurfaceContainerLow)
48     val onPrimaryContainer = getColor(context, R.attr.materialColorOnPrimaryContainer)
49     val secondaryFixedDim = getColor(context, R.attr.materialColorSecondaryFixedDim)
50     val onErrorContainer = getColor(context, R.attr.materialColorOnErrorContainer)
51     val onSecondaryFixed = getColor(context, R.attr.materialColorOnSecondaryFixed)
52     val onSurfaceInverse = getColor(context, R.attr.materialColorOnSurfaceInverse)
53     val tertiaryFixedDim = getColor(context, R.attr.materialColorTertiaryFixedDim)
54     val onTertiaryFixed = getColor(context, R.attr.materialColorOnTertiaryFixed)
55     val primaryFixedDim = getColor(context, R.attr.materialColorPrimaryFixedDim)
56     val secondaryContainer = getColor(context, R.attr.materialColorSecondaryContainer)
57     val errorContainer = getColor(context, R.attr.materialColorErrorContainer)
58     val onPrimaryFixed = getColor(context, R.attr.materialColorOnPrimaryFixed)
59     val primaryInverse = getColor(context, R.attr.materialColorPrimaryInverse)
60     val secondaryFixed = getColor(context, R.attr.materialColorSecondaryFixed)
61     val surfaceInverse = getColor(context, R.attr.materialColorSurfaceInverse)
62     val surfaceVariant = getColor(context, R.attr.materialColorSurfaceVariant)
63     val tertiaryContainer = getColor(context, R.attr.materialColorTertiaryContainer)
64     val tertiaryFixed = getColor(context, R.attr.materialColorTertiaryFixed)
65     val primaryContainer = getColor(context, R.attr.materialColorPrimaryContainer)
66     val onBackground = getColor(context, R.attr.materialColorOnBackground)
67     val primaryFixed = getColor(context, R.attr.materialColorPrimaryFixed)
68     val onSecondary = getColor(context, R.attr.materialColorOnSecondary)
69     val onTertiary = getColor(context, R.attr.materialColorOnTertiary)
70     val surfaceDim = getColor(context, R.attr.materialColorSurfaceDim)
71     val surfaceBright = getColor(context, R.attr.materialColorSurfaceBright)
72     val error = getColor(context, R.attr.materialColorError)
73     val onError = getColor(context, R.attr.materialColorOnError)
74     val surface = getColor(context, R.attr.materialColorSurface)
75     val surfaceContainerHigh = getColor(context, R.attr.materialColorSurfaceContainerHigh)
76     val surfaceContainerHighest = getColor(context, R.attr.materialColorSurfaceContainerHighest)
77     val onSurfaceVariant = getColor(context, R.attr.materialColorOnSurfaceVariant)
78     val outline = getColor(context, R.attr.materialColorOutline)
79     val outlineVariant = getColor(context, R.attr.materialColorOutlineVariant)
80     val onPrimary = getColor(context, R.attr.materialColorOnPrimary)
81     val onSurface = getColor(context, R.attr.materialColorOnSurface)
82     val surfaceContainer = getColor(context, R.attr.materialColorSurfaceContainer)
83     val primary = getColor(context, R.attr.materialColorPrimary)
84     val secondary = getColor(context, R.attr.materialColorSecondary)
85     val tertiary = getColor(context, R.attr.materialColorTertiary)
86 
87     @Deprecated("Use the new android tokens: go/sysui-colors")
88     val deprecated = DeprecatedValues(context)
89 
90     class DeprecatedValues(context: Context) {
91         val colorPrimary = getColor(context, R.attr.colorPrimary)
92         val colorPrimaryDark = getColor(context, R.attr.colorPrimaryDark)
93         val colorAccent = getColor(context, R.attr.colorAccent)
94         val colorAccentPrimary = getColor(context, R.attr.colorAccentPrimary)
95         val colorAccentSecondary = getColor(context, R.attr.colorAccentSecondary)
96         val colorAccentTertiary = getColor(context, R.attr.colorAccentTertiary)
97         val colorAccentPrimaryVariant = getColor(context, R.attr.colorAccentPrimaryVariant)
98         val colorAccentSecondaryVariant = getColor(context, R.attr.colorAccentSecondaryVariant)
99         val colorAccentTertiaryVariant = getColor(context, R.attr.colorAccentTertiaryVariant)
100         val colorSurface = getColor(context, R.attr.colorSurface)
101         val colorSurfaceHighlight = getColor(context, R.attr.colorSurfaceHighlight)
102         val colorSurfaceVariant = getColor(context, R.attr.colorSurfaceVariant)
103         val colorSurfaceHeader = getColor(context, R.attr.colorSurfaceHeader)
104         val colorError = getColor(context, R.attr.colorError)
105         val colorBackground = getColor(context, R.attr.colorBackground)
106         val colorBackgroundFloating = getColor(context, R.attr.colorBackgroundFloating)
107         val panelColorBackground = getColor(context, R.attr.panelColorBackground)
108         val textColorPrimary = getColor(context, R.attr.textColorPrimary)
109         val textColorSecondary = getColor(context, R.attr.textColorSecondary)
110         val textColorTertiary = getColor(context, R.attr.textColorTertiary)
111         val textColorPrimaryInverse = getColor(context, R.attr.textColorPrimaryInverse)
112         val textColorSecondaryInverse = getColor(context, R.attr.textColorSecondaryInverse)
113         val textColorTertiaryInverse = getColor(context, R.attr.textColorTertiaryInverse)
114         val textColorOnAccent = getColor(context, R.attr.textColorOnAccent)
115         val colorForeground = getColor(context, R.attr.colorForeground)
116         val colorForegroundInverse = getColor(context, R.attr.colorForegroundInverse)
117     }
118 
119     companion object {
getColornull120         fun getColor(context: Context, attr: Int): Color {
121             val ta = context.obtainStyledAttributes(intArrayOf(attr))
122             @ColorInt val color = ta.getColor(0, 0)
123             ta.recycle()
124             return Color(color)
125         }
126     }
127 }
128