Skip to content

Commit

Permalink
let you grab colors
Browse files Browse the repository at this point in the history
  • Loading branch information
KTibow committed Aug 21, 2024
1 parent 7aa5f5f commit 8dabc00
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
47 changes: 45 additions & 2 deletions src/routes/theme/ColorCard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
MaterialDynamicColors,
hexFromArgb,
} from "@material/material-color-utilities";
import { createEventDispatcher } from "svelte";
export let fg: string;
export let bg: string;
export let scheme: DynamicScheme;
export let grabbing: boolean;
type Color =
| "primary"
| "onPrimary"
Expand Down Expand Up @@ -45,19 +48,59 @@
| "surfaceContainerHigh"
| "surfaceContainerHighest"
| "surfaceTint";
let state = 0;
const dispatch = createEventDispatcher();
$: bgColor = hexFromArgb(MaterialDynamicColors[bg as Color].getArgb(scheme));
$: fgColor = hexFromArgb(MaterialDynamicColors[fg as Color].getArgb(scheme));
$: if (!grabbing) state = 0;
</script>

<div style="background-color: {bgColor}; color: {fgColor};">
<div
class="card"
style="background-color: {bgColor}; color: {fgColor};"
role={grabbing ? "button" : undefined}
on:click={(e) => {
if (state) {
if (state == 2) {
navigator.clipboard.writeText(bgColor);
} else {
navigator.clipboard.writeText(fgColor);
}
dispatch("grabbed");
}
}}
on:mousemove={(e) => {
if (grabbing) {
const rect = e.currentTarget.getBoundingClientRect();
const y = Math.min(Math.max(e.clientY - rect.top, 0), rect.height) / rect.height;
state = y < 0.3 ? 1 : 2;
}
}}
on:mouseleave={() => {
state = 0;
}}
>
<p class="m3-font-headline-small">{bg}</p>
<p class="m3-font-body-large">{fg} text</p>
{#if state}
<div class="overlay" style:background-color={state == 2 ? bgColor : fgColor} />
{/if}
</div>

<style>
div {
.card {
padding: 1rem;
white-space: pre-wrap;
position: relative;
}
.card[role="button"] {
cursor: pointer;
}
.overlay {
position: absolute;
inset: 0;
pointer-events: none;
}
p {
margin: 0;
Expand Down
21 changes: 20 additions & 1 deletion src/routes/theme/SchemeShowcase.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import iconCopy from "@ktibow/iconset-material-symbols/content-copy-outline";
import iconLight from "@ktibow/iconset-material-symbols/light-mode-outline";
import iconDark from "@ktibow/iconset-material-symbols/dark-mode-outline";
import iconGrab from "@ktibow/iconset-material-symbols/unarchive-outline";
import { onMount } from "svelte";
import StyleFromScheme from "$lib/misc/StyleFromScheme.svelte";
Expand All @@ -14,6 +15,7 @@
export let schemeLight: DynamicScheme;
export let schemeDark: DynamicScheme;
let showDark = false;
let grabbing = false;
const copyUsage = () =>
navigator.clipboard.writeText(
Expand All @@ -35,7 +37,13 @@
<h2 class="m3-font-title-large">Your scheme 🎉</h2>
<div class="color-container">
{#each pairs as [bgName, fgName]}
<ColorCard scheme={showDark ? schemeDark : schemeLight} fg={fgName} bg={bgName} />
<ColorCard
scheme={showDark ? schemeDark : schemeLight}
fg={fgName}
bg={bgName}
{grabbing}
on:grabbed={() => (grabbing = false)}
/>
{/each}
</div>
<div class="buttons">
Expand All @@ -47,6 +55,17 @@
<Icon icon={showDark ? iconLight : iconDark} />
{showDark ? "Light" : "Dark"}
</Button>
{#if grabbing}
<Button type="tonal" iconType="left" on:click={() => (grabbing = false)}>
<Icon icon={iconGrab} />
Stop grab
</Button>
{:else}
<Button type="tonal" iconType="left" on:click={() => (grabbing = true)}>
<Icon icon={iconGrab} />
Grab
</Button>
{/if}
</div>
</div>

Expand Down

0 comments on commit 8dabc00

Please sign in to comment.