diff --git a/src/lib/components/combinations/CombinationGrid.svelte b/src/lib/components/combinations/CombinationGrid.svelte index 4381105..9bcf827 100644 --- a/src/lib/components/combinations/CombinationGrid.svelte +++ b/src/lib/components/combinations/CombinationGrid.svelte @@ -1,14 +1,11 @@
@@ -34,18 +29,20 @@ {/if}
-
- - {#if $cross.length} +
+ {#if $colorEntries.length} @@ -54,36 +51,51 @@ - {#each $cross as { id, name, rgb, bgs }, j (id)} - + {#each $colorEntries as { id: bgId, name, rgbColor }, bgIndex (bgId)} + - {#each bgs as { grade }, i ($cross[i].id)} + {#each $colorEntries as { id: fgId }, fgIndex (fgId)} @@ -104,6 +116,34 @@ Add colors to your palette to try color combinations.

{/if} + +
+ {#each $combinations as { uid, bgName, fgName, contrast, simulatedContrasts }} + + {:else} + {#if colorCount > 1} +

+ There is no color combination matching the + selected filters. +
+ Try adding more colors to your palette or changing + filters. +

+ {:else} +

+ Add colors to your palette to try color + combinations. +

+ {/if} + {/each} +
diff --git a/src/lib/stores/combinations.ts b/src/lib/stores/combinations.ts index e677549..8e1e153 100644 --- a/src/lib/stores/combinations.ts +++ b/src/lib/stores/combinations.ts @@ -1,5 +1,5 @@ import type {Rgb} from 'culori/fn'; -import {readable, type Readable} from 'svelte/store'; +import type {Readable} from 'svelte/store'; import {derived, get, writable} from 'svelte/store'; import type {ContrastType} from '$lib/color/contrast'; import { @@ -124,33 +124,29 @@ const colorPairs = derived(filteredColors, ($colors) => { ); }); -export const cross = derived( - [filteredColors, contrastType], - ([$colors, $type]) => { - const levelFn = - $type === 'wcag2' ? wcag2Contrast : wcag3Contrast; - const gradeFn = - $type === 'wcag2' ? wcag2Grade : wcag3Grade; - - return $colors.map((fg) => ({ - id: fg.id, - name: fg.name, - rgb: fg.rgbColor, - bgs: $colors.map((bg) => { - const level = levelFn(bg.rgbColor, fg.rgbColor); - return { - level, - grade: gradeFn(level) - }; - }) - })); +// TODO don't calc here +// just make paires for classic combinations list +export const selectedCombinations = writable< + Record> +>({}); + +export const selectedPairs = derived( + [filteredColors, selectedCombinations], + ([$colors, $selected]) => { + return Object.entries($selected).flatMap(([bgId, fgs]) => { + const bg = $colors.find((c) => c.id === bgId); + return Object.entries(fgs) + .filter(([_, used]) => used) + .map(([fgId]) => { + const fg = $colors.find((c) => c.id === fgId); + return [bg, fg]; + }); + }); } ); -export const selectedCombinations = writable({}); - export const combinations = derived( - [colorPairs, contrastType, blindnessTypes], + [selectedPairs, contrastType, blindnessTypes], ([$pairs, $type, $blindnessTypes]) => { // Not in derived dependencies so it doesn't trigger // a computation (only sortedColorIds should). @@ -159,10 +155,10 @@ export const combinations = derived( const gradeFn = $type === 'wcag2' ? wcag2Grade : wcag3Grade; - return $pairs.map(([bgOutput, fgOutput]): Combination => { + return $pairs.map(([bgEntry, fgEntry]): Combination => { const baseLevel = levelFn( - bgOutput.rgbColor, - fgOutput.rgbColor + bgEntry.rgbColor, + fgEntry.rgbColor ); const baseGrade = gradeFn(baseLevel); @@ -179,8 +175,8 @@ export const combinations = derived( ? simulateDeuteranomaly : simulateTritanomaly; - const bg = simulate(bgOutput.rgbColor); - const fg = simulate(fgOutput.rgbColor); + const bg = simulate(bgEntry.rgbColor); + const fg = simulate(fgEntry.rgbColor); const level = levelFn(bg, fg); const grade = gradeFn(level); @@ -200,15 +196,15 @@ export const combinations = derived( ); return { - uid: `${bgOutput.id}-${fgOutput.id}`, - bgName: bgOutput.name, - fgName: fgOutput.name, + uid: `${bgEntry.id}-${fgEntry.id}`, + bgName: bgEntry.name, + fgName: fgEntry.name, minLevel, minGrade, contrast: { type: 'none', - bg: bgOutput.rgbColor, - fg: fgOutput.rgbColor, + bg: bgEntry.rgbColor, + fg: fgEntry.rgbColor, level: baseLevel, grade: baseGrade },
Test
- {#each $cross as { id, name, rgb } (id)} + {#each $colorEntries as { id, name, rgbColor } (id)} - + Tt
- {#if i !== j} + {#if fgIndex !== bgIndex} { - $selectedCombinations[ - $cross[i].id - ][id] = e.target.checked; + bgId + ]?.[fgId]} + on:change={(e) => { + if ( + bgId in + $selectedCombinations + ) { + $selectedCombinations[ + bgId + ][fgId] = + e.target.checked; + } else { + $selectedCombinations[ + bgId + ] = { + [fgId]: + e.target.checked + }; + } }} /> -