mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-20 14:56:17 -04:00
refactor(color-converter): improved color-converter UX (#701)
This commit is contained in:
parent
020e9cbe41
commit
abb8335041
4 changed files with 167 additions and 63 deletions
23
src/tools/color-converter/color-converter.e2e.spec.ts
Normal file
23
src/tools/color-converter/color-converter.e2e.spec.ts
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
import { expect, test } from '@playwright/test';
|
||||||
|
|
||||||
|
test.describe('Tool - Color converter', () => {
|
||||||
|
test.beforeEach(async ({ page }) => {
|
||||||
|
await page.goto('/color-converter');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Has title', async ({ page }) => {
|
||||||
|
await expect(page).toHaveTitle('Color converter - IT Tools');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Color is converted from its name to other formats', async ({ page }) => {
|
||||||
|
await page.getByTestId('input-name').fill('olive');
|
||||||
|
|
||||||
|
expect(await page.getByTestId('input-name').inputValue()).toEqual('olive');
|
||||||
|
expect(await page.getByTestId('input-hex').inputValue()).toEqual('#808000');
|
||||||
|
expect(await page.getByTestId('input-rgb').inputValue()).toEqual('rgb(128, 128, 0)');
|
||||||
|
expect(await page.getByTestId('input-hsl').inputValue()).toEqual('hsl(60, 100%, 25%)');
|
||||||
|
expect(await page.getByTestId('input-hwb').inputValue()).toEqual('hwb(60 0% 50%)');
|
||||||
|
expect(await page.getByTestId('input-cmyk').inputValue()).toEqual('device-cmyk(0% 0% 100% 50%)');
|
||||||
|
expect(await page.getByTestId('input-lch').inputValue()).toEqual('lch(52.15% 56.81 99.57)');
|
||||||
|
});
|
||||||
|
});
|
13
src/tools/color-converter/color-converter.models.test.ts
Normal file
13
src/tools/color-converter/color-converter.models.test.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import { removeAlphaChannelWhenOpaque } from './color-converter.models';
|
||||||
|
|
||||||
|
describe('color-converter models', () => {
|
||||||
|
describe('removeAlphaChannelWhenOpaque', () => {
|
||||||
|
it('remove alpha channel of an hex color when it is opaque (alpha = 1)', () => {
|
||||||
|
expect(removeAlphaChannelWhenOpaque('#000000ff')).toBe('#000000');
|
||||||
|
expect(removeAlphaChannelWhenOpaque('#ffffffFF')).toBe('#ffffff');
|
||||||
|
expect(removeAlphaChannelWhenOpaque('#000000FE')).toBe('#000000FE');
|
||||||
|
expect(removeAlphaChannelWhenOpaque('#00000000')).toBe('#00000000');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
52
src/tools/color-converter/color-converter.models.ts
Normal file
52
src/tools/color-converter/color-converter.models.ts
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
import { type Colord, colord } from 'colord';
|
||||||
|
import { withDefaultOnError } from '@/utils/defaults';
|
||||||
|
import { useValidation } from '@/composable/validation';
|
||||||
|
|
||||||
|
export { removeAlphaChannelWhenOpaque, buildColorFormat };
|
||||||
|
|
||||||
|
function removeAlphaChannelWhenOpaque(hexColor: string) {
|
||||||
|
return hexColor.replace(/^(#(?:[0-9a-f]{3}){1,2})ff$/i, '$1');
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildColorFormat({
|
||||||
|
label,
|
||||||
|
parse = value => colord(value),
|
||||||
|
format,
|
||||||
|
placeholder,
|
||||||
|
invalidMessage = `Invalid ${label.toLowerCase()} format.`,
|
||||||
|
type = 'text',
|
||||||
|
}: {
|
||||||
|
label: string
|
||||||
|
parse?: (value: string) => Colord
|
||||||
|
format: (value: Colord) => string
|
||||||
|
placeholder?: string
|
||||||
|
invalidMessage?: string
|
||||||
|
type?: 'text' | 'color-picker'
|
||||||
|
}) {
|
||||||
|
const value = ref('');
|
||||||
|
|
||||||
|
return {
|
||||||
|
type,
|
||||||
|
label,
|
||||||
|
parse: (v: string) => withDefaultOnError(() => parse(v), undefined),
|
||||||
|
format,
|
||||||
|
placeholder,
|
||||||
|
value,
|
||||||
|
validation: useValidation({
|
||||||
|
source: value,
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
message: invalidMessage,
|
||||||
|
validator: v => withDefaultOnError(() => {
|
||||||
|
if (v === '') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return parse(v).isValid();
|
||||||
|
}, false),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
|
@ -1,87 +1,103 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import type { Colord } from 'colord';
|
||||||
import { colord, extend } from 'colord';
|
import { colord, extend } from 'colord';
|
||||||
|
import _ from 'lodash';
|
||||||
import cmykPlugin from 'colord/plugins/cmyk';
|
import cmykPlugin from 'colord/plugins/cmyk';
|
||||||
import hwbPlugin from 'colord/plugins/hwb';
|
import hwbPlugin from 'colord/plugins/hwb';
|
||||||
import namesPlugin from 'colord/plugins/names';
|
import namesPlugin from 'colord/plugins/names';
|
||||||
import lchPlugin from 'colord/plugins/lch';
|
import lchPlugin from 'colord/plugins/lch';
|
||||||
import InputCopyable from '../../components/InputCopyable.vue';
|
import { buildColorFormat } from './color-converter.models';
|
||||||
|
|
||||||
extend([cmykPlugin, hwbPlugin, namesPlugin, lchPlugin]);
|
extend([cmykPlugin, hwbPlugin, namesPlugin, lchPlugin]);
|
||||||
|
|
||||||
const name = ref('');
|
const formats = {
|
||||||
const hex = ref('#1ea54cff');
|
picker: buildColorFormat({
|
||||||
const rgb = ref('');
|
label: 'color picker',
|
||||||
const hsl = ref('');
|
format: (v: Colord) => v.toHex(),
|
||||||
const hwb = ref('');
|
type: 'color-picker',
|
||||||
const cmyk = ref('');
|
}),
|
||||||
const lch = ref('');
|
hex: buildColorFormat({
|
||||||
|
label: 'hex',
|
||||||
|
format: (v: Colord) => v.toHex(),
|
||||||
|
placeholder: 'e.g. #ff0000',
|
||||||
|
}),
|
||||||
|
rgb: buildColorFormat({
|
||||||
|
label: 'rgb',
|
||||||
|
format: (v: Colord) => v.toRgbString(),
|
||||||
|
placeholder: 'e.g. rgb(255, 0, 0)',
|
||||||
|
}),
|
||||||
|
hsl: buildColorFormat({
|
||||||
|
label: 'hsl',
|
||||||
|
format: (v: Colord) => v.toHslString(),
|
||||||
|
placeholder: 'e.g. hsl(0, 100%, 50%)',
|
||||||
|
}),
|
||||||
|
hwb: buildColorFormat({
|
||||||
|
label: 'hwb',
|
||||||
|
format: (v: Colord) => v.toHwbString(),
|
||||||
|
placeholder: 'e.g. hwb(0, 0%, 0%)',
|
||||||
|
}),
|
||||||
|
lch: buildColorFormat({
|
||||||
|
label: 'lch',
|
||||||
|
format: (v: Colord) => v.toLchString(),
|
||||||
|
placeholder: 'e.g. lch(53.24, 104.55, 40.85)',
|
||||||
|
}),
|
||||||
|
cmyk: buildColorFormat({
|
||||||
|
label: 'cmyk',
|
||||||
|
format: (v: Colord) => v.toCmykString(),
|
||||||
|
placeholder: 'e.g. cmyk(0, 100%, 100%, 0)',
|
||||||
|
}),
|
||||||
|
name: buildColorFormat({
|
||||||
|
label: 'name',
|
||||||
|
format: (v: Colord) => v.toName({ closest: true }) ?? 'Unknown',
|
||||||
|
placeholder: 'e.g. red',
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
function onInputUpdated(value: string, omit: string) {
|
updateColorValue(colord('#1ea54c'));
|
||||||
try {
|
|
||||||
const color = colord(value);
|
|
||||||
|
|
||||||
if (omit !== 'name') {
|
function updateColorValue(value: Colord | undefined, omitLabel?: string) {
|
||||||
name.value = color.toName({ closest: true }) ?? '';
|
if (value === undefined) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (omit !== 'hex') {
|
|
||||||
hex.value = color.toHex();
|
if (!value.isValid()) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (omit !== 'rgb') {
|
|
||||||
rgb.value = color.toRgbString();
|
_.forEach(formats, ({ value: valueRef, format }, key) => {
|
||||||
}
|
if (key !== omitLabel) {
|
||||||
if (omit !== 'hsl') {
|
valueRef.value = format(value);
|
||||||
hsl.value = color.toHslString();
|
|
||||||
}
|
|
||||||
if (omit !== 'hwb') {
|
|
||||||
hwb.value = color.toHwbString();
|
|
||||||
}
|
|
||||||
if (omit !== 'cmyk') {
|
|
||||||
cmyk.value = color.toCmykString();
|
|
||||||
}
|
|
||||||
if (omit !== 'lch') {
|
|
||||||
lch.value = color.toLchString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch {
|
|
||||||
//
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onInputUpdated(hex.value, 'hex');
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<c-card>
|
<c-card>
|
||||||
<n-form label-width="100" label-placement="left">
|
<template v-for="({ label, parse, placeholder, validation, type }, key) in formats" :key="key">
|
||||||
<n-form-item label="color picker:">
|
<input-copyable
|
||||||
|
v-if="type === 'text'"
|
||||||
|
v-model:value="formats[key].value.value"
|
||||||
|
:test-id="`input-${key}`"
|
||||||
|
:label="`${label}:`"
|
||||||
|
label-position="left"
|
||||||
|
label-width="100px"
|
||||||
|
label-align="right"
|
||||||
|
:placeholder="placeholder"
|
||||||
|
:validation="validation"
|
||||||
|
raw-text
|
||||||
|
clearable
|
||||||
|
mt-2
|
||||||
|
@update:value="(v:string) => updateColorValue(parse(v), key)"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<n-form-item v-else-if="type === 'color-picker'" :label="`${label}:`" label-width="100" label-placement="left" :show-feedback="false">
|
||||||
<n-color-picker
|
<n-color-picker
|
||||||
v-model:value="hex"
|
v-model:value="formats[key].value.value"
|
||||||
placement="bottom-end"
|
placement="bottom-end"
|
||||||
@update:value="(v: string) => onInputUpdated(v, 'hex')"
|
@update:value="(v:string) => updateColorValue(parse(v), key)"
|
||||||
/>
|
/>
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
<n-form-item label="color name:">
|
</template>
|
||||||
<InputCopyable v-model:value="name" @update:value="(v: string) => onInputUpdated(v, 'name')" />
|
|
||||||
</n-form-item>
|
|
||||||
<n-form-item label="hex:">
|
|
||||||
<InputCopyable v-model:value="hex" @update:value="(v: string) => onInputUpdated(v, 'hex')" />
|
|
||||||
</n-form-item>
|
|
||||||
<n-form-item label="rgb:">
|
|
||||||
<InputCopyable v-model:value="rgb" @update:value="(v: string) => onInputUpdated(v, 'rgb')" />
|
|
||||||
</n-form-item>
|
|
||||||
<n-form-item label="hsl:">
|
|
||||||
<InputCopyable v-model:value="hsl" @update:value="(v: string) => onInputUpdated(v, 'hsl')" />
|
|
||||||
</n-form-item>
|
|
||||||
<n-form-item label="hwb:">
|
|
||||||
<InputCopyable v-model:value="hwb" @update:value="(v: string) => onInputUpdated(v, 'hwb')" />
|
|
||||||
</n-form-item>
|
|
||||||
<n-form-item label="lch:">
|
|
||||||
<InputCopyable v-model:value="lch" @update:value="(v: string) => onInputUpdated(v, 'lch')" />
|
|
||||||
</n-form-item>
|
|
||||||
<n-form-item label="cmyk:">
|
|
||||||
<InputCopyable v-model:value="cmyk" @update:value="(v: string) => onInputUpdated(v, 'cmyk')" />
|
|
||||||
</n-form-item>
|
|
||||||
</n-form>
|
|
||||||
</c-card>
|
</c-card>
|
||||||
</template>
|
</template>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue