|
|
@ -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 pickerWidth = 0; |
|
|
|
|
|
|
|
|
|
|
|
let adder = null; |
|
|
export let pickerHeight = 0 |
|
|
|
|
|
export let pickerWidth = 0 |
|
|
|
|
|
|
|
|
|
|
|
let adder = null |
|
|
|
|
|
|
|
|
let h = null |
|
|
let h = null |
|
|
let s = null |
|
|
let s = null |
|
|
@ -33,7 +33,7 @@ |
|
|
const dispatch = createEventDispatcher() |
|
|
const dispatch = createEventDispatcher() |
|
|
|
|
|
|
|
|
onMount(() => { |
|
|
onMount(() => { |
|
|
if(!swatches.length > 0) { |
|
|
if (!swatches.length > 0) { |
|
|
//Don't use locally stored recent colors if swatches have been passed as props |
|
|
//Don't use locally stored recent colors if swatches have been passed as props |
|
|
getRecentColors() |
|
|
getRecentColors() |
|
|
} |
|
|
} |
|
|
@ -46,7 +46,7 @@ |
|
|
function getRecentColors() { |
|
|
function getRecentColors() { |
|
|
let colorStore = localStorage.getItem("cp:recent-colors") |
|
|
let colorStore = localStorage.getItem("cp:recent-colors") |
|
|
if (colorStore) { |
|
|
if (colorStore) { |
|
|
swatches = JSON.parse(colorStore) |
|
|
swatches = JSON.parse(colorStore) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -74,26 +74,26 @@ |
|
|
|
|
|
|
|
|
//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() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function dispatchValue() { |
|
|
function dispatchValue() { |
|
|
@ -107,37 +107,36 @@ |
|
|
|
|
|
|
|
|
function handleColorInput(text) { |
|
|
function handleColorInput(text) { |
|
|
let format = getColorFormat(text) |
|
|
let format = getColorFormat(text) |
|
|
if(format) { |
|
|
if (format) { |
|
|
value = text |
|
|
value = text |
|
|
convertAndSetHSVA() |
|
|
convertAndSetHSVA() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function dispatchInputChange() { |
|
|
function dispatchInputChange() { |
|
|
if(format) { |
|
|
if (format) { |
|
|
dispatchValue() |
|
|
dispatchValue() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function addSwatch() { |
|
|
function addSwatch() { |
|
|
if(format) { |
|
|
if (format) { |
|
|
dispatch("addswatch", value) |
|
|
dispatch("addswatch", value) |
|
|
setRecentColor(value) |
|
|
setRecentColor(value) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
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) |
|
|
if(format) { |
|
|
if (format) { |
|
|
value = color |
|
|
value = color |
|
|
convertAndSetHSVA() |
|
|
convertAndSetHSVA() |
|
|
dispatchValue() |
|
|
dispatchValue() |
|
|
@ -145,9 +144,9 @@ |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$: 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> |
|
|
|
|
|
|
|
|
<div class="colorpicker-container"> |
|
|
<div class="colorpicker-container"> |
|
|
@ -182,6 +181,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> |
|
|
@ -227,7 +293,7 @@ |
|
|
border-radius: 50%; |
|
|
border-radius: 50%; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
.swatch-panel { |
|
|
.swatch-panel { |
|
|
flex: 0 0 15px; |
|
|
flex: 0 0 15px; |
|
|
display: flex; |
|
|
display: flex; |
|
|
flex-flow: row wrap; |
|
|
flex-flow: row wrap; |
|
|
@ -238,7 +304,7 @@ |
|
|
|
|
|
|
|
|
.adder { |
|
|
.adder { |
|
|
flex: 1; |
|
|
flex: 1; |
|
|
height: 20px; |
|
|
height: 20px; |
|
|
display: flex; |
|
|
display: flex; |
|
|
transition: flex 0.5s; |
|
|
transition: flex 0.5s; |
|
|
justify-content: center; |
|
|
justify-content: center; |
|
|
@ -252,7 +318,7 @@ |
|
|
font-weight: 500; |
|
|
font-weight: 500; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
.shrink { |
|
|
.shrink { |
|
|
flex: 0 0 20px; |
|
|
flex: 0 0 20px; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -263,54 +329,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> |
|
|
|
|
|
|