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 internal constructor(context: Context) { 41 val colorPrimary = getColor(context, R.attr.colorPrimary) 42 val colorPrimaryDark = getColor(context, R.attr.colorPrimaryDark) 43 val colorAccent = getColor(context, R.attr.colorAccent) 44 val colorAccentPrimary = getColor(context, R.attr.colorAccentPrimary) 45 val colorAccentSecondary = getColor(context, R.attr.colorAccentSecondary) 46 val colorAccentTertiary = getColor(context, R.attr.colorAccentTertiary) 47 val colorAccentPrimaryVariant = getColor(context, R.attr.colorAccentPrimaryVariant) 48 val colorAccentSecondaryVariant = getColor(context, R.attr.colorAccentSecondaryVariant) 49 val colorAccentTertiaryVariant = getColor(context, R.attr.colorAccentTertiaryVariant) 50 val colorSurface = getColor(context, R.attr.colorSurface) 51 val colorSurfaceHighlight = getColor(context, R.attr.colorSurfaceHighlight) 52 val colorSurfaceVariant = getColor(context, R.attr.colorSurfaceVariant) 53 val colorSurfaceHeader = getColor(context, R.attr.colorSurfaceHeader) 54 val colorError = getColor(context, R.attr.colorError) 55 val colorBackground = getColor(context, R.attr.colorBackground) 56 val colorBackgroundFloating = getColor(context, R.attr.colorBackgroundFloating) 57 val panelColorBackground = getColor(context, R.attr.panelColorBackground) 58 val textColorPrimary = getColor(context, R.attr.textColorPrimary) 59 val textColorSecondary = getColor(context, R.attr.textColorSecondary) 60 val textColorTertiary = getColor(context, R.attr.textColorTertiary) 61 val textColorPrimaryInverse = getColor(context, R.attr.textColorPrimaryInverse) 62 val textColorSecondaryInverse = getColor(context, R.attr.textColorSecondaryInverse) 63 val textColorTertiaryInverse = getColor(context, R.attr.textColorTertiaryInverse) 64 val textColorOnAccent = getColor(context, R.attr.textColorOnAccent) 65 val colorForeground = getColor(context, R.attr.colorForeground) 66 val colorForegroundInverse = getColor(context, R.attr.colorForegroundInverse) 67 68 companion object { getColornull69 fun getColor(context: Context, attr: Int): Color { 70 val ta = context.obtainStyledAttributes(intArrayOf(attr)) 71 @ColorInt val color = ta.getColor(0, 0) 72 ta.recycle() 73 return Color(color) 74 } 75 } 76 } 77