Skip to content

Commit ff0ff31

Browse files
committed
some code cleanup.
1 parent bed2114 commit ff0ff31

File tree

2 files changed

+27
-35
lines changed

2 files changed

+27
-35
lines changed

library/src/main/java/io/syslogic/colorpicker/compose/BasePainter.kt

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,9 @@ abstract class BasePainter(override val intrinsicSize: Size) : Painter() {
8787
* @return the {@link Point} where AlphaPainter shall draw the tracker rectangle.
8888
*/
8989
fun alphaToPoint(value: Float): Point {
90-
return Point(
91-
(value * rect.width()).toInt(),
92-
rect.top.toInt()
93-
)
90+
val x: Int = (value * rect.width()).toInt()
91+
val y: Int = rect.top.toInt()
92+
return Point(x, y)
9493
}
9594

9695
/**
@@ -99,10 +98,9 @@ abstract class BasePainter(override val intrinsicSize: Size) : Painter() {
9998
* @return the {@link Point} where HuePainter shall draw the tracker rectangle.
10099
*/
101100
fun hueToPoint(value: Float): Point {
102-
return Point(
103-
rect.left.toInt(),
104-
(rect.height() - value * rect.height() / 360f + rect.top).toInt()
105-
)
101+
val x: Int = rect.left.toInt()
102+
val y: Int = (rect.height() - value * rect.height() / 360f + rect.top).toInt()
103+
return Point(x, y)
106104
}
107105

108106
/**
@@ -112,9 +110,8 @@ abstract class BasePainter(override val intrinsicSize: Size) : Painter() {
112110
* @return the Point where SatValPainter shall draw the tracker circle.
113111
*/
114112
fun satValToPoint(saturation: Float, value: Float): Point {
115-
return Point(
116-
(saturation * rect.width() + rect.left).toInt(),
117-
((1f - value) * rect.height() + rect.top).toInt()
118-
)
113+
val x: Int = (saturation * rect.width() + rect.left).toInt()
114+
val y: Int = ((1f - value) * rect.height() + rect.top).toInt()
115+
return Point(x, y)
119116
}
120117
}

library/src/main/java/io/syslogic/colorpicker/compose/SatValPainter.kt

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,34 @@ class SatValPainter(intrinsicSize: Size) : BasePainter(intrinsicSize) {
2828

2929
setCanvas(drawContext, density)
3030

31-
/* Saturation Shader */
31+
/* Saturation Shader with Alpha */
32+
val alpha: Int = getAlpha().times(255).toInt()
33+
val color: Int = HSVToColor(alpha, floatArrayOf(getHue(), 1.0F, 1.0F))
3234
val mSaturationShader = LinearGradientShader (
3335
from = Offset(rect.right, rect.top),
3436
to = Offset(rect.left, rect.top),
3537
tileMode = TileMode.Clamp,
36-
colors = listOf(
37-
Color(color = HSVToColor(getAlpha().times(255).toInt(), floatArrayOf(getHue(), 1.0F, 1.0F))),
38-
Color.White
39-
)
38+
colors = listOf(Color(color), Color.White)
4039
)
4140

4241
/* Value Shader */
4342
val mContrastShader = LinearGradientShader(
4443
from = Offset(rect.left, rect.bottom),
4544
to = Offset(rect.left, rect.top),
4645
tileMode = TileMode.Clamp,
47-
colors = listOf(
48-
Color.Black,
49-
Color.White
50-
)
46+
colors = listOf(Color.Black, Color.White)
5147
)
5248

5349
/* Compose Shader */
54-
val paint = Paint()
55-
paint.shader = ComposeShader(mContrastShader, mSaturationShader, PorterDuff.Mode.MULTIPLY)
56-
canvas.drawRect(rect, paint)
50+
val compose = Paint()
51+
compose.shader = ComposeShader(mContrastShader, mSaturationShader, PorterDuff.Mode.MULTIPLY)
52+
canvas.drawRect(rect, compose)
5753

58-
/* Borderline Paint */
54+
/* Borderline */
5955
val border = Paint()
6056
border.style = Paint.Style.STROKE
6157
border.strokeWidth = borderStrokeWidth
6258
border.color = borderStrokeColor
63-
64-
/* Borderline */
6559
canvas.drawRect(rect, border)
6660

6761
/* Tracker Paint */
@@ -76,35 +70,36 @@ class SatValPainter(intrinsicSize: Size) : BasePainter(intrinsicSize) {
7670
canvas.drawCircle(p.x.toFloat(), p.y.toFloat(), satValTrackerRadius, tracker)
7771
}
7872

79-
private fun getHue() : Float {
73+
private fun getAlpha() : Float {
8074
return this.value[0]
8175
}
8276

83-
private fun getSat() : Float {
77+
private fun getHue() : Float {
8478
return this.value[1]
8579
}
8680

87-
private fun getValue() : Float {
81+
private fun getSat() : Float {
8882
return this.value[2]
8983
}
9084

91-
private fun getAlpha() : Float {
85+
private fun getValue() : Float {
9286
return this.value[3]
9387
}
9488

95-
fun setHue(value: Float) {
89+
fun setAlpha(value: Float) {
9690
this.value[0] = value
9791
}
9892

99-
fun setSat(value: Float) {
93+
fun setHue(value: Float) {
10094
this.value[1] = value
10195
}
10296

103-
fun setValue(value: Float) {
97+
fun setSat(value: Float) {
10498
this.value[2] = value
10599
}
106100

107-
fun setAlpha(value: Float) {
101+
fun setValue(value: Float) {
108102
this.value[3] = value
109103
}
104+
110105
}

0 commit comments

Comments
 (0)