Gjallarhorn: Refactor Color Gradient

This commit is contained in:
Kenneth Endfinger
2022-01-08 14:10:30 -05:00
parent 8ea1ea1540
commit 3350034060
2 changed files with 33 additions and 25 deletions

View File

@ -3,26 +3,18 @@ package cloud.kubelet.foundation.gjallarhorn.util
import java.awt.Color import java.awt.Color
import kotlin.math.max import kotlin.math.max
class ColorGradient { class ColorGradient constructor() {
data class ColorPoint( constructor(vararg points: ColorGradientPoint) : this() {
val r: Float, for (point in points) {
val g: Float, addColorPoint(point)
val b: Float, }
val value: Float
) {
fun toColor() = Color(
FloatClamp.ColorRgbComponent.convert(r).toInt(),
FloatClamp.ColorRgbComponent.convert(g).toInt(),
FloatClamp.ColorRgbComponent.convert(b).toInt()
)
} }
private val points = mutableListOf<ColorPoint>() private val points = mutableListOf<ColorGradientPoint>()
fun addColorPoint(red: Float, green: Float, blue: Float, value: Float) { fun addColorPoint(point: ColorGradientPoint) {
val point = ColorPoint(red, green, blue, value)
for (x in 0 until points.size) { for (x in 0 until points.size) {
if (value < points[x].value) { if (point.value < points[x].value) {
points.add(x, point) points.add(x, point)
return return
} }
@ -32,7 +24,7 @@ class ColorGradient {
fun getColorAtValue(value: Float): Color { fun getColorAtValue(value: Float): Color {
if (points.isEmpty()) { if (points.isEmpty()) {
return ColorPoint(0f, 0f, 0f, value).toColor() return ColorGradientPoint(0f, 0f, 0f, value).toColor()
} }
for (x in 0 until points.size) { for (x in 0 until points.size) {
@ -41,7 +33,7 @@ class ColorGradient {
val previous = points[max(0, x - 1)] val previous = points[max(0, x - 1)]
val diff = previous.value - current.value val diff = previous.value - current.value
val fractionBetween = if (diff == 0f) 0f else (value - current.value) / diff val fractionBetween = if (diff == 0f) 0f else (value - current.value) / diff
return ColorPoint( return ColorGradientPoint(
(previous.r - current.r) * fractionBetween + current.r, (previous.r - current.r) * fractionBetween + current.r,
(previous.g - current.g) * fractionBetween + current.g, (previous.g - current.g) * fractionBetween + current.g,
(previous.b - current.b) * fractionBetween + current.b, (previous.b - current.b) * fractionBetween + current.b,
@ -54,12 +46,12 @@ class ColorGradient {
} }
companion object { companion object {
val HeatMap = ColorGradient().apply { val HeatMap = ColorGradient(
addColorPoint(0f, 0f, 1f, 0.0f) ColorGradientPoint(0f, 0f, 1f, 0.0f),
addColorPoint(0f, 1f, 1f, 0.25f) ColorGradientPoint(0f, 1f, 1f, 0.25f),
addColorPoint(0f, 1f, 0f, 0.5f) ColorGradientPoint(0f, 1f, 0f, 0.5f),
addColorPoint(1f, 1f, 0f, 0.75f) ColorGradientPoint(1f, 1f, 0f, 0.75f),
addColorPoint(1f, 0f, 0f, 1.0f) ColorGradientPoint(1f, 0f, 0f, 1.0f)
} )
} }
} }

View File

@ -0,0 +1,16 @@
package cloud.kubelet.foundation.gjallarhorn.util
import java.awt.Color
data class ColorGradientPoint(
val r: Float,
val g: Float,
val b: Float,
val value: Float
) {
fun toColor() = Color(
FloatClamp.ColorRgbComponent.convert(r).toInt(),
FloatClamp.ColorRgbComponent.convert(g).toInt(),
FloatClamp.ColorRgbComponent.convert(b).toInt()
)
}