@@ -5,7 +5,14 @@ import java.io.File
55import java.io.IOException
66import java.time.Instant
77import javax.imageio.ImageIO
8- import kotlin.math.max
8+ import kotlin.math.abs
9+ import kotlin.math.pow
10+ import kotlin.math.sqrt
11+
12+ // Used in calculating the difference between two colours.
13+ // A value of 1 results in colour values of 0 vs 1 being as different as 254 vs 255.
14+ // Lower values will give more weight to changes in darker colours.
15+ private const val COLOUR_CHANGE_LINEARITY = 0.75
916
1017class ImageThresholdCache (private val dynmapExport : DynmapExport ) {
1118 /* *
@@ -69,15 +76,19 @@ private fun nPixelsChanged(from: BufferedImage, to: BufferedImage, colourChangeT
6976 * @param to the second colour
7077 * @return the colour difference on the scale `[0,1]`
7178 */
72- private fun colourDifference (from : RGB , to : RGB ): Double =
73- (1 - minOf(
74- from.red() / to.red(), to.red() / from.red(),
75- from.green() / to.green(), to.green() / from.green(),
76- from.blue() / to.blue(), to.blue() / from.blue(),
77- )).toDouble() / 0xFF
79+ fun colourDifference (from : RGB , to : RGB ): Double =
80+ maxOf(
81+ channelDifference(from.red(), to.red()),
82+ channelDifference(from.green(), to.green()),
83+ channelDifference(from.blue(), to.blue()),
84+ )
85+
86+ private val lut = DoubleArray (256 ) { (it.toDouble() / 0xFF ).pow(COLOUR_CHANGE_LINEARITY ) }
87+
88+ private fun channelDifference (from : Int , to : Int ): Double = abs(lut[from] - lut[to])
7889
7990typealias RGB = Int
8091
81- private fun RGB.red (): Int = max( 1 , ( this ushr 16 ) and 0xFF )
82- private fun RGB.green (): Int = max( 1 , ( this ushr 8 ) and 0xFF )
83- private fun RGB.blue (): Int = max( 1 , this and 0xFF )
92+ private fun RGB.red (): Int = ( this ushr 16 ) and 0xFF
93+ private fun RGB.green (): Int = ( this ushr 8 ) and 0xFF
94+ private fun RGB.blue (): Int = this and 0xFF
0 commit comments