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.content.Context 20 import androidx.annotation.ColorRes 21 import androidx.compose.runtime.Immutable 22 import androidx.compose.runtime.staticCompositionLocalOf 23 import androidx.compose.ui.graphics.Color 24 import com.android.internal.R 25 26 /** CompositionLocal used to pass [AndroidColorScheme] down the tree. */ 27 val LocalAndroidColorScheme = <lambda>null28 staticCompositionLocalOf<AndroidColorScheme> { 29 throw IllegalStateException( 30 "No AndroidColorScheme configured. Make sure to use LocalAndroidColorScheme in a " + 31 "Composable surrounded by a PlatformTheme {}." 32 ) 33 } 34 35 /** 36 * The Android color scheme. 37 * 38 * This scheme contains the Material3 colors that are not available on 39 * [androidx.compose.material3.MaterialTheme]. For other colors (e.g. primary), use 40 * `MaterialTheme.colorScheme` instead. 41 */ 42 @Immutable 43 class AndroidColorScheme( 44 // fixed tokens 45 val primaryFixed: Color, 46 val primaryFixedDim: Color, 47 val onPrimaryFixed: Color, 48 val onPrimaryFixedVariant: Color, 49 val secondaryFixed: Color, 50 val secondaryFixedDim: Color, 51 val onSecondaryFixed: Color, 52 val onSecondaryFixedVariant: Color, 53 val tertiaryFixed: Color, 54 val tertiaryFixedDim: Color, 55 val onTertiaryFixed: Color, 56 val onTertiaryFixedVariant: Color, 57 58 // custom tokens 59 val brandA: Color, 60 val brandB: Color, 61 val brandC: Color, 62 val brandD: Color, 63 val clockHour: Color, 64 val clockMinute: Color, 65 val clockSecond: Color, 66 val onShadeActive: Color, 67 val onShadeActiveVariant: Color, 68 val onShadeInactive: Color, 69 val onShadeInactiveVariant: Color, 70 val onThemeApp: Color, 71 val overviewBackground: Color, 72 val shadeActive: Color, 73 val shadeDisabled: Color, 74 val shadeInactive: Color, 75 val themeApp: Color, 76 val themeAppRing: Color, 77 val themeNotif: Color, 78 val underSurface: Color, 79 val weatherTemp: Color, 80 val widgetBackground: Color, 81 val surfaceEffect0: Color, 82 val surfaceEffect1: Color, 83 val surfaceEffect2: Color, 84 val surfaceEffect3: Color, 85 ) { 86 companion object { colornull87 internal fun color(context: Context, @ColorRes id: Int): Color { 88 return Color(context.resources.getColor(id, context.theme)) 89 } 90 invokenull91 operator fun invoke(context: Context): AndroidColorScheme { 92 return AndroidColorScheme( 93 // Fixed tokens. 94 primaryFixed = color(context, R.color.system_primary_fixed), 95 primaryFixedDim = color(context, R.color.system_primary_fixed_dim), 96 onPrimaryFixed = color(context, R.color.system_on_primary_fixed), 97 onPrimaryFixedVariant = color(context, R.color.system_on_primary_fixed_variant), 98 secondaryFixed = color(context, R.color.system_secondary_fixed), 99 secondaryFixedDim = color(context, R.color.system_secondary_fixed_dim), 100 onSecondaryFixed = color(context, R.color.system_on_secondary_fixed), 101 onSecondaryFixedVariant = color(context, R.color.system_on_secondary_fixed_variant), 102 tertiaryFixed = color(context, R.color.system_tertiary_fixed), 103 tertiaryFixedDim = color(context, R.color.system_tertiary_fixed_dim), 104 onTertiaryFixed = color(context, R.color.system_on_tertiary_fixed), 105 onTertiaryFixedVariant = color(context, R.color.system_on_tertiary_fixed_variant), 106 107 // Custom tokens. 108 brandA = color(context, R.color.customColorBrandA), 109 brandB = color(context, R.color.customColorBrandB), 110 brandC = color(context, R.color.customColorBrandC), 111 brandD = color(context, R.color.customColorBrandD), 112 clockHour = color(context, R.color.customColorClockHour), 113 clockMinute = color(context, R.color.customColorClockMinute), 114 clockSecond = color(context, R.color.customColorClockSecond), 115 onShadeActive = color(context, R.color.customColorOnShadeActive), 116 onShadeActiveVariant = color(context, R.color.customColorOnShadeActiveVariant), 117 onShadeInactive = color(context, R.color.customColorOnShadeInactive), 118 onShadeInactiveVariant = color(context, R.color.customColorOnShadeInactiveVariant), 119 onThemeApp = color(context, R.color.customColorOnThemeApp), 120 overviewBackground = color(context, R.color.customColorOverviewBackground), 121 shadeActive = color(context, R.color.customColorShadeActive), 122 shadeDisabled = color(context, R.color.customColorShadeDisabled), 123 shadeInactive = color(context, R.color.customColorShadeInactive), 124 themeApp = color(context, R.color.customColorThemeApp), 125 themeAppRing = color(context, R.color.customColorThemeAppRing), 126 themeNotif = color(context, R.color.customColorThemeNotif), 127 underSurface = color(context, R.color.customColorUnderSurface), 128 weatherTemp = color(context, R.color.customColorWeatherTemp), 129 widgetBackground = color(context, R.color.customColorWidgetBackground), 130 surfaceEffect0 = color(context, R.color.surface_effect_0), 131 surfaceEffect1 = color(context, R.color.surface_effect_1), 132 surfaceEffect2 = color(context, R.color.surface_effect_2), 133 surfaceEffect3 = color(context, R.color.surface_effect_3), 134 ) 135 } 136 } 137 } 138