diff --git a/src/main/java/at/hannibal2/skyhanni/utils/SpecialColour.kt b/src/main/java/at/hannibal2/skyhanni/utils/SpecialColour.kt index 010e67e4d7c0..ca990a5fad47 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/SpecialColour.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/SpecialColour.kt @@ -1,72 +1,70 @@ -package at.hannibal2.skyhanni.utils; +package at.hannibal2.skyhanni.utils -import java.awt.Color; +import java.awt.Color /** * Taken from NotEnoughUpdates */ -public class SpecialColour { +object SpecialColour { - private static final int RADIX = 10; + private const val MIN_CHROMA_SECS = 1 + private const val MAX_CHROMA_SECS = 60 + private const val RADIX = 10 - private static int[] decompose(String csv) { - String[] split = csv.split(":"); + var startTime: Long = -1 - int[] arr = new int[split.length]; + fun specialToChromaRGB(special: String): Int { + if (startTime < 0) startTime = System.currentTimeMillis() - for (int i = 0; i < split.length; i++) { - arr[i] = Integer.parseInt(split[split.length - 1 - i], RADIX); - } - return arr; - } + val d = decomposeColourString(special) + val chr = d[4] + val a = d[3] + val r = d[2] + val g = d[1] + val b = d[0] - public static int getSpeed(String special) { - return decompose(special)[4]; - } + val hsv = Color.RGBtoHSB(r, g, b, null) - public static float getSecondsForSpeed(int speed) { - return (255 - speed) / 254f * (MAX_CHROMA_SECS - MIN_CHROMA_SECS) + MIN_CHROMA_SECS; - } - - private static final int MIN_CHROMA_SECS = 1; - private static final int MAX_CHROMA_SECS = 60; - - public static long startTime = -1; + if (chr > 0) { + val seconds = getSecondsForSpeed(chr) + hsv[0] += (System.currentTimeMillis() - startTime) / 1000f / seconds + hsv[0] %= 1f + if (hsv[0] < 0) hsv[0] += 1f + } - @Deprecated // use String.toChromaColor() - public static int specialToChromaRGB(String special) { - if (startTime < 0) startTime = System.currentTimeMillis(); + return (a and 0xFF) shl 24 or (Color.HSBtoRGB(hsv[0], hsv[1], hsv[2]) and 0x00FFFFFF) + } - int[] d = decompose(special); - int chr = d[4]; - int a = d[3]; - int r = d[2]; - int g = d[1]; - int b = d[0]; + private fun decomposeColourString(colourString: String): IntArray { + val split = colourString.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() - float[] hsv = Color.RGBtoHSB(r, g, b, null); + val arr = IntArray(split.size) - if (chr > 0) { - float seconds = getSecondsForSpeed(chr); - hsv[0] += (System.currentTimeMillis() - startTime) / 1000f / seconds; - hsv[0] %= 1; - if (hsv[0] < 0) hsv[0] += 1; + for (i in split.indices) { + arr[i] = split[split.size - 1 - i].toInt(RADIX) } + return arr + } + + fun getSpeed(special: String): Int { + return decomposeColourString(special)[4] + } - return (a & 0xFF) << 24 | (Color.HSBtoRGB(hsv[0], hsv[1], hsv[2]) & 0x00FFFFFF); + private fun getSecondsForSpeed(speed: Int): Float { + return (255 - speed) / 254f * (MAX_CHROMA_SECS - MIN_CHROMA_SECS) + MIN_CHROMA_SECS } - public static int rotateHue(int argb, int degrees) { - int a = (argb >> 24) & 0xFF; - int r = (argb >> 16) & 0xFF; - int g = (argb >> 8) & 0xFF; - int b = (argb) & 0xFF; + fun rotateHue(argb: Int, degrees: Int): Int { + val a = (argb shr 24) and 0xFF + val r = (argb shr 16) and 0xFF + val g = (argb shr 8) and 0xFF + val b = (argb) and 0xFF - float[] hsv = Color.RGBtoHSB(r, g, b, null); + val hsv = Color.RGBtoHSB(r, g, b, null) - hsv[0] += degrees / 360f; - hsv[0] %= 1; + hsv[0] += degrees / 360f + hsv[0] %= 1f - return (a & 0xFF) << 24 | (Color.HSBtoRGB(hsv[0], hsv[1], hsv[2]) & 0x00FFFFFF); + return (a and 0xFF) shl 24 or (Color.HSBtoRGB(hsv[0], hsv[1], hsv[2]) and 0x00FFFFFF) } }