[orx-palette] Add ColorBrewer2, demos, update README (#363)

This commit is contained in:
Abe Pazos
2025-09-02 06:38:30 +02:00
committed by GitHub
parent af4c2b20d6
commit d1e5ba634d
14 changed files with 3057 additions and 66 deletions

View File

@@ -1,12 +1,44 @@
# orx-palette
Provides hundreds of color palettes.
Collections of color palettes and tools for interacting with them.
## Usage
Find demos in the demo folder.
## ColorBrewer2
A collection of color palettes based on the research of Dr. Cynthia Brewer. Explore them live at [colorbrewer2.org](https://colorbrewer2.org/).
Each Palette has between 3 and 11 colors.
Use `colorBrewer2Palettes()` to query and obtain a list of `ColorBrewer2Palette` instances.
```kotlin
import org.openrndr.extra.palette.PaletteStudio
// all palettes
val palettes = colorBrewer2Palettes()
// palettes with 5 colors
val palettes = colorBrewer2Palettes(numberOfColors = 5)
// palettes of type Sequential
val palettes = colorBrewer2Palettes(palettetype = ColorBrewer2Type.Sequential)
```
Once we have some palettes, we can pick one and use its colors:
```kotlin
palettes.first().colors.forEachIndexed { i, color ->
drawer.fill = color
drawer.circle(drawer.bounds.center, 300.0 - i * 40.0)
}
```
## Palette Studio
A class to load palette collections from JSON files, load random palettes and sort colors. JVM only.
### Usage
```kotlin
val paletteStudio = PaletteStudio(
loadDefault = true, // Loads the first collection of palettes. [default -> true]
sortBy = PaletteStudio.SortBy.DARKEST, // Sorts the colors by luminance. [default -> PaletteStudio.SortBy.NO_SORTING]
@@ -28,13 +60,13 @@ paletteStudio.randomPalette()
paletteStudio.randomize()
// changes the collection of palettes
paletteStudio.changeCollection(PaletteStudio.Collections.TWO)
paletteStudio.loadCollection(PaletteStudio.Collections.TWO)
// load your own from a JSON file with a structure of Array<Array<String>>
paletteStudio.loadExternal("data/palette-autumn.json")
```
### Keybindings
#### Keybindings
Keybindings for getting a random palette (`l`) and randomizing (`k`) one can be set easily by declaring inside the `program`:
```kotlin
@@ -42,65 +74,29 @@ val paletteStudio = PaletteStudio()
extend(paletteStudio)
```
<!-- __demos__ -->
## Demos
### DemoColorBrewer2_01
[source code](src/jvmDemo/kotlin/DemoColorBrewer2_01.kt)
## Example
![DemoColorBrewer2_01Kt](https://raw.githubusercontent.com/openrndr/orx/media/orx-palette/images/DemoColorBrewer2_01Kt.png)
```kotlin
fun main() = application {
configure {
title = "Palette"
width = 720
height = 720
}
program {
val colors = mutableListOf<ColorRGBa>()
### DemoColorBrewer2_02
[source code](src/jvmDemo/kotlin/DemoColorBrewer2_02.kt)
fun fillColors() {
for (n in 0..36) {
when(n) {
12 -> paletteStudio.changeCollection(PaletteStudio.Collections.TWO)
24 -> paletteStudio.changeCollection(PaletteStudio.Collections.THREE)
}
![DemoColorBrewer2_02Kt](https://raw.githubusercontent.com/openrndr/orx/media/orx-palette/images/DemoColorBrewer2_02Kt.png)
val color = Random.pick(paletteStudio.colors!!)
### DemoColorBrewer2_03
[source code](src/jvmDemo/kotlin/DemoColorBrewer2_03.kt)
colors.add(color)
}
}
![DemoColorBrewer2_03Kt](https://raw.githubusercontent.com/openrndr/orx/media/orx-palette/images/DemoColorBrewer2_03Kt.png)
keyboard.keyDown.listen {
if (it.name == "c") {
colors.clear()
fillColors()
}
}
### DemoPaletteStudio01
[source code](src/jvmDemo/kotlin/DemoPaletteStudio01.kt)
fillColors()
![DemoPaletteStudio01Kt](https://raw.githubusercontent.com/openrndr/orx/media/orx-palette/images/DemoPaletteStudio01Kt.png)
extend() {
drawer.background(paletteStudio.background)
### DemoPaletteStudio02
[source code](src/jvmDemo/kotlin/DemoPaletteStudio02.kt)
val size = 120.0
val radius = size / 2.0
for (x in 0 until 6) {
for (y in 0 until 6) {
val index = x + y * 6
val color = colors[index]
val x = size * x
val y = size * y
drawer.fill = color
drawer.stroke = color
if (index <= 11 || index > 23) {
drawer.circle(x + radius, y + radius, radius)
} else {
drawer.rectangle(x, y, size, size)
}
}
}
}
}
}
```
![DemoPaletteStudio02Kt](https://raw.githubusercontent.com/openrndr/orx/media/orx-palette/images/DemoPaletteStudio02Kt.png)