Connie Shi | babbda4 | 2021-08-11 17:14:22 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 androidx.compose.material3 |
| 18 | |
| 19 | import android.content.Context |
| 20 | import android.os.Build |
| 21 | import androidx.annotation.ColorRes |
| 22 | import androidx.annotation.DoNotInline |
| 23 | import androidx.annotation.RequiresApi |
| 24 | import androidx.compose.ui.graphics.Color |
| 25 | |
Shalom Gibly | 0b63071 | 2021-08-26 10:35:08 -0700 | [diff] [blame] | 26 | /** Dynamic colors in Material. */ |
Connie Shi | babbda4 | 2021-08-11 17:14:22 -0400 | [diff] [blame] | 27 | @RequiresApi(Build.VERSION_CODES.S) |
Jose Alba Aguado | 00358c5 | 2021-09-30 16:59:48 -0400 | [diff] [blame] | 28 | internal fun dynamicTonalPalette(context: Context): TonalPalette = TonalPalette( |
| 29 | // The neutral tonal range from the generated dynamic color palette. |
| 30 | neutral100 = ColorResourceHelper.getColor(context, android.R.color.system_neutral1_0), |
| 31 | neutral99 = ColorResourceHelper.getColor(context, android.R.color.system_neutral1_10), |
| 32 | neutral95 = ColorResourceHelper.getColor(context, android.R.color.system_neutral1_50), |
| 33 | neutral90 = ColorResourceHelper.getColor(context, android.R.color.system_neutral1_100), |
| 34 | neutral80 = ColorResourceHelper.getColor(context, android.R.color.system_neutral1_200), |
| 35 | neutral70 = ColorResourceHelper.getColor(context, android.R.color.system_neutral1_300), |
| 36 | neutral60 = ColorResourceHelper.getColor(context, android.R.color.system_neutral1_400), |
| 37 | neutral50 = ColorResourceHelper.getColor(context, android.R.color.system_neutral1_500), |
| 38 | neutral40 = ColorResourceHelper.getColor(context, android.R.color.system_neutral1_600), |
| 39 | neutral30 = ColorResourceHelper.getColor(context, android.R.color.system_neutral1_700), |
| 40 | neutral20 = ColorResourceHelper.getColor(context, android.R.color.system_neutral1_800), |
| 41 | neutral10 = ColorResourceHelper.getColor(context, android.R.color.system_neutral1_900), |
| 42 | neutral0 = ColorResourceHelper.getColor(context, android.R.color.system_neutral1_1000), |
Connie Shi | babbda4 | 2021-08-11 17:14:22 -0400 | [diff] [blame] | 43 | |
Jose Alba Aguado | 00358c5 | 2021-09-30 16:59:48 -0400 | [diff] [blame] | 44 | // The neutral variant tonal range, sometimes called "neutral 2", from the |
| 45 | // generated dynamic color palette. |
| 46 | neutralVariant100 = ColorResourceHelper.getColor(context, android.R.color.system_neutral2_0), |
| 47 | neutralVariant99 = ColorResourceHelper.getColor(context, android.R.color.system_neutral2_10), |
| 48 | neutralVariant95 = ColorResourceHelper.getColor(context, android.R.color.system_neutral2_50), |
| 49 | neutralVariant90 = ColorResourceHelper.getColor(context, android.R.color.system_neutral2_100), |
| 50 | neutralVariant80 = ColorResourceHelper.getColor(context, android.R.color.system_neutral2_200), |
| 51 | neutralVariant70 = ColorResourceHelper.getColor(context, android.R.color.system_neutral2_300), |
| 52 | neutralVariant60 = ColorResourceHelper.getColor(context, android.R.color.system_neutral2_400), |
| 53 | neutralVariant50 = ColorResourceHelper.getColor(context, android.R.color.system_neutral2_500), |
| 54 | neutralVariant40 = ColorResourceHelper.getColor(context, android.R.color.system_neutral2_600), |
| 55 | neutralVariant30 = ColorResourceHelper.getColor(context, android.R.color.system_neutral2_700), |
| 56 | neutralVariant20 = ColorResourceHelper.getColor(context, android.R.color.system_neutral2_800), |
| 57 | neutralVariant10 = ColorResourceHelper.getColor(context, android.R.color.system_neutral2_900), |
| 58 | neutralVariant0 = ColorResourceHelper.getColor(context, android.R.color.system_neutral2_1000), |
Connie Shi | babbda4 | 2021-08-11 17:14:22 -0400 | [diff] [blame] | 59 | |
Jose Alba Aguado | 00358c5 | 2021-09-30 16:59:48 -0400 | [diff] [blame] | 60 | // The primary tonal range from the generated dynamic color palette. |
| 61 | primary100 = ColorResourceHelper.getColor(context, android.R.color.system_accent1_0), |
| 62 | primary99 = ColorResourceHelper.getColor(context, android.R.color.system_accent1_10), |
| 63 | primary95 = ColorResourceHelper.getColor(context, android.R.color.system_accent1_50), |
| 64 | primary90 = ColorResourceHelper.getColor(context, android.R.color.system_accent1_100), |
| 65 | primary80 = ColorResourceHelper.getColor(context, android.R.color.system_accent1_200), |
| 66 | primary70 = ColorResourceHelper.getColor(context, android.R.color.system_accent1_300), |
| 67 | primary60 = ColorResourceHelper.getColor(context, android.R.color.system_accent1_400), |
| 68 | primary50 = ColorResourceHelper.getColor(context, android.R.color.system_accent1_500), |
| 69 | primary40 = ColorResourceHelper.getColor(context, android.R.color.system_accent1_600), |
| 70 | primary30 = ColorResourceHelper.getColor(context, android.R.color.system_accent1_700), |
| 71 | primary20 = ColorResourceHelper.getColor(context, android.R.color.system_accent1_800), |
| 72 | primary10 = ColorResourceHelper.getColor(context, android.R.color.system_accent1_900), |
| 73 | primary0 = ColorResourceHelper.getColor(context, android.R.color.system_accent1_1000), |
Connie Shi | babbda4 | 2021-08-11 17:14:22 -0400 | [diff] [blame] | 74 | |
Jose Alba Aguado | 00358c5 | 2021-09-30 16:59:48 -0400 | [diff] [blame] | 75 | // The secondary tonal range from the generated dynamic color palette. |
| 76 | secondary100 = ColorResourceHelper.getColor(context, android.R.color.system_accent2_0), |
| 77 | secondary99 = ColorResourceHelper.getColor(context, android.R.color.system_accent2_10), |
| 78 | secondary95 = ColorResourceHelper.getColor(context, android.R.color.system_accent2_50), |
| 79 | secondary90 = ColorResourceHelper.getColor(context, android.R.color.system_accent2_100), |
| 80 | secondary80 = ColorResourceHelper.getColor(context, android.R.color.system_accent2_200), |
| 81 | secondary70 = ColorResourceHelper.getColor(context, android.R.color.system_accent2_300), |
| 82 | secondary60 = ColorResourceHelper.getColor(context, android.R.color.system_accent2_400), |
| 83 | secondary50 = ColorResourceHelper.getColor(context, android.R.color.system_accent2_500), |
| 84 | secondary40 = ColorResourceHelper.getColor(context, android.R.color.system_accent2_600), |
| 85 | secondary30 = ColorResourceHelper.getColor(context, android.R.color.system_accent2_700), |
| 86 | secondary20 = ColorResourceHelper.getColor(context, android.R.color.system_accent2_800), |
| 87 | secondary10 = ColorResourceHelper.getColor(context, android.R.color.system_accent2_900), |
| 88 | secondary0 = ColorResourceHelper.getColor(context, android.R.color.system_accent2_1000), |
Connie Shi | babbda4 | 2021-08-11 17:14:22 -0400 | [diff] [blame] | 89 | |
Jose Alba Aguado | 00358c5 | 2021-09-30 16:59:48 -0400 | [diff] [blame] | 90 | // The tertiary tonal range from the generated dynamic color palette. |
| 91 | tertiary100 = ColorResourceHelper.getColor(context, android.R.color.system_accent3_0), |
| 92 | tertiary99 = ColorResourceHelper.getColor(context, android.R.color.system_accent3_10), |
| 93 | tertiary95 = ColorResourceHelper.getColor(context, android.R.color.system_accent3_50), |
| 94 | tertiary90 = ColorResourceHelper.getColor(context, android.R.color.system_accent3_100), |
| 95 | tertiary80 = ColorResourceHelper.getColor(context, android.R.color.system_accent3_200), |
| 96 | tertiary70 = ColorResourceHelper.getColor(context, android.R.color.system_accent3_300), |
| 97 | tertiary60 = ColorResourceHelper.getColor(context, android.R.color.system_accent3_400), |
| 98 | tertiary50 = ColorResourceHelper.getColor(context, android.R.color.system_accent3_500), |
| 99 | tertiary40 = ColorResourceHelper.getColor(context, android.R.color.system_accent3_600), |
| 100 | tertiary30 = ColorResourceHelper.getColor(context, android.R.color.system_accent3_700), |
| 101 | tertiary20 = ColorResourceHelper.getColor(context, android.R.color.system_accent3_800), |
| 102 | tertiary10 = ColorResourceHelper.getColor(context, android.R.color.system_accent3_900), |
| 103 | tertiary0 = ColorResourceHelper.getColor(context, android.R.color.system_accent3_1000), |
| 104 | ) |
| 105 | |
| 106 | /** |
| 107 | * Creates a light dynamic color scheme. |
| 108 | * |
| 109 | * Use this function to create a color scheme based off the system wallpaper. If the developer |
| 110 | * changes the wallpaper this color scheme will change accordingly. This dynamic scheme is a |
| 111 | * light theme variant. |
| 112 | * |
| 113 | * @param context The context required to get system resource data. |
| 114 | */ |
| 115 | @RequiresApi(Build.VERSION_CODES.S) |
| 116 | fun dynamicLightColorScheme(context: Context): ColorScheme { |
| 117 | val tonalPalette = dynamicTonalPalette(context) |
| 118 | return lightColorScheme( |
| 119 | primary = tonalPalette.primary40, |
| 120 | onPrimary = tonalPalette.primary100, |
| 121 | primaryContainer = tonalPalette.primary90, |
| 122 | onPrimaryContainer = tonalPalette.primary10, |
| 123 | inversePrimary = tonalPalette.primary80, |
| 124 | secondary = tonalPalette.secondary40, |
| 125 | onSecondary = tonalPalette.secondary100, |
| 126 | secondaryContainer = tonalPalette.secondary90, |
| 127 | onSecondaryContainer = tonalPalette.secondary10, |
| 128 | tertiaryContainer = tonalPalette.tertiary90, |
| 129 | onTertiaryContainer = tonalPalette.tertiary10, |
| 130 | background = tonalPalette.neutral99, |
| 131 | onBackground = tonalPalette.neutral10, |
| 132 | surface = tonalPalette.neutral99, |
| 133 | onSurface = tonalPalette.neutral10, |
| 134 | surfaceVariant = tonalPalette.neutralVariant90, |
| 135 | onSurfaceVariant = tonalPalette.neutralVariant30, |
| 136 | inverseSurface = tonalPalette.neutral20, |
| 137 | inverseOnSurface = tonalPalette.neutral95, |
| 138 | outline = tonalPalette.neutralVariant50, |
Connie Shi | babbda4 | 2021-08-11 17:14:22 -0400 | [diff] [blame] | 139 | ) |
Jose Alba Aguado | 00358c5 | 2021-09-30 16:59:48 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Creates a dark dynamic color scheme. |
| 144 | * |
| 145 | * Use this function to create a color scheme based off the system wallpaper. If the developer |
| 146 | * changes the wallpaper this color scheme will change accordingly. This dynamic scheme is a dark |
| 147 | * theme variant. |
| 148 | * |
| 149 | * @param context The context required to get system resource data. |
| 150 | */ |
| 151 | @RequiresApi(Build.VERSION_CODES.S) |
| 152 | fun dynamicDarkColorScheme(context: Context): ColorScheme { |
| 153 | val tonalPalette = dynamicTonalPalette(context) |
| 154 | return darkColorScheme( |
| 155 | primary = tonalPalette.primary80, |
| 156 | onPrimary = tonalPalette.primary20, |
| 157 | primaryContainer = tonalPalette.primary30, |
| 158 | onPrimaryContainer = tonalPalette.primary90, |
| 159 | inversePrimary = tonalPalette.primary40, |
| 160 | secondary = tonalPalette.secondary80, |
| 161 | onSecondary = tonalPalette.secondary20, |
| 162 | secondaryContainer = tonalPalette.secondary30, |
| 163 | onSecondaryContainer = tonalPalette.secondary90, |
| 164 | tertiaryContainer = tonalPalette.tertiary30, |
| 165 | onTertiaryContainer = tonalPalette.tertiary90, |
| 166 | background = tonalPalette.neutral10, |
| 167 | onBackground = tonalPalette.neutral90, |
| 168 | surface = tonalPalette.neutral10, |
| 169 | onSurface = tonalPalette.neutral90, |
| 170 | surfaceVariant = tonalPalette.neutralVariant30, |
| 171 | onSurfaceVariant = tonalPalette.neutralVariant80, |
| 172 | inverseSurface = tonalPalette.neutral90, |
| 173 | inverseOnSurface = tonalPalette.neutral20, |
| 174 | outline = tonalPalette.neutralVariant60, |
| 175 | ) |
| 176 | } |
Connie Shi | babbda4 | 2021-08-11 17:14:22 -0400 | [diff] [blame] | 177 | |
| 178 | @RequiresApi(23) |
| 179 | private object ColorResourceHelper { |
| 180 | @DoNotInline |
| 181 | fun getColor(context: Context, @ColorRes id: Int): Color { |
| 182 | return Color(context.resources.getColor(id, context.theme)) |
| 183 | } |
| 184 | } |