diff --git a/src/tools/color-converter/color-converter.vue b/src/tools/color-converter/color-converter.vue index a7d6139f..e94b2cb4 100644 --- a/src/tools/color-converter/color-converter.vue +++ b/src/tools/color-converter/color-converter.vue @@ -6,9 +6,11 @@ import cmykPlugin from 'colord/plugins/cmyk'; import hwbPlugin from 'colord/plugins/hwb'; import namesPlugin from 'colord/plugins/names'; import lchPlugin from 'colord/plugins/lch'; +import xyzPlugin from 'colord/plugins/xyz'; +import labPlugin from 'colord/plugins/lab'; import { buildColorFormat } from './color-converter.models'; -extend([cmykPlugin, hwbPlugin, namesPlugin, lchPlugin]); +extend([cmykPlugin, hwbPlugin, namesPlugin, lchPlugin, xyzPlugin, labPlugin]); const formats = { picker: buildColorFormat({ @@ -46,6 +48,18 @@ const formats = { format: (v: Colord) => v.toCmykString(), placeholder: 'e.g. cmyk(0, 100%, 100%, 0)', }), + lab: buildColorFormat({ + label: 'lab', + format: (v: Colord) => JSON.stringify(v.toLab()), + placeholder: 'e.g. { l: 14.89, a: 5.77, b: 14.41, alpha: 0.5 }', + parse: value => colord(JSON.parse(value)), + }), + xyz: buildColorFormat({ + label: 'xyz', + format: (v: Colord) => JSON.stringify(v.toXyz()), + placeholder: 'e.g. { x: 95.047, y: 100, z: 108.883, a: 1 }', + parse: value => colord(JSON.parse(value)), + }), name: buildColorFormat({ label: 'name', format: (v: Colord) => v.toName({ closest: true }) ?? 'Unknown', @@ -53,7 +67,17 @@ const formats = { }), }; -updateColorValue(colord('#1ea54c')); +const saturation = ref(0); +const brightness = ref(0); +const grayscale = ref(false); +const invert = ref(false); + +let lastColor = colord('#1ea54c'); +watch([saturation, brightness, grayscale, invert], + () => updateColorValue(lastColor), +); + +updateColorValue(lastColor); function updateColorValue(value: Colord | undefined, omitLabel?: string) { if (value === undefined) { @@ -64,40 +88,89 @@ function updateColorValue(value: Colord | undefined, omitLabel?: string) { return; } + lastColor = value; + + let correctedValue = value; + if (grayscale.value) { + correctedValue = correctedValue.grayscale(); + } + if (invert.value) { + correctedValue = correctedValue.invert(); + } + + const saturationFloat = saturation.value / 100.0; + if (saturationFloat > 0) { + correctedValue = correctedValue.saturate(saturationFloat); + } + else if (saturationFloat < 0) { + correctedValue = correctedValue.desaturate(-saturationFloat); + } + + const brightnessFloat = brightness.value / 100.0; + if (brightnessFloat > 0) { + correctedValue = correctedValue.lighten(brightnessFloat); + } + else if (brightnessFloat < 0) { + correctedValue = correctedValue.darken(-brightnessFloat); + } + _.forEach(formats, ({ value: valueRef, format }, key) => { if (key !== omitLabel) { - valueRef.value = format(value); + valueRef.value = format(correctedValue); } }); }