Add Random push/pop + palette colors add via hex strings (#179)

This commit is contained in:
Ricardo Matias
2021-03-11 16:20:49 +01:00
committed by GitHub
parent c43e6c633e
commit a9a12811c4
2 changed files with 40 additions and 12 deletions

View File

@@ -11,6 +11,7 @@ import kotlin.math.sqrt
import org.openrndr.extra.noise.fbm as orxFbm import org.openrndr.extra.noise.fbm as orxFbm
import kotlin.random.Random as DefaultRandom 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 * 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 nextGaussian: Double = 0.0
private var hasNextGaussian = false private var hasNextGaussian = false
private lateinit var state: RandomState
enum class Fractal { enum class Fractal {
FBM, BILLOW, RIGID FBM, BILLOW, RIGID
} }
@@ -37,6 +40,8 @@ object Random {
init { init {
rnd = newRandomGenerator(seed) rnd = newRandomGenerator(seed)
state = RandomState(seed, rnd)
} }
private fun newRandomGenerator(newSeed: String): DefaultRandom { private fun newRandomGenerator(newSeed: String): DefaultRandom {
@@ -62,18 +67,32 @@ object Random {
* *
* @param fn * @param fn
*/ */
fun unseeded(fn: Random.() -> Unit) { fun unseeded(fn: () -> Unit) {
val state = rnd push()
val currentSeed = seed
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 rnd = DefaultRandom
}
this.fn() /**
* Use this to go back to seeded state
*/
fun pop() {
resetState() resetState()
seed = currentSeed seed = state.seed
rnd = state rnd = state.rng
} }
/** /**
@@ -366,4 +385,3 @@ object Random {
return rect.position(vector2(0.0, 1.0)) return rect.position(vector2(0.0, 1.0))
} }
} }

View File

@@ -102,7 +102,7 @@ class PaletteStudio(
private var onChangeListener = {} private var onChangeListener = {}
private fun loadCollection(newCollection: Collections) { fun loadCollection(newCollection: Collections) {
val collectionPath: URL = collectionsResource.getValue(newCollection) val collectionPath: URL = collectionsResource.getValue(newCollection)
palettes = mutableListOf() palettes = mutableListOf()
@@ -177,6 +177,7 @@ class PaletteStudio(
onChangeListener = fn onChangeListener = fn
} }
@JvmName("addColorRGBaList")
fun add(newPalette: List<ColorRGBa>) { fun add(newPalette: List<ColorRGBa>) {
palette = createPalette(newPalette) palette = createPalette(newPalette)
@@ -184,6 +185,15 @@ class PaletteStudio(
paletteIndex = palettes.lastIndex paletteIndex = palettes.lastIndex
} }
@JvmName("addHexList")
fun add(hexColors: List<String>) {
val newPalette = hexColors.map { ColorRGBa.fromHex(it) }
palette = createPalette(newPalette)
palettes.add(newPalette)
paletteIndex = palettes.lastIndex
}
fun loadExternal(filePath: String) { fun loadExternal(filePath: String) {
palettes = mutableListOf() palettes = mutableListOf()
@@ -222,8 +232,8 @@ class PaletteStudio(
palette = createPalette(colors) palette = createPalette(colors)
} }
fun changeCollection(newCollection: Collections) { fun newCollection() {
loadCollection(newCollection) palettes.clear()
} }
private fun registerKeybindings(keyboard: Keyboard) { private fun registerKeybindings(keyboard: Keyboard) {
@@ -249,4 +259,4 @@ class PaletteStudio(
override fun setup(program: Program) { override fun setup(program: Program) {
registerKeybindings(program.keyboard) registerKeybindings(program.keyboard)
} }
} }