X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fmisc%2Futils%2FRenderingUtils.kt;h=8976c9963cd8935d64a373ee54db088513537662;hb=ca4dfc9c8252d4222f778db27e7505909420da39;hp=24813acc853fe54ef5421c046a55c48cfede0b39;hpb=f34df6e5eb1052467333f1461c67e75b21eb0d95;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavedroid/misc/utils/RenderingUtils.kt b/core/src/ru/deadsoftware/cavedroid/misc/utils/RenderingUtils.kt index 24813ac..8976c99 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/utils/RenderingUtils.kt +++ b/core/src/ru/deadsoftware/cavedroid/misc/utils/RenderingUtils.kt @@ -5,7 +5,6 @@ import com.badlogic.gdx.graphics.g2d.GlyphLayout import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.math.Rectangle import ru.deadsoftware.cavedroid.misc.Assets -import java.awt.Color as JavaColor private fun Rectangle.shifted(shift: Float) = Rectangle(x + shift, y, width, height) @@ -52,7 +51,21 @@ fun SpriteBatch.drawString(str: String, x: Float, y: Float, color: Color = Color return Assets.minecraftFont.draw(this, str, x, y) } +/** + * Parses hex color string into [Color] + * Format is strictly #FFFFFF + */ fun colorFromHexString(hex: String): Color { - val rgba = (JavaColor.decode(hex).rgb shl 8) or (0xFF) + if (hex[0] != '#' || hex.length != 7) { + return Color.WHITE + } + + var rgba = try { + hex.substring(1).toInt(16) + } catch (e: NumberFormatException) { + 0xffffff + } + + rgba = (rgba shl 8) or 0xFF return Color(rgba) }