Skip to content

Commit 52712fb

Browse files
committed
fix: using cachedRGB value directly causing incorrect serialization in ChromaColour
1 parent 0e98f95 commit 52712fb

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tasks.withType<Test> {
2+
useJUnitPlatform()
3+
}
4+
dependencies {
5+
val junit5Version = "5.8.1"
6+
"testImplementation"("org.junit.jupiter:junit-jupiter-api:$junit5Version")
7+
"testRuntimeOnly"("org.junit.jupiter:junit-jupiter-engine:$junit5Version")
8+
}

common/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ plugins {
44
id("moulconfig.dokka")
55
`maven-publish`
66
id("moulconfig.base")
7+
id("moulconfig.test")
78
}
89

910
java.toolchain.languageVersion.set(JavaLanguageVersion.of(8))

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.github.notenoughupdates.moulconfig
33
import com.google.gson.annotations.Expose
44
import java.awt.Color
55
import kotlin.math.abs
6+
import kotlin.math.roundToInt
67

78
@Suppress("DeprecatedCallableAddReplaceWith", "DEPRECATION")
89
data class ChromaColour(
@@ -35,8 +36,8 @@ data class ChromaColour(
3536
) {
3637

3738
private fun evaluateColourWithShift(hueShift: Double): Int {
38-
if (abs(cachedRGBHueOffset - hueShift) < 1/360.0)return cachedRGB
39-
val effectiveHue = ((hue.toDouble() + hueShift)%1).toFloat()
39+
if (abs(cachedRGBHueOffset - hueShift) < 1 / 360.0) return cachedRGB
40+
val effectiveHue = ((hue.toDouble() + hueShift) % 1).toFloat()
4041
val ret = (Color.HSBtoRGB(effectiveHue, saturation, brightness) and 0x00FFFFFF) or (alpha shl 24)
4142
cachedRGBHueOffset = hueShift
4243
cachedRGB = ret
@@ -68,6 +69,7 @@ data class ChromaColour(
6869
effectiveHueOffset += offset
6970
return evaluateColourWithShift(effectiveHueOffset)
7071
}
72+
7173
/**
7274
* @param offset offset the colour by a hue amount.
7375
* @return the colour, at the current time if this is a chrome colour
@@ -106,12 +108,12 @@ data class ChromaColour(
106108

107109
@Deprecated("")
108110
fun toLegacyString(): String {
109-
val timeInSeconds = timeForFullRotationInMillis / 1000
110111
val namedSpeed =
111-
if (timeInSeconds == 0) 0 else (255 - (timeInSeconds - MIN_CHROMA_SECS) * 254f / (MAX_CHROMA_SECS - MIN_CHROMA_SECS)).toInt()
112-
val red = cachedRGB shr 16 and 0xFF
113-
val green = cachedRGB shr 8 and 0xFF
114-
val blue = cachedRGB and 0xFF
112+
if (timeForFullRotationInMillis == 0) 0 else getSpeedForMillis(timeForFullRotationInMillis / 1000f)
113+
val rgb = evaluateColourWithShift(.0)
114+
val red = rgb shr 16 and 0xFF
115+
val green = rgb shr 8 and 0xFF
116+
val blue = rgb and 0xFF
115117
return special(namedSpeed, alpha, red, green, blue)
116118
}
117119

@@ -172,6 +174,11 @@ data class ChromaColour(
172174
@Deprecated("")
173175
fun getSecondsForSpeed(speed: Int): Float = (255 - speed) / 254f * (MAX_CHROMA_SECS - MIN_CHROMA_SECS) + MIN_CHROMA_SECS
174176

177+
@Deprecated("")
178+
fun getSpeedForMillis(seconds: Float): Int {
179+
return (255 - ((seconds - MIN_CHROMA_SECS) / (MAX_CHROMA_SECS - MIN_CHROMA_SECS) * 254)).roundToInt()
180+
}
181+
175182
@JvmStatic
176183
@Deprecated("")
177184
fun specialToChromaRGB(special: String): Int {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.github.notenoughupdates.moulconfig
2+
3+
import org.junit.jupiter.api.Assertions
4+
import org.junit.jupiter.api.Disabled
5+
import org.junit.jupiter.api.Test
6+
import java.awt.Color
7+
8+
/**
9+
* Test to verfiy that [ChromaColour.toLegacyString] and [ChromaColour.forLegacyString] both work as expected, serializing the RGB value.
10+
*/
11+
class ChromaColourTest {
12+
fun testWithSpeed(jColor: Color, speed: Int) {
13+
val stringRepr = "$speed:${jColor.alpha}:${jColor.red}:${jColor.green}:${jColor.blue}"
14+
val time = if (speed > 0) (ChromaColour.getSecondsForSpeed(speed) * 1000).toInt() else 0
15+
val fromStatic = ChromaColour.fromRGB(jColor.red, jColor.green, jColor.blue, time, jColor.alpha)
16+
if (speed == 0) // Can't test colour accuracy given that the system time influences this
17+
Assertions.assertEquals(jColor.rgb, fromStatic.getEffectiveColourRGB())
18+
Assertions.assertEquals(stringRepr, fromStatic.toLegacyString())
19+
Assertions.assertEquals(fromStatic, ChromaColour.forLegacyString(stringRepr))
20+
}
21+
22+
@Disabled("This test takes around ~8 seconds to run, we don't need that overhead")
23+
@Test
24+
fun testAllColours() {
25+
yieldAllColours()
26+
.forEach {
27+
testWithSpeed(it, 0)
28+
testWithSpeed(it, 10)
29+
testWithSpeed(it, 255)
30+
}
31+
}
32+
33+
fun yieldAllColours() = (0..0xFFFFFF)
34+
.asSequence()
35+
.map { Color(it) }
36+
}

0 commit comments

Comments
 (0)