Skip to content

Commit 482b8ba

Browse files
committed
refactor: Add colour test in modern and extract some more common functionality
1 parent ff5eb89 commit 482b8ba

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

common/src/main/java/io/github/notenoughupdates/moulconfig/ChromaColour.kt

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.github.notenoughupdates.moulconfig
22

33
import com.google.gson.annotations.Expose
44
import java.awt.Color
5+
import kotlin.math.abs
56

67
@Suppress("DeprecatedCallableAddReplaceWith", "DEPRECATION")
78
data 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
/**

modern/templates/kotlin/test/TestCategoryA.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.notenoughupdates.moulconfig.test
22

33
import com.google.gson.annotations.Expose
4+
import io.github.notenoughupdates.moulconfig.ChromaColour
45
import io.github.notenoughupdates.moulconfig.annotations.*
56
import io.github.notenoughupdates.moulconfig.observer.Property
67
import org.lwjgl.glfw.GLFW
@@ -99,6 +100,10 @@ class TestCategoryA {
99100
}
100101
}
101102

103+
@ConfigOption(name = "Colour Test", desc = "Test a colour editor")
104+
@ConfigEditorColour
105+
var colour = ChromaColour(0F, 1f, 1f, 0, 0xFF)
106+
102107
@Expose
103108
@ConfigOption(name = "Keybind", desc = "The Number One")
104109
@ConfigEditorKeybind(defaultKey = GLFW.GLFW_KEY_1)

0 commit comments

Comments
 (0)