Make RGBAColor work with Little Endian numbers instead of Big Endian
This commit is contained in:
parent
9d6205f9ff
commit
b26e5f0e58
@ -4,7 +4,7 @@ kotlin.code.style=official
|
|||||||
specifyKotlinAsDependency=false
|
specifyKotlinAsDependency=false
|
||||||
|
|
||||||
projectGroup=ru.dbotthepony.kommons
|
projectGroup=ru.dbotthepony.kommons
|
||||||
projectVersion=2.16.1
|
projectVersion=2.17.0
|
||||||
|
|
||||||
guavaDepVersion=33.0.0
|
guavaDepVersion=33.0.0
|
||||||
gsonDepVersion=2.8.9
|
gsonDepVersion=2.8.9
|
||||||
|
@ -14,6 +14,9 @@ private fun hex(value: Int): String {
|
|||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* All functions involving integers work with Little-Endian numbers
|
||||||
|
*/
|
||||||
class RGBAColor(red: Float, green: Float, blue: Float, alpha: Float = 1f) : IStruct4f, Comparable<RGBAColor> {
|
class RGBAColor(red: Float, green: Float, blue: Float, alpha: Float = 1f) : IStruct4f, Comparable<RGBAColor> {
|
||||||
constructor(r: Int, g: Int, b: Int) : this((r / 255f), (g / 255f), (b / 255f), 1f)
|
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))
|
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 blueInt get() = (blue * 255f).roundToInt()
|
||||||
val alphaInt get() = (alpha * 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
|
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
|
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
|
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 isFullyTransparent get() = alpha <= 0f
|
||||||
val isWhite: Boolean get() = red >= 1f && green >= 1f && blue >= 1f && alpha >= 1f
|
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 component3() = blue
|
||||||
override fun component4() = alpha
|
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 {
|
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)
|
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 LIGHT_PURPLE = rgb(16733695)
|
||||||
@JvmField val YELLOW = rgb(16777045)
|
@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 {
|
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 r = (color and 0xFF0000 ushr 16) / 255f
|
||||||
val g = (color and 0xFF00 ushr 8) / 255f
|
val g = (color and 0xFF00 ushr 8) / 255f
|
||||||
val b = (color and 0xFF) / 255f
|
val b = (color and 0xFF) / 255f
|
||||||
return RGBAColor(b, g, r)
|
return RGBAColor(b, g, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun bgr(color: Long): RGBAColor {
|
fun rgb(color: Long): RGBAColor {
|
||||||
val r = (color and 0xFF0000 ushr 16) / 255f
|
val r = (color and 0xFF0000 ushr 16) / 255f
|
||||||
val g = (color and 0xFF00 ushr 8) / 255f
|
val g = (color and 0xFF00 ushr 8) / 255f
|
||||||
val b = (color and 0xFF) / 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)
|
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 a = (color and -0x1000000 ushr 24) / 255f
|
||||||
val r = (color and 0xFF0000 ushr 16) / 255f
|
val r = (color and 0xFF0000 ushr 16) / 255f
|
||||||
val g = (color and 0xFF00 ushr 8) / 255f
|
val g = (color and 0xFF00 ushr 8) / 255f
|
||||||
|
Loading…
Reference in New Issue
Block a user