DEADSOFTWARE

Update version
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / misc / utils / RenderingUtils.kt
index d197229578bfb8a92e989eb228b05a2bf05f0c69..8976c9963cd8935d64a373ee54db088513537662 100644 (file)
@@ -50,3 +50,22 @@ fun SpriteBatch.drawString(str: String, x: Float, y: Float, color: Color = Color
     Assets.minecraftFont.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 {
+    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)
+}