1+ package com.cxz.wanandroid.utils
2+
3+ import android.graphics.Color
4+ import android.support.annotation.ColorInt
5+ import android.support.annotation.FloatRange
6+ import android.support.annotation.IntRange
7+
8+ /* *
9+ * @author chenxz
10+ * @date 2019/11/24
11+ * @desc ColorUtil
12+ */
13+ object ColorUtil {
14+
15+ /* *
16+ * 计算颜色
17+ *
18+ * @param color color值
19+ * @param alpha alpha值
20+ * @return 最终的状态栏颜色
21+ */
22+ fun alphaColor (@ColorInt color : Int , @IntRange(from = 0 , to = 255 ) alpha : Int ): Int {
23+ val red = Color .red(color)
24+ val green = Color .green(color)
25+ val blue = Color .blue(color)
26+ return Color .argb(alpha, red, green, blue)
27+ }
28+
29+ /* *
30+ * 计算颜色
31+ *
32+ * @param color color值
33+ * @param alpha alpha值[0-1]
34+ * @return 最终的状态栏颜色
35+ */
36+ fun alphaColor (@ColorInt color : Int , @FloatRange(from = 0.0 , to = 1.0 ) alpha : Float ): Int {
37+ return alphaColor(color, (alpha * 255 ).toInt())
38+ }
39+
40+ /* *
41+ * 根据fraction值来计算当前的颜色
42+ *
43+ * @param colorFrom 起始颜色
44+ * @param colorTo 结束颜色
45+ * @param fraction 变量
46+ * @return 当前颜色
47+ */
48+ fun changingColor (@ColorInt colorFrom : Int , @ColorInt colorTo : Int , @FloatRange(from = 0.0 , to = 1.0 ) fraction : Float ): Int {
49+ val redStart = Color .red(colorFrom)
50+ val blueStart = Color .blue(colorFrom)
51+ val greenStart = Color .green(colorFrom)
52+ val alphaStart = Color .alpha(colorFrom)
53+
54+ val redEnd = Color .red(colorTo)
55+ val blueEnd = Color .blue(colorTo)
56+ val greenEnd = Color .green(colorTo)
57+ val alphaEnd = Color .alpha(colorTo)
58+
59+ val redDifference = redEnd - redStart
60+ val blueDifference = blueEnd - blueStart
61+ val greenDifference = greenEnd - greenStart
62+ val alphaDifference = alphaEnd - alphaStart
63+
64+ val redCurrent = (redStart + fraction * redDifference).toInt()
65+ val blueCurrent = (blueStart + fraction * blueDifference).toInt()
66+ val greenCurrent = (greenStart + fraction * greenDifference).toInt()
67+ val alphaCurrent = (alphaStart + fraction * alphaDifference).toInt()
68+
69+ return Color .argb(alphaCurrent, redCurrent, greenCurrent, blueCurrent)
70+ }
71+
72+ }
0 commit comments