color-picker-svelte
Version:
Color picker for Svelte
30 lines (29 loc) • 802 B
JavaScript
import { TinyColor } from '@ctrl/tinycolor';
import { clamp } from './util';
export class Color {
h;
s;
v;
a;
constructor(value) {
if (typeof value === 'string') {
const hsv = new TinyColor(value).toHsv();
this.h = hsv.h;
this.s = hsv.s;
this.v = hsv.v;
this.a = hsv.a;
}
else {
this.h = clamp(0, 360, value.h);
this.s = clamp(0, 1, value.s);
this.v = clamp(0, 1, value.v);
this.a = clamp(0, 1, value.a);
}
}
toHexString() {
return new TinyColor({ h: this.h, s: this.s, v: this.v }).toHexString();
}
toHex8String() {
return new TinyColor({ h: this.h, s: this.s, v: this.v, a: this.a }).toHex8String();
}
}