From c9d04b66980bea39f5c78bbf39461d31520faebb Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 12 Feb 2024 13:58:06 +0600 Subject: [PATCH] add ahex to color --- CHANGELOG.md | 4 +++ .../common/src/commonMain/kotlin/HEXAColor.kt | 29 +++++++++++++++++++ .../src/commonTest/kotlin/HexColorTests.kt | 11 +++++++ 3 files changed, 44 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e41f7c6508d..adc76bd528f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.20.33 +* `Colors` + * `Common`: + * Add opportunity to use `HEXAColor` with `ahex` colors + ## 0.20.32 * `Versions`: diff --git a/colors/common/src/commonMain/kotlin/HEXAColor.kt b/colors/common/src/commonMain/kotlin/HEXAColor.kt index f40e547121f..046b1e7f556 100644 --- a/colors/common/src/commonMain/kotlin/HEXAColor.kt +++ b/colors/common/src/commonMain/kotlin/HEXAColor.kt @@ -12,16 +12,30 @@ import kotlin.math.floor * * Red (0.5 capacity): `0xff000088u` * * Anyway it is recommended to use + * + * @param uint rgba [UInt] in format `0xFFEEBBAA` where FF - red, EE - green, BB - blue` and AA - alpha */ @Serializable @JvmInline value class HEXAColor ( val uint: UInt ) : Comparable { + /** + * @returns [uint] 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')}" + + /** + * @returns [uint] as a string with format `#FFEEBB` where FF - red, EE - green and BB - blue + */ val hex: String get() = hexa.take(7) + /** + * @returns [uint] 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 @@ -121,6 +135,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] * diff --git a/colors/common/src/commonTest/kotlin/HexColorTests.kt b/colors/common/src/commonTest/kotlin/HexColorTests.kt index 8d710c82a53..91427d82e29 100644 --- a/colors/common/src/commonTest/kotlin/HexColorTests.kt +++ b/colors/common/src/commonTest/kotlin/HexColorTests.kt @@ -13,6 +13,7 @@ class HexColorTests { val shortHexa: String, val hex: String, val hexa: String, + val ahex: String, val rgb: String, val rgba: String, val r: Int, @@ -29,6 +30,7 @@ class HexColorTests { shortHexa = "#f00f", hex = "#ff0000", hexa = "#ff0000ff", + ahex = "#ffff0000", rgb = "rgb(255,0,0)", rgba = "rgba(255,0,0,1.0)", r = 0xff, @@ -43,6 +45,7 @@ class HexColorTests { shortHexa = "#0f0f", hex = "#00ff00", hexa = "#00ff00ff", + ahex = "#ff00ff00", rgb = "rgb(0,255,0)", rgba = "rgba(0,255,0,1.0)", r = 0x00, @@ -57,6 +60,7 @@ class HexColorTests { shortHexa = "#00ff", hex = "#0000ff", hexa = "#0000ffff", + ahex = "#ff0000ff", rgb = "rgb(0,0,255)", rgba = "rgba(0,0,255,1.0)", r = 0x00, @@ -71,6 +75,7 @@ class HexColorTests { shortHexa = "#f008", hex = "#ff0000", hexa = "#ff000088", + ahex = "#88ff0000", rgb = "rgb(255,0,0)", rgba = "rgba(255,0,0,0.533)", r = 0xff, @@ -84,6 +89,7 @@ class HexColorTests { shortHexa = "#0f08", hex = "#00ff00", hexa = "#00ff0088", + ahex = "#8800ff00", rgb = "rgb(0,255,0)", rgba = "rgba(0,255,0,0.533)", r = 0x00, @@ -97,6 +103,7 @@ class HexColorTests { shortHexa = "#00f8", hex = "#0000ff", hexa = "#0000ff88", + ahex = "#880000ff", rgb = "rgb(0,0,255)", rgba = "rgba(0,0,255,0.533)", r = 0x00, @@ -110,6 +117,7 @@ class HexColorTests { shortHexa = "#f002", hex = "#ff0000", hexa = "#ff000022", + ahex = "#22ff0000", rgb = "rgb(255,0,0)", rgba = "rgba(255,0,0,0.133)", r = 0xff, @@ -123,6 +131,7 @@ class HexColorTests { shortHexa = "#0f02", hex = "#00ff00", hexa = "#00ff0022", + ahex = "#2200ff00", rgb = "rgb(0,255,0)", rgba = "rgba(0,255,0,0.133)", r = 0x00, @@ -136,6 +145,7 @@ class HexColorTests { shortHexa = "#00f2", hex = "#0000ff", hexa = "#0000ff22", + ahex = "#220000ff", rgb = "rgb(0,0,255)", rgba = "rgba(0,0,255,0.133)", r = 0x00, @@ -150,6 +160,7 @@ class HexColorTests { testColors.forEach { assertEquals(it.hex, it.color.hex) assertEquals(it.hexa, it.color.hexa) + assertEquals(it.ahex, it.color.ahex) assertEquals(it.shortHex, it.color.shortHex) assertEquals(it.shortHexa, it.color.shortHexa) assertEquals(it.rgb, it.color.rgb)