Skip to content

Commit

Permalink
Fix Palettes
Browse files Browse the repository at this point in the history
  • Loading branch information
igr committed Mar 19, 2024
1 parent ef5d10d commit 88f37f9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
12 changes: 10 additions & 2 deletions gartwork/src/main/kotlin/dev/oblac/gart/gfx/Palettes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,22 @@ object Palettes {
return Palette(colors)
}

/**
* Grows (expands) palette by adding gradients between each pair of colors.
*/
fun gradient(palette: Palette, steps: Int): Palette {
val delta = steps / palette.size
val delta = steps.toFloat() / (palette.size - 1)
var i = 0

var gradient = Palette()
var colorCounter = 0f
while (i < palette.size - 1) {
gradient += gradient(palette[i], palette[i + 1], delta)
// since delta is float, some gradients will have more steps than others
val gradientSteps = (colorCounter + delta).toInt() - colorCounter.toInt()

gradient += gradient(palette[i], palette[i + 1], gradientSteps)
i++
colorCounter += delta
}

return gradient
Expand Down
35 changes: 35 additions & 0 deletions gartwork/src/main/kotlin/dev/oblac/gart/gfx/PointsTrail.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dev.oblac.gart.gfx

import dev.oblac.gart.skia.Point

/**
* Just a queue of points.
*/
class PointsTrail(val size: Int) {
private val points = ArrayDeque<Point>(size)

/**
* Adds a point to the trail.
*/
fun add(p: Point) {
if (points.size == size) {
points.removeFirst()
}
points.add(p)
}

/**
* Iterates over the points in the trail.
*/
fun forEach(action: (Point) -> Unit) {
points.forEach(action)
}

/**
* Iterates over the points in the trail in reverse order.
*/
fun forEachReverse(action: (Point) -> Unit) {
points.asReversed().forEach(action)
}

}
12 changes: 12 additions & 0 deletions gartwork/src/main/kotlin/dev/oblac/gart/gfx/path.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ fun pathOf(first: Point, vararg points: Point): Path {
return path
}

fun pathOf(list: List<Point>): Path {
val path = Path()
list.forEachIndexed { index, point ->
if (index == 0) {
path.moveTo(point)
} else {
path.lineTo(point)
}
}
return path
}

fun closedPathOf(first: Point, vararg points: Point): Path {
val path = Path().moveTo(first)
points.forEach { path.lineTo(it) }
Expand Down
14 changes: 14 additions & 0 deletions gartwork/src/test/kotlin/dev/oblac/gart/gfx/PalettesTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.oblac.gart.gfx

import kotlin.test.Test
import kotlin.test.assertEquals

class PalettesTest {

@Test
fun testPaletteToGradient() {
val p = Palettes.cool27
val g = Palettes.gradient(p, 256)
assertEquals(256, g.size)
}
}

0 comments on commit 88f37f9

Please sign in to comment.