1 /* 2 * Copyright (C) 2019 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.keyguard.clock 18 19 import android.graphics.Color 20 import android.util.MathUtils 21 22 private const val PRIMARY_INDEX = 5 23 private const val SECONDARY_DARK_INDEX = 8 24 private const val SECONDARY_LIGHT_INDEX = 2 25 26 /** 27 * A helper class to extract colors from a clock face. 28 */ 29 class ClockPalette { 30 31 private var darkAmount: Float = 0f 32 private var accentPrimary: Int = Color.WHITE 33 private var accentSecondaryLight: Int = Color.WHITE 34 private var accentSecondaryDark: Int = Color.BLACK 35 private val lightHSV: FloatArray = FloatArray(3) 36 private val darkHSV: FloatArray = FloatArray(3) 37 private val hsv: FloatArray = FloatArray(3) 38 39 /** Returns a color from the palette as an RGB packed int. */ 40 fun getPrimaryColor(): Int { 41 return accentPrimary 42 } 43 44 /** Returns either a light or dark color from the palette as an RGB packed int. */ 45 fun getSecondaryColor(): Int { 46 Color.colorToHSV(accentSecondaryLight, lightHSV) 47 Color.colorToHSV(accentSecondaryDark, darkHSV) 48 for (i in 0..2) { 49 hsv[i] = MathUtils.lerp(darkHSV[i], lightHSV[i], darkAmount) 50 } 51 return Color.HSVToColor(hsv) 52 } 53 54 /** See {@link ClockPlugin#setColorPalette}. */ 55 fun setColorPalette(supportsDarkText: Boolean, colorPalette: IntArray?) { 56 if (colorPalette == null || colorPalette.isEmpty()) { 57 accentPrimary = Color.WHITE 58 accentSecondaryLight = Color.WHITE 59 accentSecondaryDark = if (supportsDarkText) Color.BLACK else Color.WHITE 60 return 61 } 62 val length = colorPalette.size 63 accentPrimary = colorPalette[Math.max(0, length - PRIMARY_INDEX)] 64 accentSecondaryLight = colorPalette[Math.max(0, length - SECONDARY_LIGHT_INDEX)] 65 accentSecondaryDark = colorPalette[Math.max(0, 66 length - if (supportsDarkText) SECONDARY_DARK_INDEX else SECONDARY_LIGHT_INDEX)] 67 } 68 69 /** See {@link ClockPlugin#setDarkAmount}. */ 70 fun setDarkAmount(darkAmount: Float) { 71 this.darkAmount = darkAmount 72 } 73 } 74