From 5223ee0035a047e42e5ebb4b96adfe19c89998d5 Mon Sep 17 00:00:00 2001 From: Ricardo Matias Date: Mon, 13 Jul 2020 10:24:58 +0200 Subject: [PATCH] [orx-palette] Allow background to be randomized (#135) --- orx-palette/src/main/kotlin/PaletteStudio.kt | 49 ++++++++++---------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/orx-palette/src/main/kotlin/PaletteStudio.kt b/orx-palette/src/main/kotlin/PaletteStudio.kt index 833e6e31..59fa7e56 100644 --- a/orx-palette/src/main/kotlin/PaletteStudio.kt +++ b/orx-palette/src/main/kotlin/PaletteStudio.kt @@ -125,6 +125,28 @@ class PaletteStudio( private fun loadFromURL(url: URL): Unit = load(url.readText()) + private fun assemblePalette(clrs: List): Palette { + val background = clrs.first() + val foreground = clrs + .takeLast(clrs.size - 1) + .map { getContrast(background, it) to it } + .maxBy { it.first }!! + .second + + var constraint = clrs.size + var constraint2 = clrs.size + + if (colorCountConstraint > 0 && colorCountConstraint < clrs.size) { + constraint = colorCountConstraint + constraint2 = colorCountConstraint + 1 + } + + val colors1 = clrs.slice(0 until constraint) + val colors2 = clrs.slice(1 until constraint2) + + return Palette(background, foreground, colors1, colors2) + } + private fun createPalette(colors: List): Palette { val sortedColors = when (sortBy) { SortBy.DARKEST -> { @@ -140,25 +162,7 @@ class PaletteStudio( } } - val background = sortedColors.first() - val foreground = sortedColors - .takeLast(sortedColors.size - 1) - .map { getContrast(background, it) to it } - .maxBy { it.first }!! - .second - - var constraint = sortedColors.size - var constraint2 = sortedColors.size - - if (colorCountConstraint > 0 && colorCountConstraint < sortedColors.size) { - constraint = colorCountConstraint - constraint2 = colorCountConstraint + 1 - } - - val colors1 = sortedColors.slice(0 until constraint) - val colors2 = sortedColors.slice(1 until constraint2) - - return Palette(background, foreground, colors1, colors2) + return assemblePalette(sortedColors) } fun onChange(fn: () -> Unit) { @@ -193,12 +197,7 @@ class PaletteStudio( } fun randomize() { - palette = Palette( - background, - foreground, - Random.pick(colors, count = colors.size), - Random.pick(colors2, count = colors2.size) - ) + palette = assemblePalette(colors.shuffled()) } fun randomPalette() {