1 /*
<lambda>null2  * Copyright (C) 2023 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.customization.picker.clock.ui.viewmodel
18 
19 import android.annotation.ColorInt
20 import android.content.res.Resources
21 import android.graphics.Color
22 import com.android.themepicker.R
23 
24 /** The view model that defines custom clock colors. */
25 data class ClockColorViewModel(
26     val colorId: String,
27     val colorName: String?,
28     @ColorInt val color: Int,
29     private val colorToneMin: Double,
30     private val colorToneMax: Double,
31 ) {
32 
33     fun getColorTone(progress: Int): Double {
34         return colorToneMin + (progress.toDouble() * (colorToneMax - colorToneMin)) / 100
35     }
36 
37     companion object {
38         private const val DEFAULT_COLOR_TONE_MIN = 0
39         private const val DEFAULT_COLOR_TONE_MAX = 100
40 
41         fun getPresetColorMap(resources: Resources): Map<String, ClockColorViewModel> {
42             val ids = resources.getStringArray(R.array.clock_color_ids)
43             val names = resources.obtainTypedArray(R.array.clock_color_names)
44             val colors = resources.obtainTypedArray(R.array.clock_colors)
45             val colorToneMinList = resources.obtainTypedArray(R.array.clock_color_tone_min)
46             val colorToneMaxList = resources.obtainTypedArray(R.array.clock_color_tone_max)
47             return buildList {
48                     ids.indices.forEach { index ->
49                         add(
50                             ClockColorViewModel(
51                                 ids[index],
52                                 names.getString(index),
53                                 colors.getColor(index, Color.TRANSPARENT),
54                                 colorToneMinList.getInt(index, DEFAULT_COLOR_TONE_MIN).toDouble(),
55                                 colorToneMaxList.getInt(index, DEFAULT_COLOR_TONE_MAX).toDouble(),
56                             )
57                         )
58                     }
59                 }
60                 .associateBy { it.colorId }
61                 .also {
62                     names.recycle()
63                     colors.recycle()
64                     colorToneMinList.recycle()
65                     colorToneMaxList.recycle()
66                 }
67         }
68     }
69 }
70