|
1 | 1 | <script> |
| 2 | + import ColorSlider from './ColorSlider.svelte'; |
| 3 | + import {hexToRGb, rgbToHex, rgbToHsv, hsvToRgb} from './color-utils'; |
| 4 | + import {_} from '../locales'; |
| 5 | +
|
2 | 6 | export let value; |
| 7 | + export let defaultColor; |
| 8 | +
|
| 9 | + let editorElement; |
| 10 | + let valueAsHsv; |
| 11 | + let open = false; |
| 12 | +
|
| 13 | + const handleOpen = () => { |
| 14 | + open = !open; |
| 15 | + }; |
| 16 | + const checkClickedOutside = (e) => { |
| 17 | + if (open && editorElement && !editorElement.contains(e.target)) { |
| 18 | + open = false; |
| 19 | + } |
| 20 | + }; |
| 21 | +
|
| 22 | + const setHsv = (hsv) => { |
| 23 | + const rgb = hsvToRgb(...hsv); |
| 24 | + const hex = rgbToHex(...rgb); |
| 25 | + value = hex; |
| 26 | + valueAsHsv = hsv; |
| 27 | + }; |
| 28 | + const setHex = (hex) => { |
| 29 | + const valueAsRgb = hexToRGb(hex); |
| 30 | + value = hex; |
| 31 | + valueAsHsv = rgbToHsv(...valueAsRgb); |
| 32 | + }; |
| 33 | + setHex(value); |
| 34 | +
|
| 35 | + const hsvChangeFactory = (index) => (e) => { |
| 36 | + const newColor = [...valueAsHsv]; |
| 37 | + newColor[index] = e.detail; |
| 38 | + setHsv(newColor); |
| 39 | + }; |
| 40 | + const generateHueSteps = (hsv) => { |
| 41 | + const result = []; |
| 42 | + for (let i = 0; i < 1; i += 0.1) { |
| 43 | + result.push(rgbToHex(...hsvToRgb(i, hsv[1], hsv[2]))); |
| 44 | + } |
| 45 | + return result; |
| 46 | + }; |
| 47 | + const generateSaturationSteps = (hsv) => { |
| 48 | + const result = []; |
| 49 | + for (let i = 0; i < 1; i += 0.1) { |
| 50 | + result.push(rgbToHex(...hsvToRgb(hsv[0], i, hsv[2]))); |
| 51 | + } |
| 52 | + return result; |
| 53 | + }; |
| 54 | + const generateBrightnessSteps = (hsv) => { |
| 55 | + const result = []; |
| 56 | + for (let i = 0; i < 1; i += 0.1) { |
| 57 | + result.push(rgbToHex(...hsvToRgb(hsv[0], hsv[1], i))); |
| 58 | + } |
| 59 | + return result; |
| 60 | + }; |
| 61 | +
|
| 62 | + const handleSetHex = (e) => { |
| 63 | + setHex(e.target.value); |
| 64 | + }; |
| 65 | + const reset = () => { |
| 66 | + setHex(defaultColor); |
| 67 | + }; |
3 | 68 | </script> |
4 | 69 |
|
5 | | -<input type="color" bind:value /> |
| 70 | +<style> |
| 71 | + .container { |
| 72 | + position: relative; |
| 73 | + margin-right: 4px; |
| 74 | + } |
| 75 | + .button, .editor { |
| 76 | + border: 1px solid rgb(160, 160, 160); |
| 77 | + } |
| 78 | + .button { |
| 79 | + background-color: #eee; |
| 80 | + width: 40px; |
| 81 | + height: 2em; |
| 82 | + border-radius: 2px; |
| 83 | + margin: 0; |
| 84 | + padding: 3px; |
| 85 | + box-sizing: border-box; |
| 86 | + } |
| 87 | + .button:hover { |
| 88 | + background-color: #ddd; |
| 89 | + } |
| 90 | + .button:active { |
| 91 | + background-color: #ccc; |
| 92 | + } |
| 93 | + .color-preview { |
| 94 | + width: 100%; |
| 95 | + height: 100%; |
| 96 | + } |
| 97 | + .editor { |
| 98 | + background: white; |
| 99 | + position: absolute; |
| 100 | + z-index: 10; |
| 101 | + top: 100%; |
| 102 | + border-radius: 4px; |
| 103 | + padding: 8px; |
| 104 | + box-shadow: 0 0 4px 0 rgba(0, 0, 0, 0.5); |
| 105 | + } |
| 106 | + .slider-section { |
| 107 | + margin-bottom: 12px; |
| 108 | + } |
| 109 | + .label { |
| 110 | + display: flex; |
| 111 | + font-weight: bold; |
| 112 | + } |
| 113 | + .value { |
| 114 | + font-weight: normal; |
| 115 | + margin-left: 12px; |
| 116 | + } |
| 117 | + .bottom-section { |
| 118 | + display: flex; |
| 119 | + align-items: center; |
| 120 | + } |
| 121 | + .hex-text { |
| 122 | + width: 60px; |
| 123 | + } |
| 124 | + .reset-button { |
| 125 | + margin-left: 8px; |
| 126 | + } |
| 127 | +
|
| 128 | + :global([theme="dark"]) .button, |
| 129 | + :global([theme="dark"]) .editor { |
| 130 | + background-color: #333; |
| 131 | + border-color: #888; |
| 132 | + } |
| 133 | + :global([theme="dark"]) .button:hover { |
| 134 | + background-color: #444; |
| 135 | + } |
| 136 | + :global([theme="dark"]) .button:active { |
| 137 | + background-color: #555; |
| 138 | + } |
| 139 | +</style> |
| 140 | + |
| 141 | +<svelte:body on:mousedown={checkClickedOutside} /> |
| 142 | + |
| 143 | +<div class="container" bind:this={editorElement}> |
| 144 | + <button class="button" on:click={handleOpen}> |
| 145 | + <div class="color-preview" style={`background-color: ${value}`}></div> |
| 146 | + </button> |
| 147 | + {#if open} |
| 148 | + <div class="editor"> |
| 149 | + <div class="slider-section"> |
| 150 | + <div class="label"> |
| 151 | + {$_('color.color')} |
| 152 | + <div class="value">{Math.round(valueAsHsv[0] * 100)}</div> |
| 153 | + </div> |
| 154 | + <div class="slider"> |
| 155 | + <ColorSlider |
| 156 | + value={valueAsHsv[0]} |
| 157 | + steps={generateHueSteps(valueAsHsv)} |
| 158 | + on:change={hsvChangeFactory(0)} |
| 159 | + /> |
| 160 | + </div> |
| 161 | + </div> |
| 162 | + <div class="slider-section"> |
| 163 | + <div class="label"> |
| 164 | + {$_('color.saturation')} |
| 165 | + <div class="value">{Math.round(valueAsHsv[1] * 100)}</div> |
| 166 | + </div> |
| 167 | + <div class="slider"> |
| 168 | + <ColorSlider |
| 169 | + value={valueAsHsv[1]} |
| 170 | + steps={generateSaturationSteps(valueAsHsv)} |
| 171 | + on:change={hsvChangeFactory(1)} |
| 172 | + /> |
| 173 | + </div> |
| 174 | + </div> |
| 175 | + <div class="slider-section"> |
| 176 | + <div class="label"> |
| 177 | + {$_('color.brightness')} |
| 178 | + <div class="value">{Math.round(valueAsHsv[2] * 100)}</div> |
| 179 | + </div> |
| 180 | + <div class="slider"> |
| 181 | + <ColorSlider |
| 182 | + value={valueAsHsv[2]} |
| 183 | + steps={generateBrightnessSteps(valueAsHsv)} |
| 184 | + on:change={hsvChangeFactory(2)} |
| 185 | + /> |
| 186 | + </div> |
| 187 | + </div> |
| 188 | + <div class="bottom-section"> |
| 189 | + <input class="hex-text" type="text" value={value} on:input={handleSetHex} /> |
| 190 | + <button class="reset-button" on:click={reset}>{$_('options.reset')}</button> |
| 191 | + </div> |
| 192 | + </div> |
| 193 | + {/if} |
| 194 | +</div> |
0 commit comments