Skip to content

Commit

Permalink
bugfix: パレット周りのバグをいろいろ修正 (#58)
Browse files Browse the repository at this point in the history
* Palette編集UIから長さを変更したときfillCacheしてなかったので色がおかしくなるバグがまだ起きていたのを修正

* presetから選んだ場合もupdateStoreしておくべきだった

* deps漏れてた

* 数値キーでpreset変えたとき、タブ変えないと値が反映されてなかったのを修正

* mouseXとmouseYとprogressが毎フレーム更新されてしまっていたのを修正

* fill cacheだけだと意味が分からんのでpaletteのserializeしたのを一緒に出力しておく
  • Loading branch information
k-chop authored Jan 9, 2025
1 parent f811cb5 commit edeffeb
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 11 deletions.
9 changes: 5 additions & 4 deletions src/camera/palette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ export const getCurrentPalette = () => currentPalette;
* presetに登録してあるPaletteを選択する
*/
export const changePaletteFromPresets = (index: number) => {
let newPalette = currentPalette;

if (palettePresets[index]) {
currentPalette = palettePresets[index];
} else {
currentPalette = palettePresets[0];
newPalette = palettePresets[index];
}
markNeedsRerender();

setPalette(newPalette);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/color/color-chromajs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export class ChromaJsPalette extends BasePalette {
}

this.buildColors();
this.fillCache();
}

getRGBFromColorIndex(index: number): RGB {
Expand All @@ -37,6 +36,7 @@ export class ChromaJsPalette extends BasePalette {
this.colors = chroma
.scale(this.colorConstructor)
.colors(this.colorLength, null);
this.fillCache();
}

serialize(): string {
Expand Down
2 changes: 1 addition & 1 deletion src/color/color-d3-chromatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export class D3ChromaticPalette extends BasePalette {
this.interpolator = interpolator;

this.buildColors();
this.fillCache();
}

getRGBFromColorIndex(index: number): RGB {
Expand All @@ -70,6 +69,7 @@ export class D3ChromaticPalette extends BasePalette {
this.colors = samples(this.colorLength)
.map((t) => color(this.interpolator(t)))
.filter((v): v is NonNullable<typeof v> => v != null);
this.fillCache();
}

serialize(): string {
Expand Down
2 changes: 1 addition & 1 deletion src/color/color-others.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ export class OthersPalette extends BasePalette {
this.interpolator = interpolator;

this.buildColors();
this.fillCache();
}

buildColors(): void {
this.colors = samples(this.colorLength)
.map((t) => this.interpolator(t))
.filter((v): v is NonNullable<typeof v> => v != null);
this.fillCache();
}

getRGBFromColorIndex(index: number): RGB {
Expand Down
6 changes: 5 additions & 1 deletion src/color/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ export class BasePalette implements Palette {
}

fillCache(): void {
console.log("Fill cache: ", this.colorLength);
console.log(
"Fill palette cache:",
this.colorLength,
`(${this.serialize()})`,
);

for (let i = 0; i < this.colorLength; i++) {
const rgb = this.getRGBFromColorIndex(i);
Expand Down
8 changes: 7 additions & 1 deletion src/store/store.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import BigNumber from "bignumber.js";
import { eventmit } from "eventmit";
import { useEffect, useState } from "react";
import {
Expand All @@ -20,6 +21,11 @@ export const getStore = (key: string) => store[key];

export const updateStore = (key: string, value: any) => {
if (store[key] === value) return;
// BigNumberはeqで比較
if (value instanceof BigNumber && value.eq(store[key])) return;
// progressがobjectなので、totalが同じなら更新しない
if (value != null && value.total != null && value.total === store[key].total)
return;

store[key] = value;

Expand Down Expand Up @@ -55,7 +61,7 @@ export const useStoreValue = <T = any>(key: string) => {
return () => {
event.off(handler);
};
}, []);
}, [key]);

return value as T;
};
14 changes: 12 additions & 2 deletions src/view/right-sidebar/palette-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
} from "@/camera/palette";
import { ValueSlider } from "@/components/slider-wrapper";
import { Slider } from "@/components/ui/slider";
import { getStore, updateStore } from "@/store/store";
import { useState } from "react";
import { getStore, updateStore, useStoreValue } from "@/store/store";
import { useEffect, useState } from "react";

const paletteLengthValues = [
"4",
Expand All @@ -31,6 +31,16 @@ export const PaletteEditor = () => {
getStore("paletteOffset"),
);

// subscribe
const paletteLength = useStoreValue("paletteLength");
useEffect(() => {
setPaletteLengthValue(paletteLength);
}, [paletteLength]);
const paletteOffset = useStoreValue("paletteOffset");
useEffect(() => {
setPaletteOffsetValue(paletteOffset);
}, [paletteOffset]);

return (
<div className="flex max-w-80 flex-col gap-6">
<div>
Expand Down

0 comments on commit edeffeb

Please sign in to comment.