|
3 | 3 | import { fade } from 'svelte/transition';
|
4 | 4 | import IoMdClose from 'svelte-icons/io/IoMdClose.svelte';
|
5 | 5 | import IconButton from './shared/IconButton.svelte';
|
6 |
| - import { defaultColors, LANGS } from '../ts/constants'; |
| 6 | + import { |
| 7 | + colorSecondary, |
| 8 | + COLOR_HEX_REGEX, |
| 9 | + defaultColors, |
| 10 | + LANGS, |
| 11 | + MIN_CONTRAST, |
| 12 | + } from '../ts/constants'; |
7 | 13 | import settings, { Settings } from '../store/settings';
|
8 | 14 | import Icon from './shared/Icon.svelte';
|
9 | 15 | import IoMdCheckmark from 'svelte-icons/io/IoMdCheckmark.svelte';
|
|
15 | 21 | let localSettings: Settings = loadSettings();
|
16 | 22 |
|
17 | 23 | let colorInput = localSettings.colorTheme.hex();
|
| 24 | + let colorErrorMessageKey: string | null; |
| 25 | +
|
18 | 26 | $: colorErrorMessageKey = getColorErrorMessageKey(colorInput);
|
19 | 27 |
|
20 | 28 | const dispatch = createEventDispatcher();
|
|
28 | 36 | };
|
29 | 37 |
|
30 | 38 | const getColorErrorMessageKey = (colorStr: string) => {
|
31 |
| - let color: Color; |
32 |
| -
|
33 |
| - try { |
34 |
| - color = Color(colorStr); |
35 |
| - } catch { |
| 39 | + if (!colorStr.match(COLOR_HEX_REGEX)) { |
36 | 40 | return 'color-invalid';
|
37 | 41 | }
|
38 | 42 |
|
39 |
| - if (color.contrast(Color('#fff')) < 1.5) { |
| 43 | + const color = Color(colorStr); |
| 44 | +
|
| 45 | + if (color.contrast(colorSecondary) < MIN_CONTRAST) { |
40 | 46 | return 'color-too-bright';
|
41 | 47 | }
|
42 | 48 |
|
|
46 | 52 | const handleColorInputChange = () => {
|
47 | 53 | if (colorErrorMessageKey !== null) return;
|
48 | 54 |
|
49 |
| - updateColorTheme(localSettings.colorTheme); |
| 55 | + updateColorTheme(Color(colorInput)); |
50 | 56 | };
|
51 | 57 |
|
52 | 58 | const updateColorTheme = (color: Color) => {
|
53 | 59 | setColorTheme(color);
|
54 |
| - console.log(color.hex()); |
55 | 60 | colorInput = color.hex();
|
56 | 61 | };
|
57 | 62 |
|
|
123 | 128 | </div>
|
124 | 129 | {#each defaultColors as color}
|
125 | 130 | <div
|
126 |
| - class={`color ${localSettings.colorTheme === color ? 'color-active' : ''}`} |
| 131 | + class={`color ${color.hex() === colorInput ? 'color-active' : ''}`} |
127 | 132 | style={`--color: ${color}`}
|
128 | 133 | on:click={() => updateColorTheme(color)}
|
129 | 134 | >
|
130 |
| - {#if localSettings.colorTheme === color} |
| 135 | + {#if color.hex() === colorInput} |
131 | 136 | <Icon icon={IoMdCheckmark} />
|
132 | 137 | {/if}
|
133 | 138 | </div>
|
|
0 commit comments