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