1 /* 2 * Copyright 2024 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.photopicker.core.theme 18 19 import androidx.compose.runtime.staticCompositionLocalOf 20 import androidx.compose.ui.graphics.Color 21 import androidx.compose.ui.graphics.isUnspecified 22 23 /** CompositionLocal used to pass [AccentColorScheme] down the tree. */ 24 val CustomAccentColorScheme = <lambda>null25 staticCompositionLocalOf<AccentColorScheme> { 26 throw IllegalStateException( 27 "No CustomAccentColorScheme configured." 28 ) 29 } 30 31 /** 32 * The custom color scheme to represent the accent color input in the [ACTION_PICK_IMAGES] intent 33 * used for key UI elements in photo picker and also the related text colors used in the modified 34 * elements. 35 */ 36 class AccentColorScheme(accentColorHelper: AccentColorHelper) { 37 private val accentColor = accentColorHelper.getAccentColor() 38 private val textColorForAccentComponents = accentColorHelper.getTextColorForAccentComponents() 39 40 /** 41 * Returns the accent color which has been passed as an input in the picker intent. 42 * 43 * If the accent color is not present or is invalid the this value will be [Color.Unspecified] 44 * by default. 45 */ getAccentColorIfDefinedOrElsenull46 fun getAccentColorIfDefinedOrElse(fallbackColor: Color): Color { 47 return when (accentColor.isUnspecified) { 48 true -> fallbackColor 49 false -> accentColor 50 } 51 } 52 53 /** 54 * Returns the appropriate text color for components using the accent color as the background 55 * which has been passed as an input in the picker intent. 56 * 57 * This is helpful in maintaining the readability of the component. 58 * 59 * If the accent color is not present or is invalid the this value will be [Color.Unspecified] 60 * be default. 61 */ getTextColorForAccentComponentsIfDefinedOrElsenull62 fun getTextColorForAccentComponentsIfDefinedOrElse(fallbackColor: Color): Color { 63 return when (textColorForAccentComponents.isUnspecified) { 64 true -> fallbackColor 65 false -> textColorForAccentComponents 66 } 67 } 68 } 69