From a9a12811c42a85985179bc29db81cfecd29e2115 Mon Sep 17 00:00:00 2001 From: Ricardo Matias Date: Thu, 11 Mar 2021 16:20:49 +0100 Subject: [PATCH] Add Random push/pop + palette colors add via hex strings (#179) --- orx-noise/src/main/kotlin/Random.kt | 34 +++++++++++++++----- orx-palette/src/main/kotlin/PaletteStudio.kt | 18 ++++++++--- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/orx-noise/src/main/kotlin/Random.kt b/orx-noise/src/main/kotlin/Random.kt index 96de7e8f..558d1c23 100644 --- a/orx-noise/src/main/kotlin/Random.kt +++ b/orx-noise/src/main/kotlin/Random.kt @@ -11,6 +11,7 @@ import kotlin.math.sqrt import org.openrndr.extra.noise.fbm as orxFbm import kotlin.random.Random as DefaultRandom +private data class RandomState(var seed: String, var rng: DefaultRandom) /** * Deterministic Random using a seed to guarantee the same random values between iterations */ @@ -21,6 +22,8 @@ object Random { private var nextGaussian: Double = 0.0 private var hasNextGaussian = false + private lateinit var state: RandomState + enum class Fractal { FBM, BILLOW, RIGID } @@ -37,6 +40,8 @@ object Random { init { rnd = newRandomGenerator(seed) + + state = RandomState(seed, rnd) } private fun newRandomGenerator(newSeed: String): DefaultRandom { @@ -62,18 +67,32 @@ object Random { * * @param fn */ - fun unseeded(fn: Random.() -> Unit) { - val state = rnd - val currentSeed = seed + fun unseeded(fn: () -> Unit) { + push() + + fn() + + pop() + } + + /** + * Use this when you want to get non-deterministic values aka random every call + * Follow it by calling .pop + */ + fun push() { + state = RandomState(seed, rnd) rnd = DefaultRandom + } - this.fn() - + /** + * Use this to go back to seeded state + */ + fun pop() { resetState() - seed = currentSeed - rnd = state + seed = state.seed + rnd = state.rng } /** @@ -366,4 +385,3 @@ object Random { return rect.position(vector2(0.0, 1.0)) } } - diff --git a/orx-palette/src/main/kotlin/PaletteStudio.kt b/orx-palette/src/main/kotlin/PaletteStudio.kt index 743fc3d0..cff72210 100644 --- a/orx-palette/src/main/kotlin/PaletteStudio.kt +++ b/orx-palette/src/main/kotlin/PaletteStudio.kt @@ -102,7 +102,7 @@ class PaletteStudio( private var onChangeListener = {} - private fun loadCollection(newCollection: Collections) { + fun loadCollection(newCollection: Collections) { val collectionPath: URL = collectionsResource.getValue(newCollection) palettes = mutableListOf() @@ -177,6 +177,7 @@ class PaletteStudio( onChangeListener = fn } + @JvmName("addColorRGBaList") fun add(newPalette: List) { palette = createPalette(newPalette) @@ -184,6 +185,15 @@ class PaletteStudio( paletteIndex = palettes.lastIndex } + @JvmName("addHexList") + fun add(hexColors: List) { + val newPalette = hexColors.map { ColorRGBa.fromHex(it) } + palette = createPalette(newPalette) + + palettes.add(newPalette) + paletteIndex = palettes.lastIndex + } + fun loadExternal(filePath: String) { palettes = mutableListOf() @@ -222,8 +232,8 @@ class PaletteStudio( palette = createPalette(colors) } - fun changeCollection(newCollection: Collections) { - loadCollection(newCollection) + fun newCollection() { + palettes.clear() } private fun registerKeybindings(keyboard: Keyboard) { @@ -249,4 +259,4 @@ class PaletteStudio( override fun setup(program: Program) { registerKeybindings(program.keyboard) } -} \ No newline at end of file +}