Discretize ISO and shutter speed to common values

This commit is contained in:
2024-08-05 21:50:19 +02:00
parent b8b30ca5b0
commit 1a40373093
@@ -27,17 +27,20 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import eu.wa5p.lightmeter.ui.theme.LightMeterTheme import eu.wa5p.lightmeter.ui.theme.LightMeterTheme
import kotlin.math.roundToInt import kotlin.math.abs
const val TAG: String = "lightmeter MainActivity" const val TAG: String = "lightmeter MainActivity"
private val commonIsoValues = listOf(50, 100, 200, 400, 800, 1600, 3200, 6400)
private val commonApertureValues = listOf(0.85, 1.4, 2.0, 2.8, 4.0, 5.6, 8.0, 11.0, 16.0, 22.0)
class MainActivity : ComponentActivity(), SensorEventListener { class MainActivity : ComponentActivity(), SensorEventListener {
private lateinit var sensorManager: SensorManager private lateinit var sensorManager: SensorManager
private var illuminanceSensor: Sensor? = null private var illuminanceSensor: Sensor? = null
private var lightmeterState = mutableStateOf( private var lightmeterState = mutableStateOf(
LightmeterState( LightmeterState(
illuminance = 0.0, illuminance = 0.0,
aperture = 1.8, aperture = 2.8,
iso = 100, iso = 100,
shutterSpeed = 0.01 shutterSpeed = 0.01
) )
@@ -150,6 +153,14 @@ fun LightmeterListItemPreview() {
@Composable @Composable
fun IsoSlider(state: MutableState<LightmeterState>, recalculateShutterSpeed: () -> Unit) { fun IsoSlider(state: MutableState<LightmeterState>, recalculateShutterSpeed: () -> Unit) {
// Convert common ISO values to the range used by the slider
val minIso = commonIsoValues.first().toFloat()
val maxIso = commonIsoValues.last().toFloat()
// Find the closest ISO value to the slider's value
val isoValue = state.value.iso.toFloat()
val closestIso = commonIsoValues.minByOrNull { abs(it - isoValue) } ?: minIso
Row( Row(
modifier = Modifier.padding(all = 8.dp), modifier = Modifier.padding(all = 8.dp),
verticalAlignment = Alignment.CenterVertically verticalAlignment = Alignment.CenterVertically
@@ -160,16 +171,17 @@ fun IsoSlider(state: MutableState<LightmeterState>, recalculateShutterSpeed: ()
) )
Slider( Slider(
modifier = Modifier.weight(0.7f), modifier = Modifier.weight(0.7f),
value = state.value.iso.toFloat(), value = closestIso.toFloat(),
onValueChange = { value -> onValueChange = { value ->
state.value = state.value.copy(iso = value.roundToInt()) val newIso = commonIsoValues.minByOrNull { abs(it - value) } ?: minIso
state.value = state.value.copy(iso = newIso.toInt())
recalculateShutterSpeed() recalculateShutterSpeed()
}, },
valueRange = 1f..2000f valueRange = minIso..maxIso,
steps = (commonIsoValues.size - 1) * 10
) )
} }
} }
val previewlightmeterState = mutableStateOf( val previewlightmeterState = mutableStateOf(
LightmeterState( LightmeterState(
illuminance = 0.0, illuminance = 0.0,
@@ -197,6 +209,14 @@ fun ApertureSlider(
state: MutableState<LightmeterState>, state: MutableState<LightmeterState>,
recalculateShutterSpeed: () -> Unit recalculateShutterSpeed: () -> Unit
) { ) {
// Convert common aperture values to the range used by the slider
val minAperture = commonApertureValues.first().toFloat()
val maxAperture = commonApertureValues.last().toFloat()
// Find the closest aperture value to the slider's value
val apertureValue = state.value.aperture.toFloat()
val closestAperture = commonApertureValues.minByOrNull { abs(it - apertureValue) } ?: minAperture
Row( Row(
modifier = Modifier.padding(all = 8.dp), modifier = Modifier.padding(all = 8.dp),
verticalAlignment = Alignment.CenterVertically verticalAlignment = Alignment.CenterVertically
@@ -207,16 +227,17 @@ fun ApertureSlider(
) )
Slider( Slider(
modifier = Modifier.weight(0.7f), modifier = Modifier.weight(0.7f),
value = state.value.aperture.toFloat(), value = closestAperture.toFloat(),
onValueChange = { value -> onValueChange = { value ->
state.value = state.value.copy(aperture = value.toDouble()) val newAperture = commonApertureValues.minByOrNull { abs(it - value) } ?: minAperture
state.value = state.value.copy(aperture = newAperture.toDouble())
recalculateShutterSpeed() recalculateShutterSpeed()
}, },
valueRange = 0.85f..22f valueRange = minAperture..maxAperture,
steps = (commonApertureValues.size - 1) * 10
) )
} }
} }
@Preview(showBackground = true) @Preview(showBackground = true)
@Composable @Composable
fun ApertureSliderPreview() { fun ApertureSliderPreview() {