mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-23 17:17:14 +00:00
add rgb and rgba representations of HEXAColor
This commit is contained in:
parent
ff59b0cc9c
commit
303e1e6281
@ -22,14 +22,16 @@ value class HEXAColor (
|
||||
get() = "#${uint.toString(16).padStart(8, '0')}"
|
||||
val hex: String
|
||||
get() = hexa.take(7)
|
||||
val rgba: String
|
||||
get() = "rgba($r,$g,$b,${aOfOne.toString().take(5)})"
|
||||
val rgb: String
|
||||
get() = "rgb($r,$g,$b)"
|
||||
val shortHex: String
|
||||
get() = "#${r.shortPart()}${g.shortPart()}${b.shortPart()}"
|
||||
val shortHexa: String
|
||||
get() = "$shortHex${a.shortPart()}"
|
||||
val rgbInt: Int
|
||||
get() = (uint shr 2).toInt()
|
||||
val alphaOfOne: Float
|
||||
get() = (uint and 0xffu).toFloat() / 256f
|
||||
|
||||
val r: Int
|
||||
get() = ((uint and 0xff000000u) / 0x1000000u).toInt()
|
||||
@ -69,13 +71,13 @@ value class HEXAColor (
|
||||
g: Int = this.g,
|
||||
b: Int = this.b,
|
||||
aOfOne: Float = this.aOfOne
|
||||
) = HEXAColor(r, g, b, aOfOne)
|
||||
) = HEXAColor(r = r, g = g, b = b, aOfOne = aOfOne)
|
||||
fun copy(
|
||||
r: Int = this.r,
|
||||
g: Int = this.g,
|
||||
b: Int = this.b,
|
||||
a: Int
|
||||
) = HEXAColor(r, g, b, a)
|
||||
) = HEXAColor(r = r, g = g, b = b, a = a)
|
||||
|
||||
companion object {
|
||||
/**
|
||||
@ -105,14 +107,15 @@ value class HEXAColor (
|
||||
.removeSuffix(")")
|
||||
.replace(Regex("\\s"), "")
|
||||
.split(",")
|
||||
.map { it.toInt().toString(16) }
|
||||
.joinToString("", postfix = "ff")
|
||||
.joinToString("", postfix = "ff") {
|
||||
it.toInt().toString(16).padStart(2, '0')
|
||||
}
|
||||
color.startsWith("rgba(") -> color
|
||||
.removePrefix("rgba(")
|
||||
.removeSuffix(")")
|
||||
.replace(Regex("\\s"), "")
|
||||
.split(",").let {
|
||||
it.take(3).map { it.toInt().toString(16) } + (it.last().toFloat() * 0xff).toInt().toString(16)
|
||||
it.take(3).map { it.toInt().toString(16).padStart(2, '0') } + (it.last().toFloat() * 0xff).toInt().toString(16).padStart(2, '0')
|
||||
}
|
||||
.joinToString("")
|
||||
else -> color
|
||||
|
@ -1,17 +1,20 @@
|
||||
package dev.inmo.micro_utils.colors.common
|
||||
|
||||
import kotlin.math.floor
|
||||
import kotlin.math.round
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class HexColorTests {
|
||||
val alphaRgbaPrecision = 5
|
||||
class TestColor(
|
||||
val color: HEXAColor,
|
||||
val shortHex: String?,
|
||||
val shortHexa: String?,
|
||||
val shortHex: String,
|
||||
val shortHexa: String,
|
||||
val hex: String,
|
||||
val hexa: String,
|
||||
val rgb: String,
|
||||
val rgba: String,
|
||||
val r: Int,
|
||||
val g: Int,
|
||||
val b: Int,
|
||||
@ -25,6 +28,8 @@ class HexColorTests {
|
||||
shortHexa = "#f00f",
|
||||
hex = "#ff0000",
|
||||
hexa = "#ff0000ff",
|
||||
rgb = "rgb(255,0,0)",
|
||||
rgba = "rgba(255,0,0,1.0)",
|
||||
r = 0xff,
|
||||
g = 0x00,
|
||||
b = 0x00,
|
||||
@ -36,6 +41,8 @@ class HexColorTests {
|
||||
shortHexa = "#0f0f",
|
||||
hex = "#00ff00",
|
||||
hexa = "#00ff00ff",
|
||||
rgb = "rgb(0,255,0)",
|
||||
rgba = "rgba(0,255,0,1.0)",
|
||||
r = 0x00,
|
||||
g = 0xff,
|
||||
b = 0x00,
|
||||
@ -47,6 +54,8 @@ class HexColorTests {
|
||||
shortHexa = "#00ff",
|
||||
hex = "#0000ff",
|
||||
hexa = "#0000ffff",
|
||||
rgb = "rgb(0,0,255)",
|
||||
rgba = "rgba(0,0,255,1.0)",
|
||||
r = 0x00,
|
||||
g = 0x00,
|
||||
b = 0xff,
|
||||
@ -58,6 +67,8 @@ class HexColorTests {
|
||||
shortHexa = "#f008",
|
||||
hex = "#ff0000",
|
||||
hexa = "#ff000088",
|
||||
rgb = "rgb(255,0,0)",
|
||||
rgba = "rgba(255,0,0,0.533)",
|
||||
r = 0xff,
|
||||
g = 0x00,
|
||||
b = 0x00,
|
||||
@ -69,6 +80,8 @@ class HexColorTests {
|
||||
shortHexa = "#0f08",
|
||||
hex = "#00ff00",
|
||||
hexa = "#00ff0088",
|
||||
rgb = "rgb(0,255,0)",
|
||||
rgba = "rgba(0,255,0,0.533)",
|
||||
r = 0x00,
|
||||
g = 0xff,
|
||||
b = 0x00,
|
||||
@ -80,6 +93,8 @@ class HexColorTests {
|
||||
shortHexa = "#00f8",
|
||||
hex = "#0000ff",
|
||||
hexa = "#0000ff88",
|
||||
rgb = "rgb(0,0,255)",
|
||||
rgba = "rgba(0,0,255,0.533)",
|
||||
r = 0x00,
|
||||
g = 0x00,
|
||||
b = 0xff,
|
||||
@ -91,21 +106,25 @@ class HexColorTests {
|
||||
shortHexa = "#f002",
|
||||
hex = "#ff0000",
|
||||
hexa = "#ff000022",
|
||||
rgb = "rgb(255,0,0)",
|
||||
rgba = "rgba(255,0,0,0.133)",
|
||||
r = 0xff,
|
||||
g = 0x00,
|
||||
b = 0x00,
|
||||
a = 0x22,
|
||||
),
|
||||
TestColor(
|
||||
HEXAColor(0x00ff0022u),
|
||||
"#0f0",
|
||||
"#0f02",
|
||||
"#00ff00",
|
||||
"#00ff0022",
|
||||
0x00,
|
||||
0xff,
|
||||
0x00,
|
||||
0x22,
|
||||
color = HEXAColor(0x00ff0022u),
|
||||
shortHex = "#0f0",
|
||||
shortHexa = "#0f02",
|
||||
hex = "#00ff00",
|
||||
hexa = "#00ff0022",
|
||||
rgb = "rgb(0,255,0)",
|
||||
rgba = "rgba(0,255,0,0.133)",
|
||||
r = 0x00,
|
||||
g = 0xff,
|
||||
b = 0x00,
|
||||
a = 0x22,
|
||||
),
|
||||
TestColor(
|
||||
color = HEXAColor(0x0000ff22u),
|
||||
@ -113,6 +132,8 @@ class HexColorTests {
|
||||
shortHexa = "#00f2",
|
||||
hex = "#0000ff",
|
||||
hexa = "#0000ff22",
|
||||
rgb = "rgb(0,0,255)",
|
||||
rgba = "rgba(0,0,255,0.133)",
|
||||
r = 0x00,
|
||||
g = 0x00,
|
||||
b = 0xff,
|
||||
@ -127,6 +148,8 @@ class HexColorTests {
|
||||
assertEquals(it.hexa, it.color.hexa)
|
||||
assertEquals(it.shortHex, it.color.shortHex)
|
||||
assertEquals(it.shortHexa, it.color.shortHexa)
|
||||
assertEquals(it.rgb, it.color.rgb)
|
||||
assertEquals(it.rgba, it.color.rgba)
|
||||
assertEquals(it.r, it.color.r)
|
||||
assertEquals(it.g, it.color.g)
|
||||
assertEquals(it.b, it.color.b)
|
||||
@ -137,14 +160,12 @@ class HexColorTests {
|
||||
@Test
|
||||
fun testHexParseColor() {
|
||||
testColors.forEach {
|
||||
assertEquals(it.color, HEXAColor.parseStringColor(it.hexa))
|
||||
assertEquals(it.color.copy(aOfOne = 1f), HEXAColor.parseStringColor(it.hex))
|
||||
it.shortHex ?.let { _ ->
|
||||
assertEquals(it.color.copy(aOfOne = 1f), HEXAColor.parseStringColor(it.shortHex))
|
||||
}
|
||||
it.shortHexa ?.let { _ ->
|
||||
assertEquals(it.color.copy(a = floor(it.color.a.toFloat() / 16).toInt() * 0x10), HEXAColor.parseStringColor(it.shortHexa))
|
||||
}
|
||||
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, )
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user