Created
March 19, 2024 11:52
-
-
Save jershell/0c98662051970289220b799265434fd4 to your computer and use it in GitHub Desktop.
compose color to html color
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.graphics.toArgb | |
import io.ktor.util.* | |
import kotlin.math.roundToInt | |
import kotlin.test.DefaultAsserter.assertEquals | |
import kotlin.test.Test | |
class UtilsKtTest { | |
private val dataSet = listOf( | |
Color.Blue to HtmlColor(rgb = HtmlRGB(red = 0u, green = 0u, blue = 255u), rgbHex = "0000FF", alpha = 1f), | |
Color.Red to HtmlColor(rgb = HtmlRGB(red = 255u, green = 0u, blue = 0u), rgbHex = "FF0000", alpha = 1f), | |
Color.Green to HtmlColor(rgb = HtmlRGB(red = 0u, green = 255u, blue = 0u), rgbHex = "00FF00", alpha = 1f), | |
Color(0xFF69359C) to HtmlColor(rgb = HtmlRGB(red = 105u, green = 53u, blue = 156u), rgbHex = "69359C", alpha = 1f), | |
Color(0xe7d743c3) to HtmlColor(rgb = HtmlRGB(red = 215u, green = 67u, blue = 195u), rgbHex = "D743C3", alpha = 0.91f), | |
Color(0xe7d743c3) to HtmlColor(rgb = HtmlRGB(red = 215u, green = 67u, blue = 195u), rgbHex = "D743C3", alpha = 0.91f), | |
Color(0xf301e76e) to HtmlColor(rgb = HtmlRGB(red = 1u, green = 231u, blue = 110u), rgbHex = "01E76E", alpha = 0.95f), | |
Color(0x93e8ffdd) to HtmlColor(rgb = HtmlRGB(red = 232u, green = 255u, blue = 221u), rgbHex = "E8FFDD", alpha = 0.58f), | |
Color(0x38d08517) to HtmlColor(rgb = HtmlRGB(red = 208u, green = 133u, blue = 23u), rgbHex = "D08517", alpha = 0.22f), | |
) | |
@Test | |
fun testGetColorSet() { | |
dataSet.forEach { | |
val color = it.first.toHtmlColor() | |
assertEquals( | |
message = color.toString(), | |
expected = it.second, | |
actual = color | |
) | |
} | |
} | |
} | |
fun Color.toHtmlColor(): HtmlColor { | |
val argb = toArgb().toUInt() | |
val a = (argb shr 24) and 0xFFu | |
val r = (argb shr 16) and 0xFFu | |
val g = (argb shr 8) and 0xFFu | |
val b = argb and 0xFFu | |
val rgb = HtmlRGB( | |
red = r.toUByte(), | |
green = g.toUByte(), | |
blue = b.toUByte(), | |
) | |
return HtmlColor( | |
rgbHex = rgb.toHex(), | |
rgb = rgb, | |
alpha = ((a.toFloat() / 255.0f) * 100).roundToInt() / 100f | |
) | |
} | |
data class HtmlColor( | |
val rgbHex: String, | |
val rgb: HtmlRGB, | |
val alpha: Float = 0f | |
) | |
data class HtmlRGB( | |
val red: UByte, | |
val green: UByte, | |
val blue: UByte, | |
) | |
fun HtmlRGB.toHex(): String { | |
return hex(byteArrayOf(this.red.toByte(), this.green.toByte(), this.blue.toByte())) | |
} | |
private val digits = "0123456789ABCDEF".toCharArray() | |
/** | |
* Encode bytes to HEX string. | |
* | |
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | |
*/ | |
fun hex(bytes: ByteArray): String { | |
val result = CharArray(bytes.size * 2) | |
var resultIndex = 0 | |
val digits = digits | |
for (index in 0 until bytes.size) { | |
val b = bytes[index].toInt() and 0xff | |
result[resultIndex++] = digits[b shr 4] | |
result[resultIndex++] = digits[b and 0x0f] | |
} | |
return result.concatToString() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment