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("No CustomAccentColorScheme configured.") 27 } 28 29 /** 30 * The custom color scheme to represent the accent color input in the [ACTION_PICK_IMAGES] intent 31 * used for key UI elements in photo picker and also the related text colors used in the modified 32 * elements. 33 */ 34 class AccentColorScheme(accentColorHelper: AccentColorHelper) { 35 private val accentColor = accentColorHelper.getAccentColor() 36 private val textColorForAccentComponents = accentColorHelper.getTextColorForAccentComponents() 37 38 /** 39 * Returns the accent color which has been passed as an input in the picker intent. 40 * 41 * If the accent color is not present or is invalid the this value will be [Color.Unspecified] 42 * by default. 43 */ getAccentColorIfDefinedOrElsenull44 fun getAccentColorIfDefinedOrElse(fallbackColor: Color): Color { 45 return when (accentColor.isUnspecified) { 46 true -> fallbackColor 47 false -> accentColor 48 } 49 } 50 51 /** 52 * Returns if an accentColor is defined for this color scheme. 53 * 54 * @return true if [accentColor] is a defined color. 55 */ isAccentColorDefinednull56 fun isAccentColorDefined() = !accentColor.isUnspecified 57 58 /** 59 * Returns the appropriate text color for components using the accent color as the background 60 * which has been passed as an input in the picker intent. 61 * 62 * This is helpful in maintaining the readability of the component. 63 * 64 * If the accent color is not present or is invalid the this value will be [Color.Unspecified] 65 * be default. 66 */ 67 fun getTextColorForAccentComponentsIfDefinedOrElse(fallbackColor: Color): Color { 68 return when (textColorForAccentComponents.isUnspecified) { 69 true -> fallbackColor 70 false -> textColorForAccentComponents 71 } 72 } 73 } 74