summaryrefslogtreecommitdiff
path: root/src/com/android/customization/picker/color/ui/view/ColorOptionIconView.kt
blob: 051484302333eef0dc53aa7284efe8eb2ad2495b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.customization.picker.color.ui.view

import android.annotation.ColorInt
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

/**
 * Draw a color option icon, which is a quadrant circle that can show at most 4 different colors.
 */
class ColorOptionIconView(
    context: Context,
    attrs: AttributeSet,
) : View(context, attrs) {

    private val paint = Paint().apply { style = Paint.Style.FILL }

    private val oval = RectF()

    private var color0 = DEFAULT_PLACEHOLDER_COLOR
    private var color1 = DEFAULT_PLACEHOLDER_COLOR
    private var color2 = DEFAULT_PLACEHOLDER_COLOR
    private var color3 = DEFAULT_PLACEHOLDER_COLOR

    private var w = 0
    private var h = 0

    /**
     * @param color0 the color in the top left quadrant
     * @param color1 the color in the top right quadrant
     * @param color2 the color in the bottom left quadrant
     * @param color3 the color in the bottom right quadrant
     */
    fun bindColor(
        @ColorInt color0: Int,
        @ColorInt color1: Int,
        @ColorInt color2: Int,
        @ColorInt color3: Int,
    ) {
        this.color0 = color0
        this.color1 = color1
        this.color2 = color2
        this.color3 = color3
        invalidate()
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        this.w = w
        this.h = h
        super.onSizeChanged(w, h, oldw, oldh)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        // The w and h need to be an even number to avoid tiny pixel-level gaps between the pies
        w = w.roundDownToEven()
        h = h.roundDownToEven()

        val width = w.toFloat()
        val height = h.toFloat()

        oval.set(0f, 0f, width, height)
        canvas?.apply {
            paint.color = color3
            drawArc(
                oval,
                0f,
                90f,
                true,
                paint,
            )
            paint.color = color2
            drawArc(
                oval,
                90f,
                90f,
                true,
                paint,
            )
            paint.color = color0
            drawArc(
                oval,
                180f,
                90f,
                true,
                paint,
            )
            paint.color = color1
            drawArc(
                oval,
                270f,
                90f,
                true,
                paint,
            )
        }
    }

    companion object {
        const val DEFAULT_PLACEHOLDER_COLOR = Color.BLACK

        fun Int.roundDownToEven(): Int {
            return if (this % 2 == 0) this else this - 1
        }
    }
}