-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement basic colour conversion utility (#33)
* implement basic colour conversion utility * Add debounce and clipboard buttons * Update cypress tests
- Loading branch information
Showing
11 changed files
with
178 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ColorTranslator } from 'colortranslator' | ||
|
||
export function ColorConverter(colorCode: string) { | ||
const translator: ColorTranslator = new ColorTranslator(colorCode) | ||
|
||
return { | ||
rgb: translator.RGB, | ||
hex: translator.HEX, | ||
hsl: translator.HSL, | ||
cmyk: translator.CMYK, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<template> | ||
<div class="border-2 space-y-4 flex flex-col text-right items-center text-purple-500"> | ||
<div class="flex mb-12"> | ||
<div class="p-2 w-48"> | ||
<p class="text-xl font-bold">Enter color code:</p> | ||
<p class="text-xs">Accept RGB, HEX, CMYK, HSL</p> | ||
</div> | ||
<div class="flex flex-col overflow-ellipsis"> | ||
<input type="text" v-model="colorInput" v-debounce:300ms="convert" | ||
class="border rounded border-black p-1 w-96 text-black text-xl"> | ||
<p v-if="errorMessage" class="text-red-500 text-sm text-left">{{ errorMessage }}</p> | ||
</div> | ||
</div> | ||
<div class="flex"> | ||
<p class="text-lg w-48 p-2">Color:</p> | ||
<input type="text" class="border rounded border-gray-300 p-1 w-96" :style="{ backgroundColor: hex }" readonly> | ||
</div> | ||
<div class="flex"> | ||
<p class="text-lg w-48 p-2">RGB:</p> | ||
<input type="text" id="rgb" v-model="rgb" class="border rounded border-gray-300 p-1 w-96 text-black" readonly> | ||
<button class="bg-purple-500 hover:bg-purple-600 text-white text-sm leading-6 font-medium mx-4 rounded-lg p-2" | ||
type="button" @click="copyToClipboard(rgb)">Copy to Clipboard | ||
</button> | ||
</div> | ||
<div class="flex"> | ||
<p class="text-lg w-48 p-2">HEX:</p> | ||
<input type="text" id="hex" v-model="hex" class="border rounded border-gray-300 p-1 w-96 text-black" readonly> | ||
<button class="bg-purple-500 hover:bg-purple-600 text-white text-sm leading-6 font-medium mx-4 rounded-lg p-2" | ||
type="button" @click="copyToClipboard(hex)">Copy to Clipboard | ||
</button> | ||
</div> | ||
<div class="flex"> | ||
<p class="text-lg w-48 p-2">CMYK:</p> | ||
<input type="text" id="cmyk" v-model="cmyk" class="border rounded border-gray-300 p-1 w-96 text-black" readonly> | ||
<button class="bg-purple-500 hover:bg-purple-600 text-white text-sm leading-6 font-medium mx-4 rounded-lg p-2" | ||
type="button" @click="copyToClipboard(cmyk)">Copy to Clipboard | ||
</button> | ||
</div> | ||
<div class="flex"> | ||
<p class="text-lg w-48 p-2">HSL:</p> | ||
<input type="text" id="hsl" v-model="hsl" class="border rounded border-gray-300 p-1 w-96 text-black" readonly> | ||
<button class="bg-purple-500 hover:bg-purple-600 text-white text-sm leading-6 font-medium mx-4 rounded-lg p-2" | ||
type="button" @click="copyToClipboard(hsl)">Copy to Clipboard | ||
</button> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts" setup> | ||
import { ref } from 'vue' | ||
import { ColorConverter } from '@/utilities/ColorConverter' | ||
import { copyToClipboard } from '@/helpers/CopyToClipboard' | ||
const colorInput = ref<string>('') | ||
const rgb = ref<string>('') | ||
const hex = ref<string>('') | ||
const hsl = ref<string>('') | ||
const cmyk = ref<string>('') | ||
let errorMessage = ref<string | null>('') | ||
function convert() { | ||
try { | ||
const convertedColorCodes = ColorConverter(colorInput.value.toLowerCase()) | ||
errorMessage.value = null | ||
rgb.value = convertedColorCodes.rgb | ||
hex.value = convertedColorCodes.hex | ||
hsl.value = convertedColorCodes.hsl | ||
cmyk.value = convertedColorCodes.cmyk | ||
} catch (e: any) { | ||
if (colorInput.value) { | ||
errorMessage.value = e.message | ||
} | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { ColorConverter } from '@/utilities/ColorConverter' | ||
import { describe, expect, it } from 'vitest' | ||
|
||
describe('ColorConverter', () => { | ||
it('converts RGB color codes', () => { | ||
const RGB: string = 'rgb(255, 255, 255)' | ||
|
||
const converted = ColorConverter(RGB) | ||
|
||
expect(converted.hex).toBe('#FFFFFF') | ||
expect(converted.hsl).toBe('hsl(0, 0%, 100%)') | ||
expect(converted.cmyk).toBe('device-cmyk(0%, 0%, 0%, 0%)') | ||
}) | ||
|
||
it('converts HEX color codes', () => { | ||
const HEX: string = '#FFFFFF' | ||
|
||
const converted = ColorConverter(HEX) | ||
|
||
expect(converted.rgb).toBe('rgb(255 255 255)') | ||
expect(converted.hsl).toBe('hsl(0 0% 100%)') | ||
expect(converted.cmyk).toBe('device-cmyk(0% 0% 0% 0%)') | ||
}) | ||
|
||
it('converts HSL color codes', () => { | ||
const HSL= 'hsl(288, 8%, 55%)' | ||
|
||
const converted = ColorConverter(HSL) | ||
|
||
expect(converted.rgb).toBe('rgb(145.758, 131.07, 149.43)') | ||
expect(converted.hex).toBe('#928395') | ||
expect(converted.cmyk).toBe('device-cmyk(2.457338%, 12.286689%, 0%, 41.4%)') | ||
}) | ||
|
||
it('converts CMYK color codes', () => { | ||
const CMYK= 'cmyk(100%, 26%, 0%, 0%)' | ||
|
||
const converted = ColorConverter(CMYK) | ||
|
||
expect(converted.rgb).toBe('rgb(0, 188.7, 255)') | ||
expect(converted.hex).toBe('#00BDFF') | ||
expect(converted.hsl).toBe('hsl(195.6, 100%, 50%)') | ||
}) | ||
}) |