From 2ec8ed5464e2e6df7768b59a63e73097aac9be46 Mon Sep 17 00:00:00 2001 From: Conor_Mack Date: Sun, 31 May 2020 20:55:51 +0100 Subject: [PATCH] Palette, Slider Components and Util Updates --- .../Colorpicker/Colorpicker.svelte | 32 ++++ .../userInterface/Colorpicker/Palette.svelte | 49 ++++++ .../userInterface/Colorpicker/Slider.svelte | 87 +++++++++++ .../userInterface/Colorpicker/drag.js | 23 +++ .../userInterface/Colorpicker/index.js | 2 + .../userInterface/Colorpicker/utils.js | 146 ++++++++++++++++++ .../Colorpicker/utils/HSVAColor.js | 66 -------- .../userInterface/Colorpicker/utils/utils.js | 108 ------------- 8 files changed, 339 insertions(+), 174 deletions(-) create mode 100644 packages/builder/src/components/userInterface/Colorpicker/Palette.svelte create mode 100644 packages/builder/src/components/userInterface/Colorpicker/Slider.svelte create mode 100644 packages/builder/src/components/userInterface/Colorpicker/drag.js create mode 100644 packages/builder/src/components/userInterface/Colorpicker/index.js create mode 100644 packages/builder/src/components/userInterface/Colorpicker/utils.js delete mode 100644 packages/builder/src/components/userInterface/Colorpicker/utils/HSVAColor.js delete mode 100644 packages/builder/src/components/userInterface/Colorpicker/utils/utils.js diff --git a/packages/builder/src/components/userInterface/Colorpicker/Colorpicker.svelte b/packages/builder/src/components/userInterface/Colorpicker/Colorpicker.svelte index e69de29bb2..db70f3ad64 100644 --- a/packages/builder/src/components/userInterface/Colorpicker/Colorpicker.svelte +++ b/packages/builder/src/components/userInterface/Colorpicker/Colorpicker.svelte @@ -0,0 +1,32 @@ + + + + +
+ + + + (h = hue.detail)} /> + + (a = alpha.detail)} /> +
diff --git a/packages/builder/src/components/userInterface/Colorpicker/Palette.svelte b/packages/builder/src/components/userInterface/Colorpicker/Palette.svelte new file mode 100644 index 0000000000..4a4decfda8 --- /dev/null +++ b/packages/builder/src/components/userInterface/Colorpicker/Palette.svelte @@ -0,0 +1,49 @@ + + + + +
+
+
diff --git a/packages/builder/src/components/userInterface/Colorpicker/Slider.svelte b/packages/builder/src/components/userInterface/Colorpicker/Slider.svelte new file mode 100644 index 0000000000..82e00baa93 --- /dev/null +++ b/packages/builder/src/components/userInterface/Colorpicker/Slider.svelte @@ -0,0 +1,87 @@ + + + + +
handleClick(event.clientX)} + class="color-format-slider" + class:hue={type === 'hue'} + class:alpha={type === 'alpha'}> +
handleClick(e.detail)} + class="slider-thumb" + {style} /> +
diff --git a/packages/builder/src/components/userInterface/Colorpicker/drag.js b/packages/builder/src/components/userInterface/Colorpicker/drag.js new file mode 100644 index 0000000000..eb15c8d4fb --- /dev/null +++ b/packages/builder/src/components/userInterface/Colorpicker/drag.js @@ -0,0 +1,23 @@ +export default function(node) { + function handleMouseDown() { + window.addEventListener('mousemove', handleMouseMove); + window.addEventListener('mouseup', handleMouseUp); + } + + function handleMouseMove(event) { + let mouseX = event.clientX; + console.log(mouseX); + node.dispatchEvent( + new CustomEvent('drag', { + detail: mouseX + }) + ); + } + + function handleMouseUp() { + window.removeEventListener('mousedown', handleMouseDown); + window.removeEventListener('mousemove', handleMouseMove); + } + + node.addEventListener('mousedown', handleMouseDown); +} diff --git a/packages/builder/src/components/userInterface/Colorpicker/index.js b/packages/builder/src/components/userInterface/Colorpicker/index.js new file mode 100644 index 0000000000..ad8187a09b --- /dev/null +++ b/packages/builder/src/components/userInterface/Colorpicker/index.js @@ -0,0 +1,2 @@ +import Colorpicker from './Colorpicker.svelte'; +export default Colorpicker; diff --git a/packages/builder/src/components/userInterface/Colorpicker/utils.js b/packages/builder/src/components/userInterface/Colorpicker/utils.js new file mode 100644 index 0000000000..98bb96d259 --- /dev/null +++ b/packages/builder/src/components/userInterface/Colorpicker/utils.js @@ -0,0 +1,146 @@ +export const getRgbaValues = (rgbaString) => rgbaString.replace(/[a-z()\s]/gi, '').split(','); + +export const getHexaValues = (hexString) => hexString.match(/[A-F]{2}|[A-F]\d{1}|\d{2}|\d[A-F]/gi); + +export const isValidRgba = (rgba) => { + let [ r, g, b, a = 1 ] = rgba; + + let isValidLengthRange = rgba.length === 3 || rgba.length === 4; + let isValidColorRange = [ r, g, b ].every((v) => v >= 0 && v <= 255); + let isValidAlphaRange = rgba.length === 3 || (a >= 0 && a <= 1); + + return isValidLengthRange && isValidColorRange && isValidAlphaRange; +}; + +export const determineColorType = (color) => { + let hsva = []; + if (color.startsWith('#')) { + let [ rHex, gHex, bHex, aHex ] = getHexaValues(color); + hsva = hexaToHSVA([ rHex, gHex, bHex ], aHex); + } else if (color.startsWith('rgb')) { + let rgba = getRgbaValues(color); + hsva = rgbaToHSVA(rgba); + } +}; + +export const getHSLA = (hsv, a) => { + const [ h, s, l ] = hsvToHSL(hsv); + return `hsla(${h}, ${s}, ${l}, ${a})`; +}; + +export const hexaToHSVA = (hex, alpha = 'FF') => { + const rgba = hex.map((v) => parseInt(v, 16)).concat((parseInt(alpha, 16) / 255).toFixed(1)); + return rgbaToHSVA(rgba); +}; + +export const rgbaToHSVA = (rgba) => { + if (isValidRgba(rgba)) { + const [ r, g, b, a = '1' ] = rgba; + let hsv = rgbToHSV([ r, g, b ]); + return [ ...hsv, a ]; + } +}; + +export const hsvaToHexa = () => { + const [ r, g, b, a ] = hsvaToRgba(); + const hexa = [ r, g, b ].map((v) => v.toString(16)).concat((a * 255).toFixed(1).toString(16)); + return hexa; +}; + +export const hsvaToRgba = (h, s, v, a) => { + let rgb = _hsvToRgb([ h, s, v ]); + return [ ...rgb, a ]; +}; + +//Credit : https://github.com/Qix-/color-convert +export const _rgbToHSV = (rgb) => { + let rdif; + let gdif; + let bdif; + let h; + let s; + + const r = rgb[0] / 255; + const g = rgb[1] / 255; + const b = rgb[2] / 255; + const v = Math.max(r, g, b); + const diff = v - Math.min(r, g, b); + const diffc = function(c) { + return (v - c) / 6 / diff + 1 / 2; + }; + + if (diff === 0) { + h = 0; + s = 0; + } else { + s = diff / v; + rdif = diffc(r); + gdif = diffc(g); + bdif = diffc(b); + + if (r === v) { + h = bdif - gdif; + } else if (g === v) { + h = 1 / 3 + rdif - bdif; + } else if (b === v) { + h = 2 / 3 + gdif - rdif; + } + + if (h < 0) { + h += 1; + } else if (h > 1) { + h -= 1; + } + } + + const hsvResult = [ h * 360, s * 100, v * 100 ].map((v) => v.toFixed(0)); + return hsvResult; +}; + +//Credit : https://github.com/Qix-/color-convert +export const _hsvToRgb = (hsv) => { + const h = hsv[0] / 60; + const s = hsv[1] / 100; + let v = hsv[2] / 100; + const hi = Math.floor(h) % 6; + + const f = h - Math.floor(h); + const p = 255 * v * (1 - s); + const q = 255 * v * (1 - s * f); + const t = 255 * v * (1 - s * (1 - f)); + v *= 255; + + switch (hi) { + case 0: + return [ v, t, p ]; + case 1: + return [ q, v, p ]; + case 2: + return [ p, v, t ]; + case 3: + return [ p, q, v ]; + case 4: + return [ t, p, v ]; + case 5: + return [ v, p, q ]; + } +}; + +//Credit : https://github.com/Qix-/color-convert +export const _hsvToHSL = (hsv) => { + const h = hsv[0]; + const s = hsv[1] / 100; + const v = hsv[2] / 100; + const vmin = Math.max(v, 0.01); + let sl; + let l; + + l = (2 - s) * v; + const lmin = (2 - s) * vmin; + sl = s * vmin; + sl /= lmin <= 1 ? lmin : 2 - lmin; + sl = sl || 0; + l /= 2; + + return [ h, sl * 100, l * 100 ]; +}; diff --git a/packages/builder/src/components/userInterface/Colorpicker/utils/HSVAColor.js b/packages/builder/src/components/userInterface/Colorpicker/utils/HSVAColor.js deleted file mode 100644 index e39e700903..0000000000 --- a/packages/builder/src/components/userInterface/Colorpicker/utils/HSVAColor.js +++ /dev/null @@ -1,66 +0,0 @@ -import { - getHexaValues, - getRgbaValues, - hsvToHSL, - isValidRgba, - rgbToHSV, - hsvToRgb, -} from "./utils" - -export class HSVAColor { - h = 0 - s = 0 - v = 0 - a = 1 - - constructor(color) { - if (color.startsWith("#")) { - let [rHex, gHex, bHex, aHex] = getHexaValues(color) - this.hexaToHSVA([rHex, gHex, bHex], aHex) - } else if (color.startsWith("rgb")) { - let rgba = getRgbaValues(color) - this.rgbaToHSVA(rgba) - } - } - - setHSVA([h, s, v, a]) { - this.h = h - this.s = s - this.v = v - this.a = a - } - - getHSLA() { - const [h, s, l] = hsvToHSL([this.h, this.s, this.v]) - return `hsla(${h}, ${s}, ${l}, ${this.a})` - } - - hexaToHSVA(hex, alpha = "FF") { - const rgba = hex - .map(v => parseInt(v, 16)) - .concat((parseInt(alpha, 16) / 255).toFixed(1)) - this.rgbaToHSVA(rgba) - } - - rgbaToHSVA(rgba) { - if (isValidRgba(rgba)) { - const [r, g, b, a = "1"] = rgba - let hsv = rgbToHSV([r, g, b]) - this.setHSVA([...hsv, a]) - } - } - - hsvaToHexa() { - const [r, g, b, a] = this.hsvaToRgba() - const hexa = [r, g, b] - .map(v => v.toString(16)) - .concat((a * 255).toFixed(1).toString(16)) - return hexa - } - - hsvaToRgba() { - const { h, s, v, a } = this - let rgb = hsvToRgb([h, s, v]) - return [...rgb, a] - } -} diff --git a/packages/builder/src/components/userInterface/Colorpicker/utils/utils.js b/packages/builder/src/components/userInterface/Colorpicker/utils/utils.js deleted file mode 100644 index 7412c42e27..0000000000 --- a/packages/builder/src/components/userInterface/Colorpicker/utils/utils.js +++ /dev/null @@ -1,108 +0,0 @@ -export const getRgbaValues = rgbaString => - rgbaString.replace(/[a-z\(\)\s]/gi, "").split(",") - -export const getHexaValues = hexString => - hexString.match(/[A-F]{2}|[A-F]\d{1}|\d{2}|\d[A-F]/gi) - -export const isValidRgba = rgba => { - let [r, g, b, a = 1] = rgba - - let isValidLengthRange = rgba.length === 3 || rgba.length === 4 - let isValidColorRange = [r, g, b].every(v => v >= 0 && v <= 255) - let isValidAlphaRange = rgba.length === 3 || (a >= 0 && a <= 1) - - return isValidLengthRange && isValidColorRange && isValidAlphaRange -} - -//Credit : https://github.com/Qix-/color-convert -export const rgbToHSV = rgb => { - let rdif - let gdif - let bdif - let h - let s - - const r = rgb[0] / 255 - const g = rgb[1] / 255 - const b = rgb[2] / 255 - const v = Math.max(r, g, b) - const diff = v - Math.min(r, g, b) - const diffc = function(c) { - return (v - c) / 6 / diff + 1 / 2 - } - - if (diff === 0) { - h = 0 - s = 0 - } else { - s = diff / v - rdif = diffc(r) - gdif = diffc(g) - bdif = diffc(b) - - if (r === v) { - h = bdif - gdif - } else if (g === v) { - h = 1 / 3 + rdif - bdif - } else if (b === v) { - h = 2 / 3 + gdif - rdif - } - - if (h < 0) { - h += 1 - } else if (h > 1) { - h -= 1 - } - } - - const hsvResult = [h * 360, s * 100, v * 100].map(v => v.toFixed(0)) - return hsvResult -} - -//Credit : https://github.com/Qix-/color-convert -export const hsvToRgb = hsv => { - const h = hsv[0] / 60 - const s = hsv[1] / 100 - let v = hsv[2] / 100 - const hi = Math.floor(h) % 6 - - const f = h - Math.floor(h) - const p = 255 * v * (1 - s) - const q = 255 * v * (1 - s * f) - const t = 255 * v * (1 - s * (1 - f)) - v *= 255 - - switch (hi) { - case 0: - return [v, t, p] - case 1: - return [q, v, p] - case 2: - return [p, v, t] - case 3: - return [p, q, v] - case 4: - return [t, p, v] - case 5: - return [v, p, q] - } -} - -//Credit : https://github.com/Qix-/color-convert -export const hsvToHSL = hsv => { - const h = hsv[0] - const s = hsv[1] / 100 - const v = hsv[2] / 100 - const vmin = Math.max(v, 0.01) - let sl - let l - - l = (2 - s) * v - const lmin = (2 - s) * vmin - sl = s * vmin - sl /= lmin <= 1 ? lmin : 2 - lmin - sl = sl || 0 - l /= 2 - - return [h, sl * 100, l * 100] -}