-
Notifications
You must be signed in to change notification settings - Fork 0
/
TabColorCalculator.kt
143 lines (137 loc) · 4.72 KB
/
TabColorCalculator.kt
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.example.customizeddesigntablayout.model
import android.graphics.Color
import androidx.annotation.ColorRes
import com.example.customizeddesigntablayout.R
class TabColorCalculator(private val backgroundColor: Int) {
companion object {
fun parseColor(colorString: String?): Int {
runCatching {
Color.parseColor(colorString)
}.fold(
onSuccess = { return it },
onFailure = { throw it }
)
}
}
// 輝度とbackgroundColorを元にタブで使う色を求める(選択中テキスト色、非選択テキスト色、アイコン色、背景色 )
fun getTabColors(): TabColors {
return when {
getLightness() in 0.0..0.29 -> {
TabColors(
alpha = 33,
overlay = R.color.white,
icon = R.color.white,
selectedText = R.color.white,
unSelectedText = R.color.Grey_2,
systemUiFlagLightStatusBar = false
)
}
getLightness() < 0.6 -> {
if (blackTextIsBetter()) {
TabColors(
alpha = 89,
overlay = R.color.black,
icon = R.color.Black_2,
selectedText = R.color.white,
unSelectedText = R.color.Grey_2,
systemUiFlagLightStatusBar = true
)
} else {
TabColors(
alpha = 89,
overlay = R.color.black,
icon = R.color.white,
selectedText = R.color.white,
unSelectedText = R.color.Grey_2,
systemUiFlagLightStatusBar = false
)
}
}
getLightness() < 0.8 -> {
TabColors(
alpha = 89,
overlay = R.color.black,
icon = R.color.Black_2,
selectedText = R.color.white,
unSelectedText = R.color.Grey_2,
systemUiFlagLightStatusBar = true
)
}
getLightness() < 0.9 -> {
TabColors(
alpha = 89,
overlay = R.color.white,
icon = R.color.Black_2,
selectedText = R.color.Black_2,
unSelectedText = R.color.Black_3,
systemUiFlagLightStatusBar = true
)
}
else -> {
TabColors(
alpha = 12,
overlay = R.color.black,
icon = R.color.Black_2,
selectedText = R.color.Black_2,
unSelectedText = R.color.Black_3,
systemUiFlagLightStatusBar = true
)
}
}
}
/**
* colorを元に輝度を求める
*
* @return 輝度 Double
*/
private fun getLightness(): Double {
val colors = mutableListOf(
Color.red(backgroundColor),
Color.green(backgroundColor),
Color.blue(backgroundColor)
)
val max = colors.maxOrNull() ?: 0
val min = colors.minOrNull() ?: 0
return (max + min).toDouble() / (2 * 255).toDouble()
}
/**
* 背景色によって、白文字列 or 黒文字列が良いかを判断する.<br></br>
* 以下のW3Cのロジックを利用。
* https://www.w3.org/WAI/ER/WD-AERT/#color-contrast
*
* @return 黒を指定した方が良い場合にtrue
*/
private fun blackTextIsBetter(): Boolean {
val red = Color.red(backgroundColor)
val green = Color.green(backgroundColor)
val blue = Color.blue(backgroundColor)
val delta = (red * 299 + green * 587 + blue * 114) / 1000
return delta >= 125
}
data class TabColors(
/**
* 重ねる画像のアルファ値(0〜255)
*/
val alpha: Int,
/**
* 重ねる色
*/
@ColorRes val overlay: Int,
/**
* アイコン色
*/
@ColorRes val icon: Int,
/**
* タブ選択時テキスト色
*/
@ColorRes val selectedText: Int,
/**
* タブ非選択時テキスト色
*/
@ColorRes val unSelectedText: Int,
/**
* ステータスバーのアイコンや文字の色をLight用にするかどうかの判定(iconの色が黒ならtrue)
*/
val systemUiFlagLightStatusBar: Boolean
)
}