Skip to content

Commit

Permalink
add fill picker
Browse files Browse the repository at this point in the history
  • Loading branch information
Logicer16 committed Nov 23, 2023
1 parent 67670c6 commit 258a8c6
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
} from "@skeletonlabs/skeleton";
import {browser} from "$app/environment";
import FileSaver from "file-saver";
import type {SVGData} from "$lib/svgManipulator";
import {vips} from "$lib/vips/vips";
import type Vips from "wasm-vips";
export let svg: SVGData | undefined;
const value = undefined;
type WellKnownDirectory =
Expand Down Expand Up @@ -136,12 +139,11 @@
}
async function getFile(): Promise<File | undefined> {
const svg = await (await fetch("/favicon/favicon.svg")).arrayBuffer();
const format = DownloadFormat.getFormatFromExtension(
downloadFormats,
comboboxValue
);
if (format === undefined) return;
if (format === undefined || svg === undefined) return;
let data: BlobPart | undefined;
if (format.extension === "svg") {
data = svg;
Expand Down Expand Up @@ -234,8 +236,7 @@
}
</script>

<div class="container mx-auto space-x-2 space-y-8 p-8">
<h1 class="h1">Image Conversion Test</h1>
<div class="container mx-auto space-x-2 space-y-8">
<ProgressRadial
{value}
stroke="{100}"
Expand Down
9 changes: 9 additions & 0 deletions src/lib/SVGPreview/SVGPreview.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script lang="ts">
import type {SVGData} from "$lib/svgManipulator";
export let svg: SVGData | undefined;
</script>

{#if svg !== undefined}
{@html svg}
{/if}
42 changes: 42 additions & 0 deletions src/lib/svgManipulator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type Vips from "wasm-vips";

export type SVGData = Vips.Blob & BlobPart;

export interface SVGProcessParameters {
fillColour?: string;
}

const textDecoder = new TextDecoder("utf-8");

export function processSvg(
rawSvg: SVGData,
parameters: SVGProcessParameters
): string;
export function processSvg(
rawSvg: undefined,
parameters: SVGProcessParameters
): undefined;
export function processSvg(
rawSvg: SVGData | undefined,
parameters: SVGProcessParameters
): string | undefined {
if (rawSvg === undefined) return;
return addStyle(rawSvg, parameters);
}

function addStyle(rawSvg: SVGData, parameters: SVGProcessParameters): string {
const styles: string[] = [];
if (parameters.fillColour !== undefined) {
styles.push(`#Fill * {fill:${parameters.fillColour};}`);
}
let parsedSvg: string | undefined;
if (typeof rawSvg !== "string") {
parsedSvg = textDecoder.decode(rawSvg);
} else {
parsedSvg = rawSvg;
}
return parsedSvg.replace(
"</svg>",
`<style>${styles.join("\n")}</style></svg>`
);
}
50 changes: 37 additions & 13 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
<script lang="ts">
import {title} from "$lib/const";
import {derived, writable, type Writable} from "svelte/store";
import {processSvg, type SVGData} from "$lib/svgManipulator";
import {themeColour, title} from "$lib/const";
import Download from "$lib/Download/Download.svelte";
import {onMount} from "svelte";
import SvgPreview from "$lib/SVGPreview/SVGPreview.svelte";
let fillColour: Writable<string> | undefined = writable(themeColour);
let rawSvg = writable<string>();
onMount(() => {
fetch("/favicon/favicon.svg")
.then(async (response) => {
return response.text();
})
.then((contents) => {
rawSvg.set(contents);
})
.catch((error) => {
throw error;
});
});
let svg = derived(
[rawSvg, fillColour],
([$rawSvg, $fillColour], set: (value: SVGData) => void) => {
set(processSvg($rawSvg, {fillColour: $fillColour}));
}
);
</script>

<div class="container mx-auto space-y-8 p-8">
<h1 class="h1">{title}</h1>
<p>
Go to the image converter test page <a
class="font-medium text-blue-600 hover:underline dark:text-blue-500"
href="/image-converter">here</a>
</p>
<!-- <section>
<a class="btn variant-filled-primary" href="https://kit.svelte.dev/"
>SvelteKit</a>
<a class="btn variant-filled-secondary" href="https://tailwindcss.com/"
>Tailwind</a>
<a class="btn variant-filled-tertiary" href="https://github.com/">GitHub</a>
</section> -->
<div class="w-80">
<SvgPreview svg="{$svg}"></SvgPreview>
</div>
{#if $fillColour !== ""}
<!-- This will throw an error regarding the format of the hex colour on page load. This has no real effect as it cannot occur. -->
<input type="color" name="fill" bind:value="{$fillColour}" />
{/if}
<Download svg="{$svg}"></Download>
</div>
1 change: 0 additions & 1 deletion src/routes/image-converter/+layout.svelte

This file was deleted.

0 comments on commit 258a8c6

Please sign in to comment.