Anthony Flores
03/01/2022, 7:57 PM(255, 255, 255, 255)toRGBA()
Richard Gomez
03/01/2022, 7:59 PMfun toRGBA(r: Int, g: Int, b: Int, a: Int): RGBA
Vampire
03/01/2022, 8:01 PM(255, 255, 255, 255)
is not a thing.
If you make it a thing like with
listOf(255, 255, 255, 255)
, you should then be able to create an extension function List<Int>.toRGBA()
if you really want toRichard Gomez
03/01/2022, 8:01 PMfun List<Int>.toRGBA(): RGBA {
return // something
}
val codes = listOf(255, 255, 255, 255)
val rgba = codes.toRGBA()
Anthony Flores
03/01/2022, 8:05 PMtoRGBA(255, 255, 255, 255)
Vampire
03/01/2022, 8:12 PMAnthony Flores
03/01/2022, 8:14 PMMichael de Kaste
03/01/2022, 8:34 PM(255, 255, 255, 255)toRGBA()
to replace
toRGBA(255, 255, 255, 255)
?,
with the infix operator, so it has to be some other character
`
fun main() {
(1 ç 2 ç 3).toRgba()
}
infix fun Int.ç(other: Int) = listOf(this, other)
infix fun List<Int>.ç(other: Int) = this + other
fun List<Int>.toRgba() = { TODO() }
Tobias Berger
03/01/2022, 10:11 PMephemient
03/02/2022, 12:41 AM@JvmInline value class RG(val value: Int)
@JvmInline value class RGB(val value: Int)
@JvmInline value class RGBA(val value: Int)
infix fun Int.ç(g: Int) = RG(this.and(0xFF).shl(8) or g.and(0xFF))
infix fun RG.ç(b: Int) = RGB(value.and(0xFFFF).shl(8) or b.and(0xFF))
infix fun RGB.ç(a: Int) = RGBA(value.and(0xFFFFFF).shl(8) or a.and(0xFF))
(1 ç 2 ç 3 ç 4).value == 0x01020304
and make it type-safe, but I see absolutely no reason for something as ludicrous as thisStephan Schroeder
03/02/2022, 7:55 AMRGBA
is a data class (you could even have a default argument for A!
data class RGBA (
val red: Int,
val green: Int,
val blue: Int,
val alpha: Int = 255,
) {
init {
fun requireInRange(x: Int, name: String) {
require(x in 0..255) {"param $name is supposed to be in range 0..255 but was $x"}
}
requireInRange(red, "red")
requireInRange(green, "green")
requireInRange(blue, "blue")
requireInRange(alpha, "alpha")
}
}
)!? So every literal form could simply be
RGBA(255, 255, 255, 255)
or
RGBA(255, 255, 255) // alpha = 255
and whenever you don't don't work with hardcoded values but with a list of Ints that you read in from somewhere, you could have the an extension function
// maybe also/or on Array<Int>
fun List<Int>.toRGBA(): RGBA = when(this.size) {
4 -> {
val (red, green, blue, alpha) = this
RGBA(red, green, blue, alpha)
}
3 -> {
val (red, green, blue) = this
RGBA(red, green, blue)
}
else -> {
throw IllegalArgumentException("3 or 4 elements expected in list, but ${this.size} found.")
}
}
But like everyone noticed, this List<Int>.toRGBA-function should only be visible in the code that reads in the colours values from the Int data stream.Michael de Kaste
03/02/2022, 8:57 AMAnthony Flores
03/02/2022, 10:41 PMStephan Schroeder
03/04/2022, 7:24 AM@JvmInline
value class RGBA private constructor(private val compressedColorInfo: Int) { // there might be some scenarios where it's useful to not make `compressedColorInfo` private, but it's a good default.
// insert reverse bit-shifting here
val red: Int get() = 5
val green: Int get() = 6
val blue: Int get() = 7
val alpha: Int get() = 255
companion object {
fun from(red: Int, green: Int, blue: Int, alpha: Int = 255): RGBA {
fun requireInRange(x: Int, name: String) {
require(x in 0..255) {"param $name is supposed to be in range 0..255 but was $x"}
}
requireInRange(red, "red")
requireInRange(green, "green")
requireInRange(blue, "blue")
requireInRange(alpha, "alpha")
// insert bit-shifting here
return RGBA(13)
}
}
override fun toString() = "RGBA(red:$red, green:$green, blue:$blue, alpha:$alpha)"
}
In case you're not familiar with value/inline classes, the class overhead is (in most circumstances except e.g. when you have a list of RGBA values) compiled away, so it's (mostly) an Int at runtime. More here https://kotlinlang.org/docs/inline-classes.html