diff --git a/src/components/KeyboardHooks.svelte b/src/components/KeyboardHooks.svelte
index 8b9af563..b61dfdec 100644
--- a/src/components/KeyboardHooks.svelte
+++ b/src/components/KeyboardHooks.svelte
@@ -4,7 +4,8 @@
import focusStore from "../stores/focus-store";
$: focusedSet = new Set($focusStore.focusedColors);
$: copiedData = [] as Color[];
- $: colorSpace = $colorStore.currentPal.colorSpace;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: colorSpace = currentPal.colorSpace;
$: config = colorPickerConfig[colorSpace];
$: xStep = config.xStep;
$: yStep = config.yStep;
@@ -44,7 +45,7 @@
if (key === "backspace" || key === "delete") {
e.preventDefault();
const focusedSet = new Set($focusStore.focusedColors);
- const newColors = $colorStore.currentPal.colors.filter(
+ const newColors = currentPal.colors.filter(
(_, idx) => !focusedSet.has(idx)
);
colorStore.setCurrentPalColors(newColors);
@@ -63,7 +64,7 @@
}
e.preventDefault();
const focusSet = new Set($focusStore.focusedColors);
- const newColors = $colorStore.currentPal.colors.map((color, idx) => {
+ const newColors = currentPal.colors.map((color, idx) => {
if (!focusSet.has(idx)) {
return color;
}
@@ -91,25 +92,15 @@
// SAVE BY DUPLICATING PALETTE
if (key === "s" && metaKey) {
e.preventDefault();
- const newPal = {
- ...$colorStore.currentPal,
- name: `${$colorStore.currentPal.name} copy`,
- colors: [...$colorStore.currentPal.colors],
- };
- colorStore.createNewPal(newPal);
+ colorStore.duplicatePal($colorStore.currentPal);
}
// COPY PASTE
if (key === "c" && metaKey && $focusStore.focusedColors.length) {
- copiedData = $colorStore.currentPal.colors.filter((_, idx) =>
- focusedSet.has(idx)
- );
+ copiedData = currentPal.colors.filter((_, idx) => focusedSet.has(idx));
}
if (key === "v" && metaKey && copiedData.length) {
- colorStore.setCurrentPalColors([
- ...$colorStore.currentPal.colors,
- ...copiedData,
- ]);
+ colorStore.setCurrentPalColors([...currentPal.colors, ...copiedData]);
}
}
diff --git a/src/components/Vega.svelte b/src/components/Vega.svelte
index e467047d..36aaf96e 100644
--- a/src/components/Vega.svelte
+++ b/src/components/Vega.svelte
@@ -7,8 +7,8 @@
export let size = 300;
let producedSVG: string = "";
-
- $: getSVG(spec, $colorStore.currentPal).then((x) => {
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: getSVG(spec, currentPal).then((x) => {
producedSVG = x;
});
diff --git a/src/content-modules/ComparePal.svelte b/src/content-modules/ComparePal.svelte
index aabd06a2..d767fa0f 100644
--- a/src/content-modules/ComparePal.svelte
+++ b/src/content-modules/ComparePal.svelte
@@ -11,11 +11,13 @@
import SetColorSpace from "./contextual-tools/SetColorSpace.svelte";
- $: compareName = $configStore.comparePal;
- $: ComparisonPal = $colorStore.palettes.find((x) => x.name === compareName);
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: compareIdx = $configStore.comparePal;
+ $: focused = $focusStore.focusedColors;
+ $: ComparisonPal = compareIdx ? $colorStore.palettes[compareIdx] : undefined;
$: {
- if (!ComparisonPal && $colorStore.currentPal.name === compareName) {
- ComparisonPal = $colorStore.currentPal;
+ if (!ComparisonPal && $colorStore.currentPal === compareIdx) {
+ ComparisonPal = currentPal;
}
}
let bg = ComparisonPal?.background.toHex() || "white";
@@ -26,13 +28,13 @@
{#if ComparisonPal !== undefined}
- Compare Pal: {compareName}
+ Compare Pal: {ComparisonPal.name}
{}}
@@ -50,22 +52,22 @@
- Select comparison. Currently: {compareName || "none"}
+ Select comparison. Currently: {ComparisonPal?.name || "none"}
Current Palette:
configStore.setComparePal($colorStore.currentPal.name)}
+ pal={currentPal}
+ onClick={() => configStore.setComparePal($colorStore.currentPal)}
/>
Other Saved Palettes:
- {#each [$colorStore.currentPal, ...$colorStore.palettes] as pal, idx}
+ {#each [currentPal, ...$colorStore.palettes] as pal, idx}
configStore.setComparePal(pal.name)}
+ onClick={() => configStore.setComparePal(idx)}
/>
{/each}
diff --git a/src/content-modules/Controls.svelte b/src/content-modules/Controls.svelte
index 2c5f2ee5..996f0616 100644
--- a/src/content-modules/Controls.svelte
+++ b/src/content-modules/Controls.svelte
@@ -11,8 +11,9 @@
import Rotate from "./contextual-tools/Rotate.svelte";
import ColorChannelPicker from "../components/ColorChannelPicker.svelte";
- $: colors = $colorStore.currentPal.colors;
- $: colorSpace = $colorStore.currentPal.colorSpace;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: colors = currentPal.colors;
+ $: colorSpace = currentPal.colorSpace;
$: focusedColors = $focusStore.focusedColors;
diff --git a/src/content-modules/Eval.svelte b/src/content-modules/Eval.svelte
index 0cc43bb8..bfe0c08e 100644
--- a/src/content-modules/Eval.svelte
+++ b/src/content-modules/Eval.svelte
@@ -16,13 +16,14 @@
$: selectedBlindType = $configStore.colorSim;
let metric: "dE" | "dE94" | "none" = "none";
- $: colors = $colorStore.currentPal.colors;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: colors = currentPal.colors;
$: stats = computeStats(colors, metric);
$: colorNames = colorNameSimple(colors);
- $: palType = $colorStore.currentPal.type;
- $: evalConfig = $colorStore.currentPal.evalConfig;
- $: checks = runLintChecks($colorStore.currentPal).filter((x) =>
+ $: palType = currentPal.type;
+ $: evalConfig = currentPal.evalConfig;
+ $: checks = runLintChecks(currentPal).filter((x) =>
x.taskTypes.includes(palType)
);
@@ -243,7 +244,7 @@
{/if}
{/each}
{/each}
- {#if Object.keys($colorStore.currentPal.evalConfig)}
+ {#if Object.keys(currentPal.evalConfig)}
colorStore.setCurrentPalEvalConfig({})}
diff --git a/src/content-modules/Examples.svelte b/src/content-modules/Examples.svelte
index 52d98cff..7380e790 100644
--- a/src/content-modules/Examples.svelte
+++ b/src/content-modules/Examples.svelte
@@ -19,8 +19,9 @@
let modalState: "closed" | "input-svg" | "input-vega" | "edit-colors" =
"closed";
let modifyingExample: number | false = false;
- $: bg = $colorStore.currentPal.background;
- $: colorSpace = $colorStore.currentPal.colorSpace;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: bg = currentPal.background;
+ $: colorSpace = currentPal.colorSpace;
let value = "";
$: detectedColors = [] as string[];
diff --git a/src/content-modules/SavedPals.svelte b/src/content-modules/SavedPals.svelte
index 3e5ece8f..da2d96c2 100644
--- a/src/content-modules/SavedPals.svelte
+++ b/src/content-modules/SavedPals.svelte
@@ -3,6 +3,7 @@
import focusStore from "../stores/focus-store";
import PalPreview from "../components/PalPreview.svelte";
+
import { buttonStyle } from "../lib/styles";
import Tooltip from "../components/Tooltip.svelte";
@@ -13,11 +14,14 @@
+ {#if i === $colorStore.currentPal}
+
Current Pal:
+ {/if}
{
focusStore.clearColors();
- colorStore.startUsingPal(pal.name);
+ colorStore.startUsingPal(i);
}}
>
{pal.name}
@@ -27,9 +31,9 @@
colorStore.copyPal(pal.name)}
+ on:click={() => colorStore.duplicatePal(i)}
>
- Copy
+ Duplicate
void;
- $: colors = $colorStore.currentPal.colors;
- $: colorSpace = $colorStore.currentPal.colorSpace;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: colors = currentPal.colors;
+ $: colorSpace = currentPal.colorSpace;
function updateColor(color: Color) {
const updatedColors = [...colors];
diff --git a/src/content-modules/Swatches.svelte b/src/content-modules/Swatches.svelte
index c455f6e2..86873e3c 100644
--- a/src/content-modules/Swatches.svelte
+++ b/src/content-modules/Swatches.svelte
@@ -4,10 +4,10 @@
import focusStore from "../stores/focus-store";
import Tooltip from "../components/Tooltip.svelte";
import SwatchTooltipContent from "./SwatchTooltipContent.svelte";
- import { swap } from "../lib/utils";
- import { buttonStyle } from "../lib/styles";
- $: colors = $colorStore.currentPal.colors;
- $: bg = $colorStore.currentPal.background;
+
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: colors = currentPal.colors;
+ $: bg = currentPal.background;
$: focused = $focusStore.focusedColors;
$: focusSet = new Set(focused);
diff --git a/src/content-modules/context-free-tools/GetColorsFromString.svelte b/src/content-modules/context-free-tools/GetColorsFromString.svelte
index 5a6dc27e..3aeadff3 100644
--- a/src/content-modules/context-free-tools/GetColorsFromString.svelte
+++ b/src/content-modules/context-free-tools/GetColorsFromString.svelte
@@ -2,9 +2,10 @@
import { Color } from "../../lib/Color";
import colorStore from "../../stores/color-store";
import configStore from "../../stores/config-store";
- $: colors = $colorStore.currentPal.colors;
let state: "idle" | "error" = "idle";
- $: colorSpace = $colorStore.currentPal.colorSpace;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: colors = currentPal.colors;
+ $: colorSpace = currentPal.colorSpace;
function processBodyInput(body: string) {
try {
diff --git a/src/content-modules/context-free-tools/NewPal.svelte b/src/content-modules/context-free-tools/NewPal.svelte
index 5bb95b7f..153c3815 100644
--- a/src/content-modules/context-free-tools/NewPal.svelte
+++ b/src/content-modules/context-free-tools/NewPal.svelte
@@ -13,7 +13,8 @@
import MiniPalPreview from "../../components/MiniPalPreview.svelte";
$: familiarPals = [] as ExtendedPal[];
- $: colorSpace = $colorStore.currentPal.colorSpace;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: colorSpace = currentPal.colorSpace;
onMount(async () => {
let newPals = [] as ExtendedPal[];
diff --git a/src/content-modules/context-free-tools/Sort.svelte b/src/content-modules/context-free-tools/Sort.svelte
index 2754d8ee..175b81ea 100644
--- a/src/content-modules/context-free-tools/Sort.svelte
+++ b/src/content-modules/context-free-tools/Sort.svelte
@@ -5,7 +5,8 @@
const spaces = ["lab", "lch", "hsl", "hsv"];
let selectedColorSpace: (typeof spaces)[number] = "lab";
let selectedLetter = "a";
- $: colors = $colorStore.currentPal.colors;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: colors = currentPal.colors;
function sortByChannel(colorSpace: string, channel: number) {
const newSort = [...colors].sort((a, b) => {
diff --git a/src/content-modules/context-free-tools/SuggestColorPal.svelte b/src/content-modules/context-free-tools/SuggestColorPal.svelte
index 97c27727..27ee154c 100644
--- a/src/content-modules/context-free-tools/SuggestColorPal.svelte
+++ b/src/content-modules/context-free-tools/SuggestColorPal.svelte
@@ -7,7 +7,8 @@
import PalPreview from "../../components/PalPreview.svelte";
import { buttonStyle, AIButtonStyle } from "../../lib/styles";
- $: colorSpace = $colorStore.currentPal.colorSpace;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: colorSpace = currentPal.colorSpace;
let requestState: "idle" | "loading" | "loaded" | "failed" = "idle";
let newPal: Palette | undefined = undefined;
let palPrompt: string = "";
diff --git a/src/content-modules/context-free-tools/SuggestName.svelte b/src/content-modules/context-free-tools/SuggestName.svelte
index 56ff9492..979d198b 100644
--- a/src/content-modules/context-free-tools/SuggestName.svelte
+++ b/src/content-modules/context-free-tools/SuggestName.svelte
@@ -6,10 +6,12 @@
import { suggestNameForPalette } from "../../lib/api-calls";
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+
function makeRequest() {
if (requestState === "loading") return;
requestState = "loading";
- suggestNameForPalette($colorStore.currentPal, $configStore.engine)
+ suggestNameForPalette(currentPal, $configStore.engine)
.then((x) => {
nameSuggestions = x;
requestState = "idle";
@@ -24,7 +26,11 @@
let requestState: "idle" | "loading" = "idle";
-
+ {
+ nameSuggestions = [];
+ }}
+>
{#if requestState === "loading"}
diff --git a/src/content-modules/context-free-tools/Zoom.svelte b/src/content-modules/context-free-tools/Zoom.svelte
index 0c094316..aa8b5b3a 100644
--- a/src/content-modules/context-free-tools/Zoom.svelte
+++ b/src/content-modules/context-free-tools/Zoom.svelte
@@ -13,7 +13,8 @@
$: zZoom = $configStore.zZoom;
$: zZoom && configStore.setZoom("z", zZoom);
- $: colorSpace = $colorStore.currentPal.colorSpace;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: colorSpace = currentPal.colorSpace;
$: config = colorPickerConfig[colorSpace as keyof typeof colorPickerConfig];
$: xName = config.xChannel.toUpperCase();
diff --git a/src/content-modules/contextual-tools/AddColor.svelte b/src/content-modules/contextual-tools/AddColor.svelte
index d769afd9..8eeb30c2 100644
--- a/src/content-modules/contextual-tools/AddColor.svelte
+++ b/src/content-modules/contextual-tools/AddColor.svelte
@@ -17,8 +17,9 @@
.map((x) => componentToHex(x))
.join("")}`;
- $: colors = $colorStore.currentPal.colors;
- $: colorSpace = $colorStore.currentPal.colorSpace;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: colors = currentPal.colors;
+ $: colorSpace = currentPal.colorSpace;
let suggestions = [randColor(), randColor(), randColor()];
@@ -54,11 +55,7 @@
}
requestState = "loading";
- suggestAdditionsToPalette(
- $colorStore.currentPal,
- $configStore.engine,
- searchedString
- )
+ suggestAdditionsToPalette(currentPal, $configStore.engine, searchedString)
.then((x) => {
console.log("ai suggestions", x);
x.forEach((color) => {
diff --git a/src/content-modules/contextual-tools/AdjustColor.svelte b/src/content-modules/contextual-tools/AdjustColor.svelte
index bc511c81..842bc9c4 100644
--- a/src/content-modules/contextual-tools/AdjustColor.svelte
+++ b/src/content-modules/contextual-tools/AdjustColor.svelte
@@ -6,9 +6,10 @@
import { buttonStyle } from "../../lib/styles";
- $: colors = $colorStore.currentPal.colors;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: colors = currentPal.colors;
$: focusedColors = $focusStore.focusedColors;
- $: colorSpace = $colorStore.currentPal.colorSpace;
+ $: colorSpace = currentPal.colorSpace;
type ColorEffect = (color: Color) => [number, number, number];
function actionOnColor(focusedColors: number[], action: ColorEffect) {
diff --git a/src/content-modules/contextual-tools/AlignSelection.svelte b/src/content-modules/contextual-tools/AlignSelection.svelte
index d65f233b..896535d3 100644
--- a/src/content-modules/contextual-tools/AlignSelection.svelte
+++ b/src/content-modules/contextual-tools/AlignSelection.svelte
@@ -4,11 +4,12 @@
import { Color, colorPickerConfig } from "../../lib/Color";
import { buttonStyle } from "../../lib/styles";
- $: colors = $colorStore.currentPal.colors;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: colors = currentPal.colors;
$: focusedColors = $focusStore.focusedColors;
$: focusSet = new Set(focusedColors);
$: focusLabs = focusedColors.map((idx) => colors[idx].toChannels());
- $: colorSpace = $colorStore.currentPal.colorSpace;
+ $: colorSpace = currentPal.colorSpace;
$: zName = colorPickerConfig[colorSpace].zChannel;
$: ALIGNS = [
{ pos: 1, name: "Left", op: Math.min },
diff --git a/src/content-modules/contextual-tools/DistributePoints.svelte b/src/content-modules/contextual-tools/DistributePoints.svelte
index c8924e55..e94a5ff1 100644
--- a/src/content-modules/contextual-tools/DistributePoints.svelte
+++ b/src/content-modules/contextual-tools/DistributePoints.svelte
@@ -4,9 +4,10 @@
import focusStore from "../../stores/focus-store";
import { buttonStyle } from "../../lib/styles";
- $: colors = $colorStore.currentPal.colors;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: colors = currentPal.colors;
$: focusedColors = $focusStore.focusedColors;
- $: colorSpace = $colorStore.currentPal.colorSpace;
+ $: colorSpace = currentPal.colorSpace;
$: config = colorPickerConfig[colorSpace];
$: zName = colorPickerConfig[colorSpace].zChannel;
diff --git a/src/content-modules/contextual-tools/EvalResponse.svelte b/src/content-modules/contextual-tools/EvalResponse.svelte
index 25a2c17e..703abb07 100644
--- a/src/content-modules/contextual-tools/EvalResponse.svelte
+++ b/src/content-modules/contextual-tools/EvalResponse.svelte
@@ -32,7 +32,8 @@
}
});
}
- $: evalConfig = $colorStore.currentPal.evalConfig;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: evalConfig = currentPal.evalConfig;
function updateEvalConfig(
checkName: string,
value: any,
@@ -98,7 +99,7 @@
Failed to generate suggestions
{:else if requestState === "loaded" && suggestion}
-
+
= 2 && {
- ...$colorStore.currentPal,
+ ...currentPal,
colors: createInterpolation(),
}) as Palette;
function createInterpolatedPoints(pointA: Color, pointB: Color) {
diff --git a/src/content-modules/contextual-tools/ModifySelection.svelte b/src/content-modules/contextual-tools/ModifySelection.svelte
index c53e3025..0bf034f8 100644
--- a/src/content-modules/contextual-tools/ModifySelection.svelte
+++ b/src/content-modules/contextual-tools/ModifySelection.svelte
@@ -5,7 +5,8 @@
import { deDup } from "../../lib/utils";
$: focusedColors = $focusStore.focusedColors;
$: focusSet = new Set(focusedColors);
- $: colors = $colorStore.currentPal.colors;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: colors = currentPal.colors;
$: hasDuplicates = colors && checkForDuplicates();
function checkForDuplicates() {
diff --git a/src/content-modules/contextual-tools/Rotate.svelte b/src/content-modules/contextual-tools/Rotate.svelte
index 60635c9d..deb77bbb 100644
--- a/src/content-modules/contextual-tools/Rotate.svelte
+++ b/src/content-modules/contextual-tools/Rotate.svelte
@@ -8,10 +8,11 @@
let axis = "z" as "x" | "y" | "z";
$: focusedColors = $focusStore.focusedColors;
- $: colors = $colorStore.currentPal.colors;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: colors = currentPal.colors;
$: angle = 0;
- $: colorSpace = $colorStore.currentPal.colorSpace;
+ $: colorSpace = currentPal.colorSpace;
$: angle, rotatePoints();
$: memorizedColors = false as false | Color[];
let rotationPoint = "zero " as number | "avg" | "zero";
diff --git a/src/content-modules/contextual-tools/SuggestionModificationToSelection.svelte b/src/content-modules/contextual-tools/SuggestionModificationToSelection.svelte
index 705ddbe8..971ff029 100644
--- a/src/content-modules/contextual-tools/SuggestionModificationToSelection.svelte
+++ b/src/content-modules/contextual-tools/SuggestionModificationToSelection.svelte
@@ -8,8 +8,9 @@
import PalDiff from "../../components/PalDiff.svelte";
let requestState: "idle" | "loading" | "loaded" | "failed" = "idle";
- $: colorSpace = $colorStore.currentPal.colorSpace;
- $: colors = $colorStore.currentPal.colors;
+ $: currentPal = $colorStore.palettes[$colorStore.currentPal];
+ $: colorSpace = currentPal.colorSpace;
+ $: colors = currentPal.colors;
$: selectedColors = $focusStore.focusedColors
.map((x) => colors[x]?.toHex())
.filter((x) => x !== undefined) as string[];
@@ -20,8 +21,8 @@
return {
colors: colors.map((x) => Color.colorFromString(x, colorSpace)),
name: "mods",
- background: $colorStore.currentPal.background,
- type: $colorStore.currentPal.type,
+ background: currentPal.background,
+ type: currentPal.type,
evalConfig: {},
colorSpace,
};
@@ -31,12 +32,12 @@
requestState = "loading";
const pal = selectedColors.length
? {
- ...$colorStore.currentPal,
+ ...currentPal,
colors: selectedColors.map((x) =>
Color.colorFromString(x, colorSpace)
),
}
- : $colorStore.currentPal;
+ : currentPal;
suggestContextualAdjustments(palPrompt, pal, $configStore.engine)
.then((suggestions) => {
if (suggestions.length === 0) {
@@ -88,9 +89,7 @@
{#if requestState === "loaded"}
diff --git a/src/lib/color-stats.ts b/src/lib/color-stats.ts
index cc5a1aa7..fde5f5a7 100644
--- a/src/lib/color-stats.ts
+++ b/src/lib/color-stats.ts
@@ -1,5 +1,4 @@
import { Color } from "./Color";
-import type { Palette } from "../stores/color-store";
/////////////// MAUREEN'S CODE ///////////////////////
diff --git a/src/stores/color-store.ts b/src/stores/color-store.ts
index c2e690ed..c59cbb9e 100644
--- a/src/stores/color-store.ts
+++ b/src/stores/color-store.ts
@@ -27,12 +27,12 @@ export type Palette = Pal;
interface StoreData {
palettes: Pal[];
- currentPal: Pal;
+ currentPal: number;
}
interface StorageData {
palettes: Pal[];
- currentPal: Pal;
+ currentPal: number;
}
export function newGenericPal(name: string): Pal {
@@ -46,42 +46,27 @@ export function newGenericPal(name: string): Pal {
};
}
+function stringPalToColorPal(pal: Pal): Pal {
+ return {
+ ...pal,
+ background: Color.colorFromString(pal.background, pal.colorSpace),
+ colors: pal.colors.map((x) => Color.colorFromString(x, pal.colorSpace)),
+ };
+}
+
const InitialStore: StorageData = {
palettes: [
newGenericPal("Example 1"),
newGenericPal("Example 2"),
newGenericPal("Example 3"),
],
- currentPal: {
- name: "Untitled",
- colors: pick(outfits),
- background: "#ffffff",
- type: "categorical",
- evalConfig: {},
- colorSpace: "lab",
- },
+ currentPal: 0,
};
function convertStoreHexToColor(store: StorageData): StoreData {
- const space = store.currentPal.colorSpace;
return {
- palettes: store.palettes.map((x) => ({
- ...x,
- background: Color.colorFromString(x.background, x.colorSpace as any),
- colors: x.colors.map((y) =>
- Color.colorFromString(y, x.colorSpace as any)
- ),
- })),
- currentPal: {
- ...store.currentPal,
- background: Color.colorFromString(
- store.currentPal.background,
- space as any
- ),
- colors: store.currentPal.colors.map((y) =>
- Color.colorFromString(y, space as any)
- ),
- },
+ palettes: store.palettes.map(stringPalToColorPal),
+ currentPal: store.currentPal,
};
}
@@ -92,36 +77,21 @@ function convertStoreColorToHex(store: StoreData): StorageData {
background: x.background.toString(),
colors: x.colors.map((y) => y.toString()),
})),
- currentPal: {
- ...store.currentPal,
- background: store.currentPal.background.toString(),
- colors: store.currentPal.colors.map((y) => y.toString()),
- },
+ currentPal: store.currentPal,
};
}
-function insertPalette(palettes: Palette[], pal: Palette): Palette[] {
- let nameCount = palettes.reduce(
- (acc, x) => acc + (x.name === pal.name ? 1 : 0),
- 0
- );
- const name = nameCount === 0 ? pal.name : `${pal.name} ${nameCount}`;
- return [{ ...pal, name }, ...palettes];
-}
-
// install defaults if not present
function addDefaults(store: Partial): StorageData {
// check if the base store objects work right
const storeData = { ...InitialStore, ...store };
// check if the current pal works right
- storeData.currentPal = {
- ...InitialStore.currentPal,
- ...storeData.currentPal,
- };
+ storeData.currentPal = 0;
// also check all of the palettes
+ const genericPal = newGenericPal("Example");
storeData.palettes = storeData.palettes!.map((pal) => ({
- ...InitialStore.currentPal,
+ ...genericPal,
...pal,
}));
@@ -129,12 +99,10 @@ function addDefaults(store: Partial): StorageData {
}
function createStore() {
+ const target =
+ localStorage.getItem("color-pal") || JSON.stringify(InitialStore);
let storeData: StoreData = convertStoreHexToColor(
- addDefaults(
- JSON.parse(
- localStorage.getItem("color-pal") || JSON.stringify(InitialStore)
- )
- )
+ addDefaults(JSON.parse(target))
);
// persist new store to storage
@@ -167,7 +135,12 @@ function createStore() {
return newVal;
});
const palUp = (updateFunc: (old: Palette) => Palette) =>
- persistUpdate((n) => ({ ...n, currentPal: updateFunc(n.currentPal) }));
+ persistUpdate((n) => {
+ const newPal = updateFunc(n.palettes[n.currentPal]);
+ const updatedPals = [...n.palettes];
+ updatedPals[n.currentPal] = newPal;
+ return { ...n, palettes: updatedPals };
+ });
const palsUp = (updateFunc: (old: Palette[]) => Palette[]) =>
persistUpdate((n) => ({ ...n, palettes: updateFunc(n.palettes) }));
@@ -216,27 +189,31 @@ function createStore() {
setPalettes: simpleSet("palettes"),
setCurrentPal: simpleSet("currentPal"),
setCurrentPalColors: (colors: Color[]) => palUp((n) => ({ ...n, colors })),
- startUsingPal: (palName: string) => {
- persistUpdate((n) => {
- const newPal = n.palettes.find((x) => x.name === palName);
- if (!newPal) return n;
- const updatedPals = insertPalette(
- n.palettes.filter((x) => x.name !== palName),
- n.currentPal
- );
- return { ...n, currentPal: newPal, palettes: updatedPals };
- });
+ startUsingPal: (indx: number) => {
+ persistUpdate((n) => ({ ...n, currentPal: indx }));
},
createNewPal: (newPal: Palette) =>
persistUpdate((n) => ({
- ...n,
- currentPal: newPal,
- palettes: insertPalette(n.palettes, n.currentPal),
+ currentPal: 0,
+ palettes: [newPal, ...n.palettes],
})),
removePal: (index: number) =>
- palsUp((n) => n.filter((_x, idx) => idx !== index)),
- copyPal: (pal: string) =>
- palsUp((n) => insertPalette(n, n.find((x) => x.name === pal)!)),
+ persistUpdate((n) => {
+ let palettes = [...n.palettes].filter((_, i) => i !== index);
+ if (palettes.length === 0) {
+ palettes = [stringPalToColorPal(newGenericPal("Example"))];
+ }
+ return {
+ ...n,
+ currentPal: Math.min(index, palettes.length - 1),
+ palettes,
+ };
+ }),
+ duplicatePal: (index: number) =>
+ palsUp((n) => {
+ const newPal = { ...n[index], name: `${n[index].name} copy` };
+ return [...n.slice(0, index), newPal, ...n.slice(index)];
+ }),
setSort: (sort: Color[]) => palUp((n) => ({ ...n, colors: deDup(sort) })),
setCurrentPalName: (name: string) => palUp((n) => ({ ...n, name })),
setCurrentPalType: (type: PalType) => palUp((n) => ({ ...n, type })),
diff --git a/src/stores/config-store.ts b/src/stores/config-store.ts
index aa94a537..c9312d33 100644
--- a/src/stores/config-store.ts
+++ b/src/stores/config-store.ts
@@ -4,7 +4,7 @@ interface StoreData {
route: "examples" | "compare" | "eval";
controlsOpen: boolean;
savedPalsOpen: boolean;
- comparePal: string | undefined;
+ comparePal: number | undefined;
colorSim: "deuteranopia" | "protanopia" | "tritanopia" | "none";
includeQuotes: boolean;
xZoom: [number, number];