|
|
|
@ -1,5 +1,7 @@ |
|
|
|
<script> |
|
|
|
import { onMount, createEventDispatcher } from "svelte"; |
|
|
|
import { fade } from 'svelte/transition'; |
|
|
|
import Swatch from "./Swatch.svelte"; |
|
|
|
import CheckedBackground from "./CheckedBackground.svelte" |
|
|
|
import {buildStyle} from "./helpers.js" |
|
|
|
import { |
|
|
|
@ -13,7 +15,15 @@ |
|
|
|
import Input from "./Input.svelte"; |
|
|
|
|
|
|
|
export let value = "#3ec1d3ff"; |
|
|
|
export let swatches = [] //TODO: Safe swatches - limit to 12. warn in console |
|
|
|
export let disableSwatches = false |
|
|
|
export let format = "hexa"; |
|
|
|
export let open = false; |
|
|
|
|
|
|
|
export let pickerHeight = 0; |
|
|
|
export let pickerWidth = 0; |
|
|
|
|
|
|
|
let adder = null; |
|
|
|
|
|
|
|
let h = null; |
|
|
|
let s = null; |
|
|
|
@ -23,11 +33,33 @@ |
|
|
|
const dispatch = createEventDispatcher(); |
|
|
|
|
|
|
|
onMount(() => { |
|
|
|
if(!swatches.length > 0) { |
|
|
|
//Don't use locally stored recent colors if swatches have been passed as props |
|
|
|
getRecentColors() |
|
|
|
} |
|
|
|
|
|
|
|
if (format) { |
|
|
|
convertAndSetHSVA() |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
function getRecentColors() { |
|
|
|
let colorStore = localStorage.getItem("cp:recent-colors") |
|
|
|
if (colorStore) { |
|
|
|
swatches = JSON.parse(colorStore) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function setRecentColor(color) { |
|
|
|
if (swatches.length === 12) { |
|
|
|
swatches.splice(0, 1) |
|
|
|
} |
|
|
|
if (!swatches.includes(color)) { |
|
|
|
swatches = [...swatches, color] |
|
|
|
localStorage.setItem("cp:recent-colors", JSON.stringify(swatches)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function convertAndSetHSVA() { |
|
|
|
let hsva = convertToHSVA(value, format); |
|
|
|
setHSVA(hsva); |
|
|
|
@ -45,17 +77,27 @@ |
|
|
|
s = detail.s; |
|
|
|
v = detail.v; |
|
|
|
value = convertHsvaToFormat([h, s, v, a], format); |
|
|
|
dispatch("change", value) |
|
|
|
dispatchValue() |
|
|
|
} |
|
|
|
|
|
|
|
function setHue(hue) { |
|
|
|
h = hue; |
|
|
|
value = convertHsvaToFormat([h, s, v, a], format); |
|
|
|
function setHue({color, isDrag}) { |
|
|
|
h = color; |
|
|
|
value = convertHsvaToFormat([h, s, v, a], format); |
|
|
|
if(!isDrag) { |
|
|
|
dispatchValue() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function setAlpha(alpha) { |
|
|
|
a = alpha === "1.00" ? "1" :alpha; |
|
|
|
function setAlpha({color, isDrag}) { |
|
|
|
a = color === "1.00" ? "1" : color; |
|
|
|
value = convertHsvaToFormat([h, s, v, a], format); |
|
|
|
if(!isDrag) { |
|
|
|
dispatchValue() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function dispatchValue() { |
|
|
|
dispatch("change", value) |
|
|
|
} |
|
|
|
|
|
|
|
function changeFormatAndConvert(f) { |
|
|
|
@ -64,17 +106,48 @@ |
|
|
|
} |
|
|
|
|
|
|
|
function handleColorInput(text) { |
|
|
|
let f = getColorFormat(text) |
|
|
|
if(f) { |
|
|
|
format = f; |
|
|
|
let format = getColorFormat(text) |
|
|
|
if(format) { |
|
|
|
value = text |
|
|
|
convertAndSetHSVA() |
|
|
|
dispatch("change", value) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
$: border = s < 10 ? "1px dashed #dedada" : "" |
|
|
|
function dispatchInputChange() { |
|
|
|
if(format) { |
|
|
|
dispatchValue() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function addSwatch() { |
|
|
|
if(format) { |
|
|
|
dispatch("addswatch", value) |
|
|
|
setRecentColor(value) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function removeSwatch(idx) { |
|
|
|
let removedSwatch = swatches.splice(idx, 1); |
|
|
|
swatches = swatches |
|
|
|
dispatch("removeswatch", removedSwatch) |
|
|
|
localStorage.setItem("cp:recent-colors", JSON.stringify(swatches)) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function applySwatch(color) { |
|
|
|
if(value !== color) { |
|
|
|
format = getColorFormat(color) |
|
|
|
if(format) { |
|
|
|
value = color |
|
|
|
convertAndSetHSVA() |
|
|
|
dispatchValue() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
$: border = (v > 90 && s < 5) ? "1px dashed #dedada" : "" |
|
|
|
$: style = buildStyle({background: value, border}) |
|
|
|
$: shrink = swatches.length > 0 |
|
|
|
</script> |
|
|
|
|
|
|
|
<style> |
|
|
|
@ -83,7 +156,8 @@ |
|
|
|
font-size: 11px; |
|
|
|
font-weight: 400; |
|
|
|
flex-direction: column; |
|
|
|
height: 265px; |
|
|
|
/* height: 265px; */ |
|
|
|
height: auto; |
|
|
|
width: 220px; |
|
|
|
background: #ffffff; |
|
|
|
border-radius: 2px; |
|
|
|
@ -118,14 +192,44 @@ |
|
|
|
border-radius: 50%; |
|
|
|
} |
|
|
|
|
|
|
|
.swatch-panel { |
|
|
|
flex: 0 0 15px; |
|
|
|
display: flex; |
|
|
|
flex-flow: row wrap; |
|
|
|
justify-content: flex-start; |
|
|
|
padding: 0 5px; |
|
|
|
max-height: 56px; |
|
|
|
} |
|
|
|
|
|
|
|
.adder { |
|
|
|
flex: 1; |
|
|
|
height: 20px; |
|
|
|
display: flex; |
|
|
|
transition: flex 0.5s; |
|
|
|
justify-content: center; |
|
|
|
align-items: center; |
|
|
|
background: #f1f3f4; |
|
|
|
cursor: pointer; |
|
|
|
border: 1px solid #d4d4d4; |
|
|
|
border-radius: 8px; |
|
|
|
margin-left: 5px; |
|
|
|
margin-top: 3px; |
|
|
|
font-weight: 500; |
|
|
|
} |
|
|
|
|
|
|
|
.shrink { |
|
|
|
flex: 0 0 20px; |
|
|
|
} |
|
|
|
|
|
|
|
.format-input-panel { |
|
|
|
display: flex; |
|
|
|
flex-direction: column; |
|
|
|
justify-content: center; |
|
|
|
padding-top: 3px; |
|
|
|
} |
|
|
|
</style> |
|
|
|
|
|
|
|
<div class="colorpicker-container"> |
|
|
|
<div class="colorpicker-container" bind:clientHeight={pickerHeight} bind:clientWidth={pickerWidth}> |
|
|
|
|
|
|
|
<div class="palette-panel"> |
|
|
|
<Palette on:change={setSaturationAndValue} {h} {s} {v} {a} /> |
|
|
|
@ -139,21 +243,38 @@ |
|
|
|
</CheckedBackground> |
|
|
|
</div> |
|
|
|
<div> |
|
|
|
<Slider type="hue" value={h} on:change={hue => setHue(hue.detail)} /> |
|
|
|
<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 => setAlpha(alpha.detail)} /> |
|
|
|
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)} /> |
|
|
|
<Input {value} on:input={event => handleColorInput(event.target.value)} on:change={dispatchInputChange} /> |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
|
|
|
|
|