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 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))
}
}

View File

@@ -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<ColorRGBa>) {
palette = createPalette(newPalette)
@@ -184,6 +185,15 @@ class PaletteStudio(
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) {
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)
}
}
}