mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-22 16:23:50 +00:00
fixes and testsAdditions in HEXAColor
This commit is contained in:
parent
c9d04b6698
commit
b1eb26a89e
@ -13,26 +13,26 @@ import kotlin.math.floor
|
|||||||
*
|
*
|
||||||
* Anyway it is recommended to use
|
* Anyway it is recommended to use
|
||||||
*
|
*
|
||||||
* @param uint rgba [UInt] in format `0xFFEEBBAA` where FF - red, EE - green, BB - blue` and AA - alpha
|
* @param hexaUInt rgba [UInt] in format `0xFFEEBBAA` where FF - red, EE - green, BB - blue` and AA - alpha
|
||||||
*/
|
*/
|
||||||
@Serializable
|
@Serializable
|
||||||
@JvmInline
|
@JvmInline
|
||||||
value class HEXAColor (
|
value class HEXAColor (
|
||||||
val uint: UInt
|
val hexaUInt: UInt
|
||||||
) : Comparable<HEXAColor> {
|
) : Comparable<HEXAColor> {
|
||||||
/**
|
/**
|
||||||
* @returns [uint] as a string with format `#FFEEBBAA` where FF - red, EE - green, BB - blue and AA - alpha
|
* @returns [hexaUInt] as a string with format `#FFEEBBAA` where FF - red, EE - green, BB - blue and AA - alpha
|
||||||
*/
|
*/
|
||||||
val hexa: String
|
val hexa: String
|
||||||
get() = "#${uint.toString(16).padStart(8, '0')}"
|
get() = "#${hexaUInt.toString(16).padStart(8, '0')}"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @returns [uint] as a string with format `#FFEEBB` where FF - red, EE - green and BB - blue
|
* @returns [hexaUInt] as a string with format `#FFEEBB` where FF - red, EE - green and BB - blue
|
||||||
*/
|
*/
|
||||||
val hex: String
|
val hex: String
|
||||||
get() = hexa.take(7)
|
get() = hexa.take(7)
|
||||||
/**
|
/**
|
||||||
* @returns [uint] as a string with format `#AAFFEEBB` where AA - alpha, FF - red, EE - green and BB - blue
|
* @returns [hexaUInt] as a string with format `#AAFFEEBB` where AA - alpha, FF - red, EE - green and BB - blue
|
||||||
*/
|
*/
|
||||||
val ahex: String
|
val ahex: String
|
||||||
get() = "#${a.toString(16).padStart(2, '2')}${hex.drop(1)}"
|
get() = "#${a.toString(16).padStart(2, '2')}${hex.drop(1)}"
|
||||||
@ -44,21 +44,25 @@ value class HEXAColor (
|
|||||||
get() = "#${r.shortPart()}${g.shortPart()}${b.shortPart()}"
|
get() = "#${r.shortPart()}${g.shortPart()}${b.shortPart()}"
|
||||||
val shortHexa: String
|
val shortHexa: String
|
||||||
get() = "$shortHex${a.shortPart()}"
|
get() = "$shortHex${a.shortPart()}"
|
||||||
|
val rgbUInt: UInt
|
||||||
|
get() = (hexaUInt / 256u)
|
||||||
val rgbInt: Int
|
val rgbInt: Int
|
||||||
get() = (uint shr 2).toInt()
|
get() = rgbUInt.toInt()
|
||||||
|
val ahexUInt
|
||||||
|
get() = (a * 0x1000000).toUInt() + rgbUInt
|
||||||
|
|
||||||
val r: Int
|
val r: Int
|
||||||
get() = ((uint and 0xff000000u) / 0x1000000u).toInt()
|
get() = ((hexaUInt and 0xff000000u) / 0x1000000u).toInt()
|
||||||
val g: Int
|
val g: Int
|
||||||
get() = ((uint and 0x00ff0000u) / 0x10000u).toInt()
|
get() = ((hexaUInt and 0x00ff0000u) / 0x10000u).toInt()
|
||||||
val b: Int
|
val b: Int
|
||||||
get() = ((uint and 0x0000ff00u) / 0x100u).toInt()
|
get() = ((hexaUInt and 0x0000ff00u) / 0x100u).toInt()
|
||||||
val a: Int
|
val a: Int
|
||||||
get() = ((uint and 0x000000ffu)).toInt()
|
get() = ((hexaUInt and 0x000000ffu)).toInt()
|
||||||
val aOfOne: Float
|
val aOfOne: Float
|
||||||
get() = a.toFloat() / (0xff)
|
get() = a.toFloat() / (0xff)
|
||||||
init {
|
init {
|
||||||
require(uint in 0u ..0xffffffffu)
|
require(hexaUInt in 0u ..0xffffffffu)
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(r: Int, g: Int, b: Int, a: Int) : this(
|
constructor(r: Int, g: Int, b: Int, a: Int) : this(
|
||||||
@ -78,7 +82,7 @@ value class HEXAColor (
|
|||||||
return hexa
|
return hexa
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun compareTo(other: HEXAColor): Int = (uint - other.uint).coerceIn(Int.MIN_VALUE.toUInt(), Int.MAX_VALUE.toLong().toUInt()).toInt()
|
override fun compareTo(other: HEXAColor): Int = (hexaUInt - other.hexaUInt).coerceIn(Int.MIN_VALUE.toUInt(), Int.MAX_VALUE.toLong().toUInt()).toInt()
|
||||||
|
|
||||||
fun copy(
|
fun copy(
|
||||||
r: Int = this.r,
|
r: Int = this.r,
|
||||||
|
@ -14,6 +14,8 @@ class HexColorTests {
|
|||||||
val hex: String,
|
val hex: String,
|
||||||
val hexa: String,
|
val hexa: String,
|
||||||
val ahex: String,
|
val ahex: String,
|
||||||
|
val ahexUInt: UInt,
|
||||||
|
val rgbUInt: UInt,
|
||||||
val rgb: String,
|
val rgb: String,
|
||||||
val rgba: String,
|
val rgba: String,
|
||||||
val r: Int,
|
val r: Int,
|
||||||
@ -25,12 +27,14 @@ class HexColorTests {
|
|||||||
val testColors: List<TestColor>
|
val testColors: List<TestColor>
|
||||||
get() = listOf(
|
get() = listOf(
|
||||||
TestColor(
|
TestColor(
|
||||||
color = HEXAColor(uint = 0xff0000ffu),
|
color = HEXAColor(hexaUInt = 0xff0000ffu),
|
||||||
shortHex = "#f00",
|
shortHex = "#f00",
|
||||||
shortHexa = "#f00f",
|
shortHexa = "#f00f",
|
||||||
hex = "#ff0000",
|
hex = "#ff0000",
|
||||||
hexa = "#ff0000ff",
|
hexa = "#ff0000ff",
|
||||||
ahex = "#ffff0000",
|
ahex = "#ffff0000",
|
||||||
|
ahexUInt = 0xffff0000u,
|
||||||
|
rgbUInt = 0xff0000u,
|
||||||
rgb = "rgb(255,0,0)",
|
rgb = "rgb(255,0,0)",
|
||||||
rgba = "rgba(255,0,0,1.0)",
|
rgba = "rgba(255,0,0,1.0)",
|
||||||
r = 0xff,
|
r = 0xff,
|
||||||
@ -40,12 +44,14 @@ class HexColorTests {
|
|||||||
"rgba(255,0,0,1)",
|
"rgba(255,0,0,1)",
|
||||||
),
|
),
|
||||||
TestColor(
|
TestColor(
|
||||||
color = HEXAColor(uint = 0x00ff00ffu),
|
color = HEXAColor(hexaUInt = 0x00ff00ffu),
|
||||||
shortHex = "#0f0",
|
shortHex = "#0f0",
|
||||||
shortHexa = "#0f0f",
|
shortHexa = "#0f0f",
|
||||||
hex = "#00ff00",
|
hex = "#00ff00",
|
||||||
hexa = "#00ff00ff",
|
hexa = "#00ff00ff",
|
||||||
ahex = "#ff00ff00",
|
ahex = "#ff00ff00",
|
||||||
|
ahexUInt = 0xff00ff00u,
|
||||||
|
rgbUInt = 0x00ff00u,
|
||||||
rgb = "rgb(0,255,0)",
|
rgb = "rgb(0,255,0)",
|
||||||
rgba = "rgba(0,255,0,1.0)",
|
rgba = "rgba(0,255,0,1.0)",
|
||||||
r = 0x00,
|
r = 0x00,
|
||||||
@ -61,6 +67,8 @@ class HexColorTests {
|
|||||||
hex = "#0000ff",
|
hex = "#0000ff",
|
||||||
hexa = "#0000ffff",
|
hexa = "#0000ffff",
|
||||||
ahex = "#ff0000ff",
|
ahex = "#ff0000ff",
|
||||||
|
ahexUInt = 0xff0000ffu,
|
||||||
|
rgbUInt = 0x0000ffu,
|
||||||
rgb = "rgb(0,0,255)",
|
rgb = "rgb(0,0,255)",
|
||||||
rgba = "rgba(0,0,255,1.0)",
|
rgba = "rgba(0,0,255,1.0)",
|
||||||
r = 0x00,
|
r = 0x00,
|
||||||
@ -76,6 +84,8 @@ class HexColorTests {
|
|||||||
hex = "#ff0000",
|
hex = "#ff0000",
|
||||||
hexa = "#ff000088",
|
hexa = "#ff000088",
|
||||||
ahex = "#88ff0000",
|
ahex = "#88ff0000",
|
||||||
|
ahexUInt = 0x88ff0000u,
|
||||||
|
rgbUInt = 0xff0000u,
|
||||||
rgb = "rgb(255,0,0)",
|
rgb = "rgb(255,0,0)",
|
||||||
rgba = "rgba(255,0,0,0.533)",
|
rgba = "rgba(255,0,0,0.533)",
|
||||||
r = 0xff,
|
r = 0xff,
|
||||||
@ -90,6 +100,8 @@ class HexColorTests {
|
|||||||
hex = "#00ff00",
|
hex = "#00ff00",
|
||||||
hexa = "#00ff0088",
|
hexa = "#00ff0088",
|
||||||
ahex = "#8800ff00",
|
ahex = "#8800ff00",
|
||||||
|
ahexUInt = 0x8800ff00u,
|
||||||
|
rgbUInt = 0x00ff00u,
|
||||||
rgb = "rgb(0,255,0)",
|
rgb = "rgb(0,255,0)",
|
||||||
rgba = "rgba(0,255,0,0.533)",
|
rgba = "rgba(0,255,0,0.533)",
|
||||||
r = 0x00,
|
r = 0x00,
|
||||||
@ -104,6 +116,8 @@ class HexColorTests {
|
|||||||
hex = "#0000ff",
|
hex = "#0000ff",
|
||||||
hexa = "#0000ff88",
|
hexa = "#0000ff88",
|
||||||
ahex = "#880000ff",
|
ahex = "#880000ff",
|
||||||
|
ahexUInt = 0x880000ffu,
|
||||||
|
rgbUInt = 0x0000ffu,
|
||||||
rgb = "rgb(0,0,255)",
|
rgb = "rgb(0,0,255)",
|
||||||
rgba = "rgba(0,0,255,0.533)",
|
rgba = "rgba(0,0,255,0.533)",
|
||||||
r = 0x00,
|
r = 0x00,
|
||||||
@ -118,6 +132,8 @@ class HexColorTests {
|
|||||||
hex = "#ff0000",
|
hex = "#ff0000",
|
||||||
hexa = "#ff000022",
|
hexa = "#ff000022",
|
||||||
ahex = "#22ff0000",
|
ahex = "#22ff0000",
|
||||||
|
ahexUInt = 0x22ff0000u,
|
||||||
|
rgbUInt = 0xff0000u,
|
||||||
rgb = "rgb(255,0,0)",
|
rgb = "rgb(255,0,0)",
|
||||||
rgba = "rgba(255,0,0,0.133)",
|
rgba = "rgba(255,0,0,0.133)",
|
||||||
r = 0xff,
|
r = 0xff,
|
||||||
@ -132,6 +148,8 @@ class HexColorTests {
|
|||||||
hex = "#00ff00",
|
hex = "#00ff00",
|
||||||
hexa = "#00ff0022",
|
hexa = "#00ff0022",
|
||||||
ahex = "#2200ff00",
|
ahex = "#2200ff00",
|
||||||
|
ahexUInt = 0x2200ff00u,
|
||||||
|
rgbUInt = 0x00ff00u,
|
||||||
rgb = "rgb(0,255,0)",
|
rgb = "rgb(0,255,0)",
|
||||||
rgba = "rgba(0,255,0,0.133)",
|
rgba = "rgba(0,255,0,0.133)",
|
||||||
r = 0x00,
|
r = 0x00,
|
||||||
@ -146,6 +164,8 @@ class HexColorTests {
|
|||||||
hex = "#0000ff",
|
hex = "#0000ff",
|
||||||
hexa = "#0000ff22",
|
hexa = "#0000ff22",
|
||||||
ahex = "#220000ff",
|
ahex = "#220000ff",
|
||||||
|
ahexUInt = 0x220000ffu,
|
||||||
|
rgbUInt = 0x0000ffu,
|
||||||
rgb = "rgb(0,0,255)",
|
rgb = "rgb(0,0,255)",
|
||||||
rgba = "rgba(0,0,255,0.133)",
|
rgba = "rgba(0,0,255,0.133)",
|
||||||
r = 0x00,
|
r = 0x00,
|
||||||
@ -161,6 +181,8 @@ class HexColorTests {
|
|||||||
assertEquals(it.hex, it.color.hex)
|
assertEquals(it.hex, it.color.hex)
|
||||||
assertEquals(it.hexa, it.color.hexa)
|
assertEquals(it.hexa, it.color.hexa)
|
||||||
assertEquals(it.ahex, it.color.ahex)
|
assertEquals(it.ahex, it.color.ahex)
|
||||||
|
assertEquals(it.rgbUInt, it.color.rgbUInt)
|
||||||
|
assertEquals(it.ahexUInt, it.color.ahexUInt)
|
||||||
assertEquals(it.shortHex, it.color.shortHex)
|
assertEquals(it.shortHex, it.color.shortHex)
|
||||||
assertEquals(it.shortHexa, it.color.shortHexa)
|
assertEquals(it.shortHexa, it.color.shortHexa)
|
||||||
assertEquals(it.rgb, it.color.rgb)
|
assertEquals(it.rgb, it.color.rgb)
|
||||||
@ -178,7 +200,7 @@ class HexColorTests {
|
|||||||
assertEquals(it.color.copy(aOfOne = 1f), HEXAColor.parseStringColor(it.hex))
|
assertEquals(it.color.copy(aOfOne = 1f), HEXAColor.parseStringColor(it.hex))
|
||||||
assertEquals(it.color, HEXAColor.parseStringColor(it.hexa))
|
assertEquals(it.color, HEXAColor.parseStringColor(it.hexa))
|
||||||
assertEquals(it.color.copy(aOfOne = 1f), HEXAColor.parseStringColor(it.rgb))
|
assertEquals(it.color.copy(aOfOne = 1f), HEXAColor.parseStringColor(it.rgb))
|
||||||
assertTrue(it.color.uint.toInt() - HEXAColor.parseStringColor(it.rgba).uint.toInt() in -0x1 .. 0x1, )
|
assertTrue(it.color.hexaUInt.toInt() - HEXAColor.parseStringColor(it.rgba).hexaUInt.toInt() in -0x1 .. 0x1, )
|
||||||
assertEquals(it.color.copy(aOfOne = 1f), HEXAColor.parseStringColor(it.shortHex))
|
assertEquals(it.color.copy(aOfOne = 1f), HEXAColor.parseStringColor(it.shortHex))
|
||||||
assertEquals(it.color.copy(a = floor(it.color.a.toFloat() / 16).toInt() * 0x10), HEXAColor.parseStringColor(it.shortHexa))
|
assertEquals(it.color.copy(a = floor(it.color.a.toFloat() / 16).toInt() * 0x10), HEXAColor.parseStringColor(it.shortHexa))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user