From b26e5f0e58986683f488251a4e4890a77728857c Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 10 May 2024 11:30:28 +0700 Subject: [PATCH] Make RGBAColor work with Little Endian numbers instead of Big Endian --- gradle.properties | 2 +- .../ru/dbotthepony/kommons/math/RGBAColor.kt | 79 ++++++++++++------- 2 files changed, 53 insertions(+), 28 deletions(-) diff --git a/gradle.properties b/gradle.properties index b156a51..84367f4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ kotlin.code.style=official specifyKotlinAsDependency=false projectGroup=ru.dbotthepony.kommons -projectVersion=2.16.1 +projectVersion=2.17.0 guavaDepVersion=33.0.0 gsonDepVersion=2.8.9 diff --git a/src/main/kotlin/ru/dbotthepony/kommons/math/RGBAColor.kt b/src/main/kotlin/ru/dbotthepony/kommons/math/RGBAColor.kt index 9df1e16..47d703d 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/math/RGBAColor.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/math/RGBAColor.kt @@ -14,6 +14,9 @@ private fun hex(value: Int): String { return v } +/** + * All functions involving integers work with Little-Endian numbers + */ class RGBAColor(red: Float, green: Float, blue: Float, alpha: Float = 1f) : IStruct4f, Comparable { constructor(r: Int, g: Int, b: Int) : this((r / 255f), (g / 255f), (b / 255f), 1f) constructor(r: Int, g: Int, b: Int, a: Int) : this((r / 255f), (g / 255f), (b / 255f), (a / 255f)) @@ -29,18 +32,48 @@ class RGBAColor(red: Float, green: Float, blue: Float, alpha: Float = 1f) : IStr val blueInt get() = (blue * 255f).roundToInt() val alphaInt get() = (alpha * 255f).roundToInt() - fun toRGBA(): Int { + /** + * Big-endian RGBA/Little-endian ABGR + */ + fun toABGR(): Int { return (redInt shl 24) or (greenInt shl 16) or (blueInt shl 8) or alphaInt } - fun toARGB(): Int { + /** + * Big-endian ABGR/Little-endian RGBA + */ + fun toRGBA(): Int { + return (alphaInt shl 24) or (blueInt shl 16) or (greenInt shl 8) or redInt + } + + /** + * Big-endian ARGB/Little-endian BGRA + */ + fun toBGRA(): Int { return (alphaInt shl 24) or (redInt shl 16) or (greenInt shl 8) or blueInt } - fun toBGRA(): Int { + /** + * Big-endian BGRA/Little-endian ARGB + */ + fun toARGB(): Int { return (blueInt shl 24) or (greenInt shl 16) or (redInt shl 8) or alphaInt } + /** + * Big-endian BGR/Little-endian RGB + */ + fun toRGB(): Int { + return (blueInt shl 16) or (greenInt shl 8) or redInt + } + + /** + * Big-endian RGB/Little-endian BGR + */ + fun toBGR(): Int { + return (redInt shl 16) or (greenInt shl 8) or blueInt + } + val isFullyTransparent get() = alpha <= 0f val isWhite: Boolean get() = red >= 1f && green >= 1f && blue >= 1f && alpha >= 1f @@ -119,14 +152,6 @@ class RGBAColor(red: Float, green: Float, blue: Float, alpha: Float = 1f) : IStr override fun component3() = blue override fun component4() = alpha - fun toIntInv(): Int { - return (blueInt shl 16) or (greenInt shl 8) or redInt - } - - fun toRGB(): Int { - return (redInt shl 16) or (greenInt shl 8) or blueInt - } - fun copy(red: Float = this.red, green: Float = this.green, blue: Float = this.blue, alpha: Float = this.alpha): RGBAColor { return RGBAColor(red, green, blue, alpha) } @@ -216,28 +241,28 @@ class RGBAColor(red: Float, green: Float, blue: Float, alpha: Float = 1f) : IStr @JvmField val LIGHT_PURPLE = rgb(16733695) @JvmField val YELLOW = rgb(16777045) - fun rgb(color: Int): RGBAColor { - val r = (color and 0xFF0000 ushr 16) / 255f - val g = (color and 0xFF00 ushr 8) / 255f - val b = (color and 0xFF) / 255f - return RGBAColor(r, g, b) - } - - fun rgb(color: Long): RGBAColor { - val r = (color and 0xFF0000 ushr 16) / 255f - val g = (color and 0xFF00 ushr 8) / 255f - val b = (color and 0xFF) / 255f - return RGBAColor(r, g, b) - } - fun bgr(color: Int): RGBAColor { + val r = (color and 0xFF0000 ushr 16) / 255f + val g = (color and 0xFF00 ushr 8) / 255f + val b = (color and 0xFF) / 255f + return RGBAColor(r, g, b) + } + + fun bgr(color: Long): RGBAColor { + val r = (color and 0xFF0000 ushr 16) / 255f + val g = (color and 0xFF00 ushr 8) / 255f + val b = (color and 0xFF) / 255f + return RGBAColor(r, g, b) + } + + fun rgb(color: Int): RGBAColor { val r = (color and 0xFF0000 ushr 16) / 255f val g = (color and 0xFF00 ushr 8) / 255f val b = (color and 0xFF) / 255f return RGBAColor(b, g, r) } - fun bgr(color: Long): RGBAColor { + fun rgb(color: Long): RGBAColor { val r = (color and 0xFF0000 ushr 16) / 255f val g = (color and 0xFF00 ushr 8) / 255f val b = (color and 0xFF) / 255f @@ -252,7 +277,7 @@ class RGBAColor(red: Float, green: Float, blue: Float, alpha: Float = 1f) : IStr return RGBAColor(a, b, g, r) } - fun argb(color: Int): RGBAColor { + fun bgra(color: Int): RGBAColor { val a = (color and -0x1000000 ushr 24) / 255f val r = (color and 0xFF0000 ushr 16) / 255f val g = (color and 0xFF00 ushr 8) / 255f