Marcin Wisniowski
09/11/2023, 5:36 PMMarcin Wisniowski
09/11/2023, 5:37 PMephemient
09/11/2023, 5:41 PMwhen (value) {
in Int.MIN_VALUE..1 -> "low"
in 2..9 -> "middle"
in 10..Int.MAX_VALUE -> "high"
or define your own
value class LessThanOrEqualTo(val endInclusive: Int) {
operator fun contains(value: Int): Boolean = value <= endInclusive
}
when (value) {
in LessThanOrEqualTo(1) -> "low"
but either way you'll run into the issue of https://youtrack.jetbrains.com/issue/KT-8781Marcin Wisniowski
09/11/2023, 5:46 PMbram
09/11/2023, 5:52 PMMap<Int, Color>
and rounding values?ephemient
09/11/2023, 5:57 PMList<Color>
, you just have to take a little bit of care in handling roundingephemient
09/11/2023, 6:00 PMMarcin Wisniowski
09/11/2023, 6:01 PMMarcin Wisniowski
09/11/2023, 6:02 PMAyfri
09/11/2023, 6:05 PM<
and >
and other operators inside when
statements ?ephemient
09/11/2023, 6:07 PMKlitos Kyriacou
09/12/2023, 2:22 PMSortedMap
. Example:
val numberToSize = sortedMapOf(
Comparator.reverseOrder(),
0.0 to "XS", // 0.0 to 0.2499999...
0.25 to "S", // 0.25 to 0.499999...
0.5 to "M",
0.7 to "L"
)
val size = numberToSize.tailMap(number).values.first()
ephemient
09/12/2023, 6:32 PMephemient
09/12/2023, 6:42 PMlistOf(
Color(0xFF2E74DF),
Color(0xFF379CF6),
Color(0xFF4ACFF3),
Color(0xFF5CDCA6),
Color(0xFF70E552),
Color(0xFFEEFF83),
Color(0xFFDC6C08),
Color(0xFFCE4611),
Color(0xFFBC1113),
Color(0xFF6D231A),
).getOrElse(
10 - (value.coerceIn(0.0, 1.0) * 10).toInt()
) { Color(0xFF8F3068) }
such that NaN falls into the expected bucketKlitos Kyriacou
09/13/2023, 8:36 AMvalue.coerceIn(0.0, 1.0) * 10 + 0.5).toInt()
to avoid precision errors (e.g. 0.8 * 10 is 7.999...)ephemient
09/13/2023, 8:44 AMephemient
09/13/2023, 8:45 AM+ 0.5
shouldn't be used, that significantly changes where the boundaries areephemient
09/13/2023, 8:47 AM0.8 * 10 = 7.999...
from, that's not the caseephemient
09/13/2023, 8:47 AMKlitos Kyriacou
09/13/2023, 8:50 AM