mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-26 03:58:45 +00:00
commit
ba5c5f17d5
@ -1,5 +1,11 @@
|
||||
# Changelog
|
||||
|
||||
## 0.20.33
|
||||
|
||||
* `Colors`
|
||||
* `Common`:
|
||||
* Add opportunity to use `HEXAColor` with `ahex` colors
|
||||
|
||||
## 0.20.32
|
||||
|
||||
* `Versions`:
|
||||
|
@ -12,16 +12,30 @@ import kotlin.math.floor
|
||||
* * Red (0.5 capacity): `0xff000088u`
|
||||
*
|
||||
* Anyway it is recommended to use
|
||||
*
|
||||
* @param hexaUInt rgba [UInt] in format `0xFFEEBBAA` where FF - red, EE - green, BB - blue` and AA - alpha
|
||||
*/
|
||||
@Serializable
|
||||
@JvmInline
|
||||
value class HEXAColor (
|
||||
val uint: UInt
|
||||
val hexaUInt: UInt
|
||||
) : Comparable<HEXAColor> {
|
||||
/**
|
||||
* @returns [hexaUInt] as a string with format `#FFEEBBAA` where FF - red, EE - green, BB - blue and AA - alpha
|
||||
*/
|
||||
val hexa: String
|
||||
get() = "#${uint.toString(16).padStart(8, '0')}"
|
||||
get() = "#${hexaUInt.toString(16).padStart(8, '0')}"
|
||||
|
||||
/**
|
||||
* @returns [hexaUInt] as a string with format `#FFEEBB` where FF - red, EE - green and BB - blue
|
||||
*/
|
||||
val hex: String
|
||||
get() = hexa.take(7)
|
||||
/**
|
||||
* @returns [hexaUInt] as a string with format `#AAFFEEBB` where AA - alpha, FF - red, EE - green and BB - blue
|
||||
*/
|
||||
val ahex: String
|
||||
get() = "#${a.toString(16).padStart(2, '2')}${hex.drop(1)}"
|
||||
val rgba: String
|
||||
get() = "rgba($r,$g,$b,${aOfOne.toString().take(5)})"
|
||||
val rgb: String
|
||||
@ -30,21 +44,25 @@ value class HEXAColor (
|
||||
get() = "#${r.shortPart()}${g.shortPart()}${b.shortPart()}"
|
||||
val shortHexa: String
|
||||
get() = "$shortHex${a.shortPart()}"
|
||||
val rgbUInt: UInt
|
||||
get() = (hexaUInt / 256u)
|
||||
val rgbInt: Int
|
||||
get() = (uint shr 2).toInt()
|
||||
get() = rgbUInt.toInt()
|
||||
val ahexUInt
|
||||
get() = (a * 0x1000000).toUInt() + rgbUInt
|
||||
|
||||
val r: Int
|
||||
get() = ((uint and 0xff000000u) / 0x1000000u).toInt()
|
||||
get() = ((hexaUInt and 0xff000000u) / 0x1000000u).toInt()
|
||||
val g: Int
|
||||
get() = ((uint and 0x00ff0000u) / 0x10000u).toInt()
|
||||
get() = ((hexaUInt and 0x00ff0000u) / 0x10000u).toInt()
|
||||
val b: Int
|
||||
get() = ((uint and 0x0000ff00u) / 0x100u).toInt()
|
||||
get() = ((hexaUInt and 0x0000ff00u) / 0x100u).toInt()
|
||||
val a: Int
|
||||
get() = ((uint and 0x000000ffu)).toInt()
|
||||
get() = ((hexaUInt and 0x000000ffu)).toInt()
|
||||
val aOfOne: Float
|
||||
get() = a.toFloat() / (0xff)
|
||||
init {
|
||||
require(uint in 0u ..0xffffffffu)
|
||||
require(hexaUInt in 0u ..0xffffffffu)
|
||||
}
|
||||
|
||||
constructor(r: Int, g: Int, b: Int, a: Int) : this(
|
||||
@ -64,7 +82,7 @@ value class HEXAColor (
|
||||
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(
|
||||
r: Int = this.r,
|
||||
@ -121,6 +139,21 @@ value class HEXAColor (
|
||||
else -> color
|
||||
}.lowercase().toUInt(16).let(::HEXAColor)
|
||||
|
||||
/**
|
||||
* Creates [HEXAColor] from [uint] presume it is in format `0xFFEEBBAA` where FF - red, EE - green, BB - blue` and AA - alpha
|
||||
*/
|
||||
fun fromHexa(uint: UInt) = HEXAColor(uint)
|
||||
|
||||
/**
|
||||
* Creates [HEXAColor] from [uint] presume it is in format `0xAAFFEEBB` where AA - alpha, FF - red, EE - green and BB - blue`
|
||||
*/
|
||||
fun fromAhex(uint: UInt) = HEXAColor(
|
||||
a = ((uint and 0xff000000u) / 0x1000000u).toInt(),
|
||||
r = ((uint and 0x00ff0000u) / 0x10000u).toInt(),
|
||||
g = ((uint and 0x0000ff00u) / 0x100u).toInt(),
|
||||
b = ((uint and 0x000000ffu)).toInt()
|
||||
)
|
||||
|
||||
/**
|
||||
* Parsing color from [color]
|
||||
*
|
||||
|
@ -13,6 +13,9 @@ class HexColorTests {
|
||||
val shortHexa: String,
|
||||
val hex: String,
|
||||
val hexa: String,
|
||||
val ahex: String,
|
||||
val ahexUInt: UInt,
|
||||
val rgbUInt: UInt,
|
||||
val rgb: String,
|
||||
val rgba: String,
|
||||
val r: Int,
|
||||
@ -24,11 +27,14 @@ class HexColorTests {
|
||||
val testColors: List<TestColor>
|
||||
get() = listOf(
|
||||
TestColor(
|
||||
color = HEXAColor(uint = 0xff0000ffu),
|
||||
color = HEXAColor(hexaUInt = 0xff0000ffu),
|
||||
shortHex = "#f00",
|
||||
shortHexa = "#f00f",
|
||||
hex = "#ff0000",
|
||||
hexa = "#ff0000ff",
|
||||
ahex = "#ffff0000",
|
||||
ahexUInt = 0xffff0000u,
|
||||
rgbUInt = 0xff0000u,
|
||||
rgb = "rgb(255,0,0)",
|
||||
rgba = "rgba(255,0,0,1.0)",
|
||||
r = 0xff,
|
||||
@ -38,11 +44,14 @@ class HexColorTests {
|
||||
"rgba(255,0,0,1)",
|
||||
),
|
||||
TestColor(
|
||||
color = HEXAColor(uint = 0x00ff00ffu),
|
||||
color = HEXAColor(hexaUInt = 0x00ff00ffu),
|
||||
shortHex = "#0f0",
|
||||
shortHexa = "#0f0f",
|
||||
hex = "#00ff00",
|
||||
hexa = "#00ff00ff",
|
||||
ahex = "#ff00ff00",
|
||||
ahexUInt = 0xff00ff00u,
|
||||
rgbUInt = 0x00ff00u,
|
||||
rgb = "rgb(0,255,0)",
|
||||
rgba = "rgba(0,255,0,1.0)",
|
||||
r = 0x00,
|
||||
@ -57,6 +66,9 @@ class HexColorTests {
|
||||
shortHexa = "#00ff",
|
||||
hex = "#0000ff",
|
||||
hexa = "#0000ffff",
|
||||
ahex = "#ff0000ff",
|
||||
ahexUInt = 0xff0000ffu,
|
||||
rgbUInt = 0x0000ffu,
|
||||
rgb = "rgb(0,0,255)",
|
||||
rgba = "rgba(0,0,255,1.0)",
|
||||
r = 0x00,
|
||||
@ -71,6 +83,9 @@ class HexColorTests {
|
||||
shortHexa = "#f008",
|
||||
hex = "#ff0000",
|
||||
hexa = "#ff000088",
|
||||
ahex = "#88ff0000",
|
||||
ahexUInt = 0x88ff0000u,
|
||||
rgbUInt = 0xff0000u,
|
||||
rgb = "rgb(255,0,0)",
|
||||
rgba = "rgba(255,0,0,0.533)",
|
||||
r = 0xff,
|
||||
@ -84,6 +99,9 @@ class HexColorTests {
|
||||
shortHexa = "#0f08",
|
||||
hex = "#00ff00",
|
||||
hexa = "#00ff0088",
|
||||
ahex = "#8800ff00",
|
||||
ahexUInt = 0x8800ff00u,
|
||||
rgbUInt = 0x00ff00u,
|
||||
rgb = "rgb(0,255,0)",
|
||||
rgba = "rgba(0,255,0,0.533)",
|
||||
r = 0x00,
|
||||
@ -97,6 +115,9 @@ class HexColorTests {
|
||||
shortHexa = "#00f8",
|
||||
hex = "#0000ff",
|
||||
hexa = "#0000ff88",
|
||||
ahex = "#880000ff",
|
||||
ahexUInt = 0x880000ffu,
|
||||
rgbUInt = 0x0000ffu,
|
||||
rgb = "rgb(0,0,255)",
|
||||
rgba = "rgba(0,0,255,0.533)",
|
||||
r = 0x00,
|
||||
@ -110,6 +131,9 @@ class HexColorTests {
|
||||
shortHexa = "#f002",
|
||||
hex = "#ff0000",
|
||||
hexa = "#ff000022",
|
||||
ahex = "#22ff0000",
|
||||
ahexUInt = 0x22ff0000u,
|
||||
rgbUInt = 0xff0000u,
|
||||
rgb = "rgb(255,0,0)",
|
||||
rgba = "rgba(255,0,0,0.133)",
|
||||
r = 0xff,
|
||||
@ -123,6 +147,9 @@ class HexColorTests {
|
||||
shortHexa = "#0f02",
|
||||
hex = "#00ff00",
|
||||
hexa = "#00ff0022",
|
||||
ahex = "#2200ff00",
|
||||
ahexUInt = 0x2200ff00u,
|
||||
rgbUInt = 0x00ff00u,
|
||||
rgb = "rgb(0,255,0)",
|
||||
rgba = "rgba(0,255,0,0.133)",
|
||||
r = 0x00,
|
||||
@ -136,6 +163,9 @@ class HexColorTests {
|
||||
shortHexa = "#00f2",
|
||||
hex = "#0000ff",
|
||||
hexa = "#0000ff22",
|
||||
ahex = "#220000ff",
|
||||
ahexUInt = 0x220000ffu,
|
||||
rgbUInt = 0x0000ffu,
|
||||
rgb = "rgb(0,0,255)",
|
||||
rgba = "rgba(0,0,255,0.133)",
|
||||
r = 0x00,
|
||||
@ -150,6 +180,9 @@ class HexColorTests {
|
||||
testColors.forEach {
|
||||
assertEquals(it.hex, it.color.hex)
|
||||
assertEquals(it.hexa, it.color.hexa)
|
||||
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.shortHexa, it.color.shortHexa)
|
||||
assertEquals(it.rgb, it.color.rgb)
|
||||
@ -158,6 +191,7 @@ class HexColorTests {
|
||||
assertEquals(it.g, it.color.g)
|
||||
assertEquals(it.b, it.color.b)
|
||||
assertEquals(it.a, it.color.a)
|
||||
assertEquals(it.color, HEXAColor.fromAhex(it.ahexUInt))
|
||||
}
|
||||
}
|
||||
|
||||
@ -167,7 +201,7 @@ class HexColorTests {
|
||||
assertEquals(it.color.copy(aOfOne = 1f), HEXAColor.parseStringColor(it.hex))
|
||||
assertEquals(it.color, HEXAColor.parseStringColor(it.hexa))
|
||||
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(a = floor(it.color.a.toFloat() / 16).toInt() * 0x10), HEXAColor.parseStringColor(it.shortHexa))
|
||||
}
|
||||
|
@ -15,5 +15,5 @@ crypto_js_version=4.1.1
|
||||
# Project data
|
||||
|
||||
group=dev.inmo
|
||||
version=0.20.32
|
||||
android_code_version=238
|
||||
version=0.20.33
|
||||
android_code_version=239
|
||||
|
Loading…
Reference in New Issue
Block a user