@@ -2,6 +2,7 @@ package io.github.notenoughupdates.moulconfig
22
33import com.google.gson.annotations.Expose
44import java.awt.Color
5+ import kotlin.math.abs
56
67@Suppress(" DeprecatedCallableAddReplaceWith" , " DEPRECATION" )
78data class ChromaColour (
@@ -32,20 +33,40 @@ data class ChromaColour(
3233 @Expose
3334 val alpha : Int ,
3435) {
35- private val cachedRGB: Int = (Color .HSBtoRGB (hue, saturation, brightness) and 0x00FFFFFF ) or (alpha shl 24 )
36+
37+ private fun evaluateColourWithShift (hueShift : Double ): Int {
38+ if (abs(cachedRGBHueOffset - hueShift) < 1 / 360.0 )return cachedRGB
39+ val effectiveHue = ((hue.toDouble() + hueShift)% 1 ).toFloat()
40+ val ret = (Color .HSBtoRGB (effectiveHue, saturation, brightness) and 0x00FFFFFF ) or (alpha shl 24 )
41+ cachedRGBHueOffset = hueShift
42+ cachedRGB = ret
43+ return ret
44+ }
45+
46+ /* *
47+ * The value of [evaluateColourWithShift] at [cachedRGBHueOffset]
48+ */
49+ @Transient
50+ private var cachedRGB: Int = 0
51+
52+ /* *
53+ * The last queried value of [evaluateColourWithShift].
54+ */
55+ @Transient
56+ private var cachedRGBHueOffset: Double = Double .NaN
3657
3758 /* *
3859 * @param offset offset the colour by a hue amount.
3960 * @return the colour, at the current time if this is a chrome colour
4061 */
4162 fun getEffectiveColourRGB (offset : Float ): Int {
42- var effectiveHue = if (timeForFullRotationInMillis > 0 ) {
63+ var effectiveHueOffset = if (timeForFullRotationInMillis > 0 ) {
4364 System .currentTimeMillis() / timeForFullRotationInMillis.toDouble()
4465 } else {
45- hue.toDouble()
66+ . 0
4667 }
47- effectiveHue + = offset.toDouble()
48- return ( Color . HSBtoRGB ((effectiveHue % 1 ).toFloat(), saturation, brightness) and 0x00FFFFFF ) or (alpha shl 24 )
68+ effectiveHueOffset + = offset
69+ return evaluateColourWithShift(effectiveHueOffset )
4970 }
5071 /* *
5172 * @param offset offset the colour by a hue amount.
@@ -60,9 +81,9 @@ data class ChromaColour(
6081 * @return the colour, at the current time if this is a chrome colour
6182 */
6283 fun getEffectiveColourWithTimeOffsetRGB (offset : Int ): Int {
63- if (timeForFullRotationInMillis == 0 ) return cachedRGB
84+ if (timeForFullRotationInMillis == 0 ) return evaluateColourWithShift(. 0 )
6485 val effectiveHue = (System .currentTimeMillis() + offset) / timeForFullRotationInMillis.toDouble()
65- return ( Color . HSBtoRGB (( effectiveHue % 1 ).toFloat(), saturation, brightness) and 0x00FFFFFF ) or (alpha shl 24 )
86+ return evaluateColourWithShift( effectiveHue)
6687 }
6788
6889 /* *
0 commit comments