|
|
@ -1,7 +1,7 @@ |
|
|
<script> |
|
|
<script> |
|
|
import { onMount, createEventDispatcher } from "svelte"; |
|
|
import { onMount, createEventDispatcher } from "svelte" |
|
|
import { fade } from 'svelte/transition'; |
|
|
import { fade } from "svelte/transition" |
|
|
import Swatch from "./Swatch.svelte"; |
|
|
import Swatch from "./Swatch.svelte" |
|
|
import CheckedBackground from "./CheckedBackground.svelte" |
|
|
import CheckedBackground from "./CheckedBackground.svelte" |
|
|
import { buildStyle } from "./helpers.js" |
|
|
import { buildStyle } from "./helpers.js" |
|
|
import { |
|
|
import { |
|
|
@ -14,16 +14,16 @@ |
|
|
import ButtonGroup from "./ButtonGroup.svelte" |
|
|
import ButtonGroup from "./ButtonGroup.svelte" |
|
|
import Input from "./Input.svelte" |
|
|
import Input from "./Input.svelte" |
|
|
|
|
|
|
|
|
export let value = "#3ec1d3ff"; |
|
|
export let value = "#3ec1d3ff" |
|
|
export let swatches = [] //TODO: Safe swatches - limit to 12. warn in console |
|
|
export let swatches = [] //TODO: Safe swatches - limit to 12. warn in console |
|
|
export let disableSwatches = false |
|
|
export let disableSwatches = false |
|
|
export let format = "hexa"; |
|
|
export let format = "hexa" |
|
|
export let open = false; |
|
|
export let open = false |
|
|
|
|
|
|
|
|
export let pickerHeight = 0; |
|
|
export let pickerHeight = 0 |
|
|
export let pickerWidth = 0; |
|
|
export let pickerWidth = 0 |
|
|
|
|
|
|
|
|
let adder = null; |
|
|
let adder = null |
|
|
|
|
|
|
|
|
let h = null |
|
|
let h = null |
|
|
let s = null |
|
|
let s = null |
|
|
@ -74,23 +74,23 @@ |
|
|
|
|
|
|
|
|
//fired by choosing a color from the palette |
|
|
//fired by choosing a color from the palette |
|
|
function setSaturationAndValue({ detail }) { |
|
|
function setSaturationAndValue({ detail }) { |
|
|
s = detail.s; |
|
|
s = detail.s |
|
|
v = detail.v; |
|
|
v = detail.v |
|
|
value = convertHsvaToFormat([h, s, v, a], format); |
|
|
value = convertHsvaToFormat([h, s, v, a], format) |
|
|
dispatchValue() |
|
|
dispatchValue() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function setHue({ color, isDrag }) { |
|
|
function setHue({ color, isDrag }) { |
|
|
h = color; |
|
|
h = color |
|
|
value = convertHsvaToFormat([h, s, v, a], format); |
|
|
value = convertHsvaToFormat([h, s, v, a], format) |
|
|
if (!isDrag) { |
|
|
if (!isDrag) { |
|
|
dispatchValue() |
|
|
dispatchValue() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function setAlpha({ color, isDrag }) { |
|
|
function setAlpha({ color, isDrag }) { |
|
|
a = color === "1.00" ? "1" : color; |
|
|
a = color === "1.00" ? "1" : color |
|
|
value = convertHsvaToFormat([h, s, v, a], format); |
|
|
value = convertHsvaToFormat([h, s, v, a], format) |
|
|
if (!isDrag) { |
|
|
if (!isDrag) { |
|
|
dispatchValue() |
|
|
dispatchValue() |
|
|
} |
|
|
} |
|
|
@ -101,8 +101,8 @@ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function changeFormatAndConvert(f) { |
|
|
function changeFormatAndConvert(f) { |
|
|
format = f; |
|
|
format = f |
|
|
value = convertHsvaToFormat([h, s, v, a], format); |
|
|
value = convertHsvaToFormat([h, s, v, a], format) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function handleColorInput(text) { |
|
|
function handleColorInput(text) { |
|
|
@ -127,13 +127,12 @@ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function removeSwatch(idx) { |
|
|
function removeSwatch(idx) { |
|
|
let removedSwatch = swatches.splice(idx, 1); |
|
|
let removedSwatch = swatches.splice(idx, 1) |
|
|
swatches = swatches |
|
|
swatches = swatches |
|
|
dispatch("removeswatch", removedSwatch) |
|
|
dispatch("removeswatch", removedSwatch) |
|
|
localStorage.setItem("cp:recent-colors", JSON.stringify(swatches)) |
|
|
localStorage.setItem("cp:recent-colors", JSON.stringify(swatches)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function applySwatch(color) { |
|
|
function applySwatch(color) { |
|
|
if (value !== color) { |
|
|
if (value !== color) { |
|
|
format = getColorFormat(color) |
|
|
format = getColorFormat(color) |
|
|
@ -145,7 +144,7 @@ |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$: border = (v > 90 && s < 5) ? "1px dashed #dedada" : "" |
|
|
$: border = v > 90 && s < 5 ? "1px dashed #dedada" : "" |
|
|
$: style = buildStyle({ background: value, border }) |
|
|
$: style = buildStyle({ background: value, border }) |
|
|
$: shrink = swatches.length > 0 |
|
|
$: shrink = swatches.length > 0 |
|
|
</script> |
|
|
</script> |
|
|
@ -174,6 +173,73 @@ |
|
|
</div> |
|
|
</div> |
|
|
</div> |
|
|
</div> |
|
|
|
|
|
|
|
|
|
|
|
</div> |
|
|
|
|
|
<div |
|
|
|
|
|
class="colorpicker-container" |
|
|
|
|
|
bind:clientHeight={pickerHeight} |
|
|
|
|
|
bind:clientWidth={pickerWidth}> |
|
|
|
|
|
|
|
|
|
|
|
<div class="palette-panel"> |
|
|
|
|
|
<Palette on:change={setSaturationAndValue} {h} {s} {v} {a} /> |
|
|
|
|
|
</div> |
|
|
|
|
|
|
|
|
|
|
|
<div class="control-panel"> |
|
|
|
|
|
<div class="alpha-hue-panel"> |
|
|
|
|
|
<div> |
|
|
|
|
|
<CheckedBackground borderRadius="50%" backgroundSize="8px"> |
|
|
|
|
|
<div class="selected-color" {style} /> |
|
|
|
|
|
</CheckedBackground> |
|
|
|
|
|
</div> |
|
|
|
|
|
<div> |
|
|
|
|
|
<Slider |
|
|
|
|
|
type="hue" |
|
|
|
|
|
value={h} |
|
|
|
|
|
on:change={hue => setHue(hue.detail)} |
|
|
|
|
|
on:dragend={dispatchValue} /> |
|
|
|
|
|
|
|
|
|
|
|
<CheckedBackground borderRadius="10px" backgroundSize="7px"> |
|
|
|
|
|
<Slider |
|
|
|
|
|
type="alpha" |
|
|
|
|
|
value={a} |
|
|
|
|
|
on:change={(alpha, isDrag) => setAlpha(alpha.detail, isDrag)} |
|
|
|
|
|
on:dragend={dispatchValue} /> |
|
|
|
|
|
</CheckedBackground> |
|
|
|
|
|
|
|
|
|
|
|
</div> |
|
|
|
|
|
</div> |
|
|
|
|
|
|
|
|
|
|
|
{#if !disableSwatches} |
|
|
|
|
|
<div transition:fade class="swatch-panel"> |
|
|
|
|
|
{#if swatches.length > 0} |
|
|
|
|
|
{#each swatches as color, idx} |
|
|
|
|
|
<Swatch |
|
|
|
|
|
{color} |
|
|
|
|
|
on:click={() => applySwatch(color)} |
|
|
|
|
|
on:removeswatch={() => removeSwatch(idx)} /> |
|
|
|
|
|
{/each} |
|
|
|
|
|
{/if} |
|
|
|
|
|
{#if swatches.length !== 12} |
|
|
|
|
|
<div |
|
|
|
|
|
bind:this={adder} |
|
|
|
|
|
transition:fade |
|
|
|
|
|
class="adder" |
|
|
|
|
|
on:click={addSwatch} |
|
|
|
|
|
class:shrink> |
|
|
|
|
|
<span>+</span> |
|
|
|
|
|
</div> |
|
|
|
|
|
{/if} |
|
|
|
|
|
</div> |
|
|
|
|
|
{/if} |
|
|
|
|
|
|
|
|
|
|
|
<div class="format-input-panel"> |
|
|
|
|
|
<ButtonGroup {format} onclick={changeFormatAndConvert} /> |
|
|
|
|
|
<Input |
|
|
|
|
|
{value} |
|
|
|
|
|
on:input={event => handleColorInput(event.target.value)} |
|
|
|
|
|
on:change={dispatchInputChange} /> |
|
|
|
|
|
</div> |
|
|
|
|
|
</div> |
|
|
|
|
|
|
|
|
</div> |
|
|
</div> |
|
|
|
|
|
|
|
|
<style> |
|
|
<style> |
|
|
@ -255,54 +321,3 @@ |
|
|
padding-top: 3px; |
|
|
padding-top: 3px; |
|
|
} |
|
|
} |
|
|
</style> |
|
|
</style> |
|
|
|
|
|
|
|
|
<div class="colorpicker-container" bind:clientHeight={pickerHeight} bind:clientWidth={pickerWidth}> |
|
|
|
|
|
|
|
|
|
|
|
<div class="palette-panel"> |
|
|
|
|
|
<Palette on:change={setSaturationAndValue} {h} {s} {v} {a} /> |
|
|
|
|
|
</div> |
|
|
|
|
|
|
|
|
|
|
|
<div class="control-panel"> |
|
|
|
|
|
<div class="alpha-hue-panel"> |
|
|
|
|
|
<div> |
|
|
|
|
|
<CheckedBackground borderRadius="50%" backgroundSize="8px"> |
|
|
|
|
|
<div class="selected-color" {style} /> |
|
|
|
|
|
</CheckedBackground> |
|
|
|
|
|
</div> |
|
|
|
|
|
<div> |
|
|
|
|
|
<Slider type="hue" value={h} on:change={(hue) => setHue(hue.detail)} on:dragend={dispatchValue} /> |
|
|
|
|
|
|
|
|
|
|
|
<CheckedBackground borderRadius="10px" backgroundSize="7px"> |
|
|
|
|
|
<Slider |
|
|
|
|
|
type="alpha" |
|
|
|
|
|
value={a} |
|
|
|
|
|
on:change={(alpha, isDrag) => setAlpha(alpha.detail, isDrag)} |
|
|
|
|
|
on:dragend={dispatchValue} |
|
|
|
|
|
/> |
|
|
|
|
|
</CheckedBackground> |
|
|
|
|
|
|
|
|
|
|
|
</div> |
|
|
|
|
|
</div> |
|
|
|
|
|
|
|
|
|
|
|
{#if !disableSwatches} |
|
|
|
|
|
<div transition:fade class="swatch-panel"> |
|
|
|
|
|
{#if swatches.length > 0} |
|
|
|
|
|
{#each swatches as color, idx} |
|
|
|
|
|
<Swatch {color} on:click={() => applySwatch(color)} on:removeswatch={() => removeSwatch(idx)} /> |
|
|
|
|
|
{/each} |
|
|
|
|
|
{/if} |
|
|
|
|
|
{#if swatches.length !== 12} |
|
|
|
|
|
<div bind:this={adder} transition:fade class="adder" on:click={addSwatch} class:shrink> |
|
|
|
|
|
<span>+</span> |
|
|
|
|
|
</div> |
|
|
|
|
|
{/if} |
|
|
|
|
|
</div> |
|
|
|
|
|
{/if} |
|
|
|
|
|
|
|
|
|
|
|
<div class="format-input-panel"> |
|
|
|
|
|
<ButtonGroup {format} onclick={changeFormatAndConvert} /> |
|
|
|
|
|
<Input {value} on:input={event => handleColorInput(event.target.value)} on:change={dispatchInputChange} /> |
|
|
|
|
|
</div> |
|
|
|
|
|
</div> |
|
|
|
|
|
|
|
|
|
|
|
</div> |
|
|
|
|
|
|