Skip to content

Commit 3f2f1af

Browse files
fix: use a sync pickColors
1 parent 1c3e201 commit 3f2f1af

File tree

7 files changed

+60
-59
lines changed

7 files changed

+60
-59
lines changed

1d/cca_1d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ColorObject } from "../types/ColorObject"
22
import { nextCellColorId } from "../utils/nextCellColorId"
3-
import { pickRandomColors } from "../utils/pickColors"
3+
import { pickColors } from "../utils/pickColors"
44
import { setupCanvas } from "../utils/setupCanvas"
55

66
export class CCA1D {
@@ -22,7 +22,7 @@ export class CCA1D {
2222
this.canvasEl = canvasEl
2323
this.width = width
2424
this.height = height
25-
this.colors = pickRandomColors(colorsCount)
25+
this.colors = pickColors(colorsCount)
2626
this.state = []
2727
this.ctx = setupCanvas(this.canvasEl, width, height)
2828
this.setRandomState()

2d/automaton2d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ColorObject } from "../types/ColorObject"
2-
import { pickRandomColors } from "../utils/pickColors"
2+
import { pickColors } from "../utils/pickColors"
33
import { randomInt } from "../utils/randomInt"
44
import { setupCanvas } from "../utils/setupCanvas"
55

@@ -30,7 +30,7 @@ export class Automaton2D {
3030
this.rowsCount = this.height / resolution
3131
this.colsCount = this.width / resolution
3232
this.colorsCount = colorsCount
33-
this.colors = pickRandomColors(colorsCount)
33+
this.colors = pickColors(colorsCount)
3434
this.state = []
3535
this.ctx = setupCanvas(this.canvasEl, this.width, this.height)
3636
}

2d/conway/conway.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ColorObject } from "../../types/ColorObject"
2-
import { pickRandomColors } from "../../utils/pickColors"
2+
import { pickColors } from "../../utils/pickColors"
33
import { Automaton2D } from "../automaton2d"
44

55
export class ConwayAutomaton extends Automaton2D {
@@ -8,7 +8,7 @@ export class ConwayAutomaton extends Automaton2D {
88

99
constructor(...args: ConstructorParameters<typeof Automaton2D>) {
1010
super(...args)
11-
this.colors = pickRandomColors(this.colorsCount)
11+
this.colors = pickColors(this.colorsCount)
1212
this.colorOff = this.colors[0]
1313
this.colorOn = this.colors[1]
1414

2d/langton/langton.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { pickRandomColors } from "../../utils/pickColors"
1+
import { pickColors } from "../../utils/pickColors"
22
import { Automaton2D } from "../automaton2d"
33

44
export class LangtonAutomaton extends Automaton2D {
@@ -10,7 +10,7 @@ export class LangtonAutomaton extends Automaton2D {
1010
constructor(...args: ConstructorParameters<typeof Automaton2D>) {
1111
super(...args)
1212
this.colorsCount = 2
13-
this.colors = pickRandomColors(this.colorsCount)
13+
this.colors = pickColors(this.colorsCount)
1414

1515
this.setUniformStateAndRender()
1616

3d/cca_3d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as THREE from "three"
22
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls"
33
import type { Cell } from "../types/ Cell"
44
import { nextCellColorId } from "../utils/nextCellColorId"
5-
import { pickRandomColors } from "../utils/pickColors"
5+
import { pickColors } from "../utils/pickColors"
66

77
interface Cell3D extends Cell {
88
mesh?: THREE.Mesh
@@ -53,7 +53,7 @@ export class CCA3D {
5353
this.cellSize = Math.min(width, height) / resolution / 4
5454
this.cellFilling = 1.0
5555
this.threshold = threshold // 4 is good
56-
this.colors = pickRandomColors(colorsCount) // 10 is good
56+
this.colors = pickColors(colorsCount) // 10 is good
5757
this.state = []
5858
this.bufferState = []
5959
this.initialRotationSpeed = 0.004

main.ts

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ import { LangtonAutomaton } from "./2d/langton/langton"
2121
import { QuadLifeAutomaton } from "./2d/quadlife/quadlife"
2222
import { CCA3D } from "./3d/cca_3d"
2323
import type { AutomatonBase } from "./types/Automaton"
24-
import type { Movie } from "./types/MoviePalette"
2524
import type { Settings } from "./types/Settings"
2625
import { slugify } from "./utils/slugify"
2726

27+
interface StoredPalette {
28+
colors: [number, number, number][]
29+
}
30+
31+
const moviePalettes = new Map<string, StoredPalette>()
32+
2833
let pane: Pane
2934
let settings: Settings
3035
let automaton: AutomatonBase
@@ -353,13 +358,24 @@ window.onload = () => {
353358
fetch(`${MOVIES_PALETTES_API}/movies`)
354359
.then((response) => response.json())
355360
.then((data) => {
356-
const options = [
357-
{ text: "Random", value: null },
358-
...data.movies.map((movie) => ({
359-
text: `${movie.title} (${movie.year || "N/A"})`,
360-
value: slugify(movie.title),
361-
})),
362-
]
361+
const options = [{ text: "Random", value: null }]
362+
363+
// Filter movies with palettes and store their colors
364+
data.movies.forEach((movie) => {
365+
if (movie.palettes && movie.palettes.length > 0) {
366+
const slug = slugify(movie.title)
367+
// Store the palette colors
368+
moviePalettes.set(slug, {
369+
colors: movie.palettes[0].colors,
370+
})
371+
// Add to dropdown options
372+
options.push({
373+
text: `${movie.title} (${movie.year || "N/A"})`,
374+
value: slug,
375+
})
376+
}
377+
})
378+
363379
paletteSelector.options = options
364380
})
365381
.catch((error) => {
@@ -383,17 +399,26 @@ const getSettings = (pane: Pane): Settings => {
383399
return settings as Settings
384400
}
385401

386-
const createAutomaton = (
402+
const createAutomaton = async (
387403
canvasEl: HTMLCanvasElement,
388404
width: number,
389405
height: number,
390406
settings: Settings,
391-
): AutomatonBase => {
407+
): Promise<AutomatonBase> => {
392408
const resolution: number = settings.resolution || 5
409+
const paletteColors = settings.palette
410+
? moviePalettes.get(settings.palette)?.colors
411+
: undefined
393412

394413
switch (settings.algo) {
395414
case "cca-1D":
396-
return new CCA1D(canvasEl, width, height, settings.cca1dColorsCount || 4)
415+
return await CCA1D.create(
416+
canvasEl,
417+
width,
418+
height,
419+
settings.cca1dColorsCount || 4,
420+
paletteColors,
421+
)
397422
case "cca-2D":
398423
return new CCA2D(
399424
settings.cca2dThreshold,

utils/pickColors.ts

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import chroma from "chroma-js"
22
import type { ColorObject } from "../types/ColorObject"
3-
import type { MoviePalette } from "../types/MoviePalette"
3+
import type { RGB } from "../types/MoviePalette"
44

55
const MOVIES_PALETTES_API = import.meta.env.VITE_MOVIES_PALETTES_API
66

@@ -37,49 +37,25 @@ export const pickRandomColors = (colorsCount: number): ColorObject[] => {
3737
}))
3838
}
3939

40-
export const pickColors = async (
40+
export const pickColors = (
4141
colorsCount: number,
42-
palette?: string,
43-
): Promise<ColorObject[]> => {
44-
if (!palette) {
45-
return pickRandomColors(colorsCount)
46-
}
47-
48-
try {
49-
// Fetch movie palette
50-
const response = await fetch(`${MOVIES_PALETTES_API}/palettes/${palette}`)
51-
if (!response.ok) {
52-
throw new Error(`HTTP error! status: ${response.status}`)
53-
}
54-
const data = await response.json()
55-
56-
if (!data.palettes?.length) {
57-
console.warn(`No palettes found for movie ${palette}`)
58-
return pickRandomColors(colorsCount)
59-
}
60-
61-
const firstPalette = data.palettes[0]
62-
const paletteColors = firstPalette.colors
63-
console.log(paletteColors.length)
64-
console.log(colorsCount)
65-
66-
// Check if we have enough colors
67-
if (paletteColors.length < colorsCount) {
42+
paletteColors?: RGB[],
43+
): ColorObject[] => {
44+
// If no palette colors provided or not enough colors, use random colors
45+
if (!paletteColors || paletteColors.length < colorsCount) {
46+
if (paletteColors) {
6847
console.warn(
69-
`Not enough colors in palette for ${palette} (needed: ${colorsCount}, got: ${paletteColors.length}). Using random colors instead.`,
48+
`Not enough colors in palette (needed: ${colorsCount}, got: ${paletteColors.length}). Using random colors instead.`,
7049
)
71-
return pickRandomColors(colorsCount)
7250
}
73-
74-
// Use palette colors
75-
return Array.from({ length: colorsCount }, (_, i) => ({
76-
id: i,
77-
colorRgb: paletteColors[i],
78-
}))
79-
} catch (error) {
80-
console.error("Failed to fetch movie palette:", error)
8151
return pickRandomColors(colorsCount)
8252
}
53+
54+
// Use palette colors
55+
return Array.from({ length: colorsCount }, (_, i) => ({
56+
id: i,
57+
colorRgb: paletteColors[i],
58+
}))
8359
}
8460

8561
/*

0 commit comments

Comments
 (0)