From b1eb26a89ea31f66b8821c6833fe0779b77d5972 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 12 Feb 2024 14:25:45 +0600 Subject: [PATCH] fixes and testsAdditions in HEXAColor --- .../common/src/commonMain/kotlin/HEXAColor.kt | 30 +++++++++++-------- .../src/commonTest/kotlin/HexColorTests.kt | 28 +++++++++++++++-- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/colors/common/src/commonMain/kotlin/HEXAColor.kt b/colors/common/src/commonMain/kotlin/HEXAColor.kt index 046b1e7f556..b2aa70fec29 100644 --- a/colors/common/src/commonMain/kotlin/HEXAColor.kt +++ b/colors/common/src/commonMain/kotlin/HEXAColor.kt @@ -13,26 +13,26 @@ import kotlin.math.floor * * 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 @JvmInline value class HEXAColor ( - val uint: UInt + val hexaUInt: UInt ) : Comparable { /** - * @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 - 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 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 get() = "#${a.toString(16).padStart(2, '2')}${hex.drop(1)}" @@ -44,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( @@ -78,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, diff --git a/colors/common/src/commonTest/kotlin/HexColorTests.kt b/colors/common/src/commonTest/kotlin/HexColorTests.kt index 91427d82e29..3975605ef5a 100644 --- a/colors/common/src/commonTest/kotlin/HexColorTests.kt +++ b/colors/common/src/commonTest/kotlin/HexColorTests.kt @@ -14,6 +14,8 @@ class HexColorTests { val hex: String, val hexa: String, val ahex: String, + val ahexUInt: UInt, + val rgbUInt: UInt, val rgb: String, val rgba: String, val r: Int, @@ -25,12 +27,14 @@ class HexColorTests { val testColors: List 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, @@ -40,12 +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, @@ -61,6 +67,8 @@ class HexColorTests { hex = "#0000ff", hexa = "#0000ffff", ahex = "#ff0000ff", + ahexUInt = 0xff0000ffu, + rgbUInt = 0x0000ffu, rgb = "rgb(0,0,255)", rgba = "rgba(0,0,255,1.0)", r = 0x00, @@ -76,6 +84,8 @@ class HexColorTests { hex = "#ff0000", hexa = "#ff000088", ahex = "#88ff0000", + ahexUInt = 0x88ff0000u, + rgbUInt = 0xff0000u, rgb = "rgb(255,0,0)", rgba = "rgba(255,0,0,0.533)", r = 0xff, @@ -90,6 +100,8 @@ class HexColorTests { hex = "#00ff00", hexa = "#00ff0088", ahex = "#8800ff00", + ahexUInt = 0x8800ff00u, + rgbUInt = 0x00ff00u, rgb = "rgb(0,255,0)", rgba = "rgba(0,255,0,0.533)", r = 0x00, @@ -104,6 +116,8 @@ class HexColorTests { hex = "#0000ff", hexa = "#0000ff88", ahex = "#880000ff", + ahexUInt = 0x880000ffu, + rgbUInt = 0x0000ffu, rgb = "rgb(0,0,255)", rgba = "rgba(0,0,255,0.533)", r = 0x00, @@ -118,6 +132,8 @@ class HexColorTests { hex = "#ff0000", hexa = "#ff000022", ahex = "#22ff0000", + ahexUInt = 0x22ff0000u, + rgbUInt = 0xff0000u, rgb = "rgb(255,0,0)", rgba = "rgba(255,0,0,0.133)", r = 0xff, @@ -132,6 +148,8 @@ class HexColorTests { hex = "#00ff00", hexa = "#00ff0022", ahex = "#2200ff00", + ahexUInt = 0x2200ff00u, + rgbUInt = 0x00ff00u, rgb = "rgb(0,255,0)", rgba = "rgba(0,255,0,0.133)", r = 0x00, @@ -146,6 +164,8 @@ class HexColorTests { hex = "#0000ff", hexa = "#0000ff22", ahex = "#220000ff", + ahexUInt = 0x220000ffu, + rgbUInt = 0x0000ffu, rgb = "rgb(0,0,255)", rgba = "rgba(0,0,255,0.133)", r = 0x00, @@ -161,6 +181,8 @@ class HexColorTests { 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) @@ -178,7 +200,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)) }