Skip to content

Commit 02c4a8d

Browse files
committed
library: fix wrong hsv param order
1 parent 111d23d commit 02c4a8d

File tree

4 files changed

+21
-28
lines changed

4 files changed

+21
-28
lines changed

iosApp/iosApp/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<key>CFBundleShortVersionString</key>
1818
<string>1.0.4</string>
1919
<key>CFBundleVersion</key>
20-
<string>546</string>
20+
<string>548</string>
2121
<key>LSRequiresIPhoneOS</key>
2222
<true/>
2323
<key>CADisableMinimumFrameDurationOnPhone</key>

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic/ColorPalette.kt

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ import kotlin.math.abs
4343
import kotlin.math.min
4444
import kotlin.math.roundToInt
4545

46-
/** * A color palette component that allows users to select colors from a grid of HSV values.
46+
/**
47+
* A color palette component that allows users to select colors from a grid of HSV values.
4748
*
4849
* @param initialColor The initial color to display in the palette.
4950
* @param onColorChanged Callback invoked when the selected color changes.
@@ -77,10 +78,7 @@ fun ColorPalette(
7778
var alpha by remember { mutableStateOf(initialColor.alpha.coerceIn(0f, 1f)) }
7879
var lastEmittedColor by remember { mutableStateOf<Color?>(null) }
7980
var lastAcceptedHSV by remember { mutableStateOf<Triple<Float, Float, Float>?>(null) }
80-
8181
LaunchedEffect(initialColor, rows, hueColumns, includeGrayColumn) {
82-
if (lastEmittedColor?.let { colorsEqualApprox(it, initialColor) } == true) return@LaunchedEffect
83-
8482
val hsvInit = initialColor.toHsv()
8583
val h = hsvInit.h.toFloat()
8684
val s = (hsvInit.s / 100.0).toFloat()
@@ -179,6 +177,7 @@ private fun PaletteCanvas(
179177
selectedCol: Int,
180178
onSelect: (row: Int, col: Int) -> Unit,
181179
) {
180+
val onSelectState = rememberUpdatedState(onSelect)
182181
val totalColumns = hueColumns + if (includeGrayColumn) 1 else 0
183182
val rowSV = remember(rows) { buildRowSV(rows) }
184183
val grayV = remember(rows) { buildGrayV(rows) }
@@ -194,14 +193,14 @@ private fun PaletteCanvas(
194193
detectTapGestures { pos ->
195194
if (sizePx.width == 0 || sizePx.height == 0) return@detectTapGestures
196195
val (r, c) = pointToCell(pos, sizePx, rows, totalColumns)
197-
onSelect(r, c)
196+
onSelectState.value(r, c)
198197
}
199198
}
200199
.pointerInput(rows, hueColumns, includeGrayColumn) {
201200
detectDragGestures { change, _ ->
202201
if (sizePx.width == 0 || sizePx.height == 0) return@detectDragGestures
203202
val (r, c) = pointToCell(change.position, sizePx, rows, totalColumns)
204-
onSelect(r, c)
203+
onSelectState.value(r, c)
205204
}
206205
}
207206
.fillMaxWidth()
@@ -299,11 +298,11 @@ private fun cellColor(
299298
val totalColumns = hueColumns + if (includeGrayColumn) 1 else 0
300299
val (s, v) = rowSV[row]
301300
return if (includeGrayColumn && col == totalColumns - 1) {
302-
Hsv(0.0, (grayV[row] * 100.0), 0.0).toColor()
301+
Hsv(0.0, 0.0, (grayV[row] * 100.0)).toColor()
303302
} else {
304303
val step = 360f / hueColumns
305304
val h = (col * step) % 360f
306-
Hsv(h.toDouble(), (v * 100.0), (s * 100.0)).toColor()
305+
Hsv(h.toDouble(), (s * 100.0), (v * 100.0)).toColor()
307306
}
308307
}
309308

@@ -315,13 +314,6 @@ private fun pointToCell(pos: Offset, size: IntSize, rows: Int, totalColumns: Int
315314
return row to col
316315
}
317316

318-
private fun colorsEqualApprox(a: Color, b: Color, eps: Float = 0.002f): Boolean {
319-
return abs(a.red - b.red) < eps &&
320-
abs(a.green - b.green) < eps &&
321-
abs(a.blue - b.blue) < eps &&
322-
abs(a.alpha - b.alpha) < eps
323-
}
324-
325317
private fun hsvEqualApprox(
326318
a: Triple<Float, Float, Float>,
327319
b: Triple<Float, Float, Float>,
@@ -346,4 +338,3 @@ private fun <T> List<T>.indexOfMinBy(selector: (T) -> Float): Int {
346338
}
347339

348340
private fun sq(v: Float) = v * v
349-

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic/ColorPicker.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import androidx.compose.runtime.LaunchedEffect
1818
import androidx.compose.runtime.getValue
1919
import androidx.compose.runtime.mutableStateOf
2020
import androidx.compose.runtime.remember
21+
import androidx.compose.runtime.rememberUpdatedState
2122
import androidx.compose.runtime.setValue
2223
import androidx.compose.ui.Alignment
2324
import androidx.compose.ui.Modifier
@@ -122,8 +123,8 @@ fun HsvColorPicker(
122123

123124
val selectedColor = Hsv(
124125
h = currentHue.toDouble(),
125-
v = (currentValue * 100.0),
126-
s = (currentSaturation * 100.0)
126+
s = (currentSaturation * 100.0),
127+
v = (currentValue * 100.0)
127128
).toColor(currentAlpha)
128129

129130
LaunchedEffect(initialColor) {
@@ -237,7 +238,7 @@ fun HsvSaturationSlider(
237238
) {
238239
val saturationColors = remember(currentHue) {
239240
listOf(
240-
Hsv(currentHue.toDouble(), 100.0, 0.0).toColor(1f),
241+
Hsv(currentHue.toDouble(), 0.0, 100.0).toColor(1f),
241242
Hsv(currentHue.toDouble(), 100.0, 100.0).toColor(1f)
242243
)
243244
}
@@ -268,7 +269,7 @@ fun HsvValueSlider(
268269
hapticEffect: SliderDefaults.SliderHapticEffect = SliderDefaults.DefaultHapticEffect
269270
) {
270271
val valueColors = remember(currentHue, currentSaturation) {
271-
listOf(Color.Black, Hsv(currentHue.toDouble(), 100.0, (currentSaturation * 100.0)).toColor())
272+
listOf(Color.Black, Hsv(currentHue.toDouble(), (currentSaturation * 100.0), 100.0).toColor())
272273
}
273274
ColorSlider(
274275
value = currentValue,
@@ -299,7 +300,7 @@ fun HsvAlphaSlider(
299300
hapticEffect: SliderDefaults.SliderHapticEffect = SliderDefaults.DefaultHapticEffect
300301
) {
301302
val alphaColors = remember(currentHue, currentSaturation, currentValue) {
302-
val baseColor = Hsv(currentHue.toDouble(), (currentValue * 100.0), (currentSaturation * 100.0)).toColor()
303+
val baseColor = Hsv(currentHue.toDouble(), (currentSaturation * 100.0), (currentValue * 100.0)).toColor()
303304
listOf(baseColor.copy(alpha = 0f), baseColor.copy(alpha = 1f))
304305
}
305306

@@ -817,6 +818,7 @@ private fun ColorSlider(
817818
modifier: Modifier = Modifier,
818819
hapticEffect: SliderDefaults.SliderHapticEffect = SliderDefaults.DefaultHapticEffect,
819820
) {
821+
val onValueChangedState = rememberUpdatedState(onValueChanged)
820822
val density = LocalDensity.current
821823
var sliderWidth by remember { mutableStateOf(0.dp) }
822824
val indicatorSizeDp = 20.dp
@@ -856,7 +858,7 @@ private fun ColorSlider(
856858
onDragStart = { offset ->
857859
val newValue =
858860
handleSliderInteraction(offset.x, size.width.toFloat(), with(density) { sliderHeightDp.toPx() })
859-
onValueChanged(newValue)
861+
onValueChangedState.value(newValue)
860862
hapticState.reset(newValue)
861863
},
862864
onHorizontalDrag = { change, _ ->
@@ -865,7 +867,7 @@ private fun ColorSlider(
865867
change.position.x,
866868
size.width.toFloat(),
867869
with(density) { sliderHeightDp.toPx() }).coerceIn(0f, 1f)
868-
onValueChanged(newValue)
870+
onValueChangedState.value(newValue)
869871
hapticState.handleHapticFeedback(newValue, hapticEffect, hapticFeedback)
870872
}
871873
)

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/utils/ColorUtils.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ data class OkLab(val l: Double, val a: Double, val b: Double) {
3030
/**
3131
* User-friendly HSV-like representation.
3232
* - h: hue in degrees [0, 360)
33-
* - v: value/brightness in percent [0.0, 100.0]
3433
* - s: saturation in percent [0.0, 100.0]
34+
* - v: value/brightness in percent [0.0, 100.0]
3535
*/
36-
data class Hsv(val h: Double, val v: Double, val s: Double) {
36+
data class Hsv(val h: Double, val s: Double, val v: Double) {
3737
fun toColor(alpha: Float = 1f): Color {
3838
val hue = (((h % 360.0) + 360.0) % 360.0).toFloat()
39-
val vN = (v / 100.0).coerceIn(0.0, 1.0).toFloat()
4039
val sN = (s / 100.0).coerceIn(0.0, 1.0).toFloat()
40+
val vN = (v / 100.0).coerceIn(0.0, 1.0).toFloat()
4141
return hsv(hue, sN, vN, alpha)
4242
}
4343
}
@@ -57,7 +57,7 @@ fun Color.toHsv(): Hsv {
5757
val h = hsvArr[0].toDouble()
5858
val s = (hsvArr[1] * 100.0).coerceIn(0.0, 100.0)
5959
val v = (hsvArr[2] * 100.0).coerceIn(0.0, 100.0)
60-
return Hsv(h, v, s)
60+
return Hsv(h, s, v)
6161
}
6262

6363
object ColorUtils {

0 commit comments

Comments
 (0)