From 0619a74fee4117dcff413b0a17a9db6ddffda57f Mon Sep 17 00:00:00 2001 From: James Stuckey Weber Date: Wed, 11 Dec 2024 17:15:19 +0000 Subject: [PATCH] Add button and drag to swap colors --- src/lib/components/colors/Header.svelte | 19 ++++++++++++++++++- src/lib/components/colors/index.svelte | 4 ++-- src/lib/stores.ts | 8 +++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/lib/components/colors/Header.svelte b/src/lib/components/colors/Header.svelte index adb627e6..2af2bf35 100644 --- a/src/lib/components/colors/Header.svelte +++ b/src/lib/components/colors/Header.svelte @@ -4,6 +4,7 @@ import CopyButton from '$lib/components/util/CopyButton.svelte'; import type { ColorFormatId } from '$lib/constants'; + import { switchColors } from '$lib/stores'; import { getSpaceFromFormatId } from '$lib/utils'; interface Props { @@ -20,6 +21,7 @@ let editing = $state(false); let inputValue = $state(''); let hasError = $state(false); + let isDragging = false; // When not editing, sync input value with color (e.g. when sliders change) $effect(() => { @@ -80,6 +82,12 @@ break; } }; + const switchColorsT = () => { + switchColors(); + }; + const makeDropable = (event: DragEvent) => { + if (!isDragging) event.preventDefault(); + };
-
+ diff --git a/src/lib/components/colors/index.svelte b/src/lib/components/colors/index.svelte index ee82c135..edefc17b 100644 --- a/src/lib/components/colors/index.svelte +++ b/src/lib/components/colors/index.svelte @@ -3,7 +3,7 @@ import Header from '$lib/components/colors/Header.svelte'; import Sliders from '$lib/components/colors/Sliders.svelte'; import SupportWarning from '$lib/components/colors/SupportWarning.svelte'; - import { bg, fg, format } from '$lib/stores'; + import { bg, fg, format, switchColors } from '$lib/stores';

Check the contrast ratio between two colors

@@ -13,7 +13,7 @@
- +
diff --git a/src/lib/stores.ts b/src/lib/stores.ts index cc5773cd..2928b2a5 100644 --- a/src/lib/stores.ts +++ b/src/lib/stores.ts @@ -10,7 +10,7 @@ import { REC_2020, sRGB, } from 'colorjs.io/fn'; -import { writable } from 'svelte/store'; +import { get, writable } from 'svelte/store'; // eslint-disable-next-line import/no-unresolved import { browser, dev } from '$app/environment'; @@ -57,6 +57,12 @@ export const reset = () => { fg.set(INITIAL_FG); }; +export const switchColors = () => { + const temp = get(bg); + bg.set(get(fg)); + fg.set(temp); +}; + /* c8 ignore next 5 */ if (browser && dev) { bg.subscribe(($bg) => (window.bg = $bg));