<template>
<div class="color-picker">
<ul class="swatches">
<li
:key="index"
v-for="(swatch, index) in swatches"
:style="{ background: `#${swatch}` }"
class="swatch"
:class="[
{ active: index === activeSwatch },
{ light: isLight(swatch) }
]"
@click="activeSwatch = index"
>
<check-icon />
</li>
</ul>
<div class="color">
<button
:key="index"
v-for="(mode, index) in colorModes"
class="color-mode"
:class="[{ active: index === activeMode }, `color-mode-${mode}`]"
@click="activeMode = index"
>
{{ mode }}
</button>
</div>
<div class="color-code">{{ activeCode }}</div>
</div>
</template>
<script>
import { rgb, hex, hsl, isLight } from "@/utils/color";
import CheckIcon from "@/assets/check.svg";
const modes = { rgb, hex, hsl };
export default {
components: { CheckIcon },
props: {
swatches: {
type: Array,
default() {
return [];
}
}
},
data() {
return {
activeSwatch: 0,
activeMode: 0,
colorModes: ["hex", "rgb", "hsl"],
isLight
};
},
computed: {
activeCode() {
const activeColor = this.swatches[this.activeSwatch];
const activeMode = this.colorModes[this.activeMode];
return modes[activeMode](activeColor);
}
}
};
</script>
Artwork by Simon Ålander
Artwork by Simon Ålander
5 stars to display