Skip to content

Commit bed2114

Browse files
committed
applying alpha to the gradient.
1 parent 7ad97c2 commit bed2114

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ fun ColorPickerComponent(
5252
val rowPadding = dimensionResource(R.dimen.compose_row_padding)
5353
val listener: OnColorChangedListener? = onColorChanged
5454

55+
var currentColor: Int by remember { mutableStateOf(initialColor.hashCode()) }
5556
var currentAlpha: Float by remember { mutableStateOf(.5F) }
56-
var currentSat: Float by remember { mutableStateOf(0.0F) }
57-
var currentVal: Float by remember { mutableStateOf(0.0F) }
57+
var currentSat: Float by remember { mutableStateOf(.5F) }
58+
var currentVal: Float by remember { mutableStateOf(.5F) }
5859
var currentHue: Float by remember { mutableStateOf(180F) }
59-
var currentColor: Int by remember { mutableStateOf(initialColor.hashCode()) }
6060

6161
var offsetSatVal by remember { mutableStateOf(Offset.Zero) }
6262
var sizeSatVal by remember { mutableStateOf(IntSize.Zero) }
@@ -94,7 +94,7 @@ fun ColorPickerComponent(
9494
text = "Hue:",
9595
textAlign = TextAlign.End,
9696
modifier = Modifier
97-
.defaultMinSize(minWidth = Dp(42F))
97+
.defaultMinSize(minWidth = Dp(44F))
9898
.testTag("text_hue")
9999
)
100100
Text(
@@ -110,7 +110,7 @@ fun ColorPickerComponent(
110110
text = "Sat:",
111111
textAlign = TextAlign.End,
112112
modifier = Modifier
113-
.defaultMinSize(minWidth = Dp(42F))
113+
.defaultMinSize(minWidth = Dp(44F))
114114
.testTag("text_sat")
115115
)
116116
Text(
@@ -126,7 +126,7 @@ fun ColorPickerComponent(
126126
text = "Val:",
127127
textAlign = TextAlign.End,
128128
modifier = Modifier
129-
.defaultMinSize(minWidth = Dp(42F))
129+
.defaultMinSize(minWidth = Dp(44F))
130130
.testTag("text_val")
131131
)
132132
Text(
@@ -223,7 +223,9 @@ fun ColorPickerComponent(
223223
contentDescription = "Sat/Val by Hue",
224224
contentScale = ContentScale.FillBounds,
225225
painter = SatValPainter(Size(900F, 900F)).also {
226-
it.setValue(currentSat, currentVal)
226+
it.setAlpha(currentAlpha)
227+
it.setSat(currentSat)
228+
it.setValue(currentVal)
227229
it.setHue(currentHue)
228230
},
229231
modifier = Modifier

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ class HuePainter(intrinsicSize: Size) : BasePainter(intrinsicSize) {
5959

6060
private fun getHueValues(): List<Color> {
6161
var i = 0
62-
val list: MutableList<Color> = MutableList(361) { Color.Black }
62+
val list: MutableList<Color> = MutableList(360) { Color.Black }
6363
while (i < list.size) {
6464
val value = (list.size - i).toFloat()
65-
list[i] = Color(android.graphics.Color.HSVToColor(floatArrayOf(value, 1f, 1f)))
65+
list[i] = Color(android.graphics.Color.HSVToColor(255, floatArrayOf(value, 1f, 1f)))
6666
i++
6767
}
6868
return list

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

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.compose.ui.geometry.Offset
77
import androidx.compose.ui.geometry.Size
88
import androidx.compose.ui.graphics.Color
99
import androidx.compose.ui.graphics.LinearGradientShader
10+
import androidx.compose.ui.graphics.TileMode
1011
import androidx.compose.ui.graphics.drawscope.DrawScope
1112
import androidx.compose.ui.graphics.painter.Painter
1213

@@ -17,8 +18,7 @@ import androidx.compose.ui.graphics.painter.Painter
1718
*/
1819
class SatValPainter(intrinsicSize: Size) : BasePainter(intrinsicSize) {
1920

20-
private var value: FloatArray = FloatArray(2)
21-
private var hue: Float = 360F
21+
private var value: FloatArray = FloatArray(4)
2222

2323
/**
2424
* Implementation of drawing logic for instances of [Painter].
@@ -29,19 +29,25 @@ class SatValPainter(intrinsicSize: Size) : BasePainter(intrinsicSize) {
2929
setCanvas(drawContext, density)
3030

3131
/* Saturation Shader */
32-
val mSaturationShader = LinearGradientShader(
33-
from = Offset(rect.right, rect.top),
34-
to = Offset(rect.left, rect.top),
35-
tileMode = androidx.compose.ui.graphics.TileMode.Clamp,
36-
colors = listOf(Color(HSVToColor(floatArrayOf(hue, 1f, 1f))), Color.White)
32+
val mSaturationShader = LinearGradientShader (
33+
from = Offset(rect.right, rect.top),
34+
to = Offset(rect.left, rect.top),
35+
tileMode = TileMode.Clamp,
36+
colors = listOf(
37+
Color(color = HSVToColor(getAlpha().times(255).toInt(), floatArrayOf(getHue(), 1.0F, 1.0F))),
38+
Color.White
39+
)
3740
)
3841

39-
/* Contrast Shader */
42+
/* Value Shader */
4043
val mContrastShader = LinearGradientShader(
41-
from = Offset(rect.left, rect.bottom),
42-
to = Offset(rect.left, rect.top),
43-
tileMode = androidx.compose.ui.graphics.TileMode.Clamp,
44-
colors = listOf(Color.Black, Color.White)
44+
from = Offset(rect.left, rect.bottom),
45+
to = Offset(rect.left, rect.top),
46+
tileMode = TileMode.Clamp,
47+
colors = listOf(
48+
Color.Black,
49+
Color.White
50+
)
4551
)
4652

4753
/* Compose Shader */
@@ -66,16 +72,39 @@ class SatValPainter(intrinsicSize: Size) : BasePainter(intrinsicSize) {
6672
tracker.isAntiAlias = true
6773

6874
/* Circular Tracker */
69-
val p: Point = satValToPoint(value[0], value[1])
75+
val p: Point = satValToPoint(getSat(), getValue())
7076
canvas.drawCircle(p.x.toFloat(), p.y.toFloat(), satValTrackerRadius, tracker)
7177
}
7278

73-
fun setValue(saturation: Float, value: Float) {
74-
this.value[0] = saturation
75-
this.value[1] = value
79+
private fun getHue() : Float {
80+
return this.value[0]
81+
}
82+
83+
private fun getSat() : Float {
84+
return this.value[1]
85+
}
86+
87+
private fun getValue() : Float {
88+
return this.value[2]
89+
}
90+
91+
private fun getAlpha() : Float {
92+
return this.value[3]
7693
}
7794

7895
fun setHue(value: Float) {
79-
this.hue = value
96+
this.value[0] = value
97+
}
98+
99+
fun setSat(value: Float) {
100+
this.value[1] = value
101+
}
102+
103+
fun setValue(value: Float) {
104+
this.value[2] = value
105+
}
106+
107+
fun setAlpha(value: Float) {
108+
this.value[3] = value
80109
}
81110
}

0 commit comments

Comments
 (0)