From 5653581b8c1c16bcad660d5958a6571a3f0bfa8a Mon Sep 17 00:00:00 2001 From: ln1778 <465410291@qq.com> Date: Mon, 9 Sep 2024 14:27:02 +0800 Subject: [PATCH] 333 --- packages/agColorPicker/index.js | 9 + packages/agColorPicker/src/color.js | 317 ++++++++++++++++++ .../src/components/alpha-slider.vue | 132 ++++++++ .../src/components/hue-slider.vue | 123 +++++++ .../src/components/picker-dropdown.vue | 128 +++++++ .../src/components/predefine.vue | 61 ++++ .../agColorPicker/src/components/sv-panel.vue | 100 ++++++ packages/agColorPicker/src/draggable.js | 36 ++ packages/agColorPicker/src/index.vue | 201 +++++++++++ packages/agDatePicker/src/index.vue | 6 +- packages/agForm/src/index.vue | 49 ++- packages/agUpload/src/index.vue | 8 +- packages/index.js | 4 +- src/App.vue | 28 +- 14 files changed, 1175 insertions(+), 27 deletions(-) create mode 100644 packages/agColorPicker/index.js create mode 100644 packages/agColorPicker/src/color.js create mode 100644 packages/agColorPicker/src/components/alpha-slider.vue create mode 100644 packages/agColorPicker/src/components/hue-slider.vue create mode 100644 packages/agColorPicker/src/components/picker-dropdown.vue create mode 100644 packages/agColorPicker/src/components/predefine.vue create mode 100644 packages/agColorPicker/src/components/sv-panel.vue create mode 100644 packages/agColorPicker/src/draggable.js create mode 100644 packages/agColorPicker/src/index.vue diff --git a/packages/agColorPicker/index.js b/packages/agColorPicker/index.js new file mode 100644 index 0000000..9a44060 --- /dev/null +++ b/packages/agColorPicker/index.js @@ -0,0 +1,9 @@ +import agColorPicker from './src' + +// 为组件提供 install 安装方法,供按需引入 +agColorPicker.install = function (Vue) { + Vue.component(agColorPicker.name, agColorPicker) +} + +// 导出组件 +export default agColorPicker diff --git a/packages/agColorPicker/src/color.js b/packages/agColorPicker/src/color.js new file mode 100644 index 0000000..a392eeb --- /dev/null +++ b/packages/agColorPicker/src/color.js @@ -0,0 +1,317 @@ +const hsv2hsl = function(hue, sat, val) { + return [ + hue, + (sat * val / ((hue = (2 - sat) * val) < 1 ? hue : 2 - hue)) || 0, + hue / 2 + ]; +}; + +// Need to handle 1.0 as 100%, since once it is a number, there is no difference between it and 1 +// +const isOnePointZero = function(n) { + return typeof n === 'string' && n.indexOf('.') !== -1 && parseFloat(n) === 1; +}; + +const isPercentage = function(n) { + return typeof n === 'string' && n.indexOf('%') !== -1; +}; + +// Take input from [0, n] and return it as [0, 1] +const bound01 = function(value, max) { + if (isOnePointZero(value)) value = '100%'; + + const processPercent = isPercentage(value); + value = Math.min(max, Math.max(0, parseFloat(value))); + + // Automatically convert percentage into number + if (processPercent) { + value = parseInt(value * max, 10) / 100; + } + + // Handle floating point rounding errors + if ((Math.abs(value - max) < 0.000001)) { + return 1; + } + + // Convert into [0, 1] range if it isn't already + return (value % max) / parseFloat(max); +}; + +const INT_HEX_MAP = { 10: 'A', 11: 'B', 12: 'C', 13: 'D', 14: 'E', 15: 'F' }; + +const toHex = function({ r, g, b }) { + const hexOne = function(value) { + value = Math.min(Math.round(value), 255); + const high = Math.floor(value / 16); + const low = value % 16; + return '' + (INT_HEX_MAP[high] || high) + (INT_HEX_MAP[low] || low); + }; + + if (isNaN(r) || isNaN(g) || isNaN(b)) return ''; + + return '#' + hexOne(r) + hexOne(g) + hexOne(b); +}; + +const HEX_INT_MAP = { A: 10, B: 11, C: 12, D: 13, E: 14, F: 15 }; + +const parseHexChannel = function(hex) { + if (hex.length === 2) { + return (HEX_INT_MAP[hex[0].toUpperCase()] || +hex[0]) * 16 + (HEX_INT_MAP[hex[1].toUpperCase()] || +hex[1]); + } + + return HEX_INT_MAP[hex[1].toUpperCase()] || +hex[1]; +}; + +const hsl2hsv = function(hue, sat, light) { + sat = sat / 100; + light = light / 100; + let smin = sat; + const lmin = Math.max(light, 0.01); + let sv; + let v; + + light *= 2; + sat *= (light <= 1) ? light : 2 - light; + smin *= lmin <= 1 ? lmin : 2 - lmin; + v = (light + sat) / 2; + sv = light === 0 ? (2 * smin) / (lmin + smin) : (2 * sat) / (light + sat); + + return { + h: hue, + s: sv * 100, + v: v * 100 + }; +}; + +// `rgbToHsv` +// Converts an RGB color value to HSV +// *Assumes:* r, g, and b are contained in the set [0, 255] or [0, 1] +// *Returns:* { h, s, v } in [0,1] +const rgb2hsv = function(r, g, b) { + r = bound01(r, 255); + g = bound01(g, 255); + b = bound01(b, 255); + + const max = Math.max(r, g, b); + const min = Math.min(r, g, b); + let h, s; + let v = max; + + const d = max - min; + s = max === 0 ? 0 : d / max; + + if (max === min) { + h = 0; // achromatic + } else { + switch (max) { + case r: + h = (g - b) / d + (g < b ? 6 : 0); + break; + case g: + h = (b - r) / d + 2; + break; + case b: + h = (r - g) / d + 4; + break; + } + h /= 6; + } + + return { h: h * 360, s: s * 100, v: v * 100 }; +}; + +// `hsvToRgb` +// Converts an HSV color value to RGB. +// *Assumes:* h is contained in [0, 1] or [0, 360] and s and v are contained in [0, 1] or [0, 100] +// *Returns:* { r, g, b } in the set [0, 255] +const hsv2rgb = function(h, s, v) { + h = bound01(h, 360) * 6; + s = bound01(s, 100); + v = bound01(v, 100); + + const i = Math.floor(h); + const f = h - i; + const p = v * (1 - s); + const q = v * (1 - f * s); + const t = v * (1 - (1 - f) * s); + const mod = i % 6; + const r = [v, q, p, p, t, v][mod]; + const g = [t, v, v, q, p, p][mod]; + const b = [p, p, t, v, v, q][mod]; + + return { + r: Math.round(r * 255), + g: Math.round(g * 255), + b: Math.round(b * 255) + }; +}; + +export default class Color { + constructor(options) { + this._hue = 0; + this._saturation = 100; + this._value = 100; + this._alpha = 100; + + this.enableAlpha = false; + this.format = 'hex'; + this.value = ''; + + options = options || {}; + + for (let option in options) { + if (options.hasOwnProperty(option)) { + this[option] = options[option]; + } + } + + this.doOnChange(); + } + + set(prop, value) { + if (arguments.length === 1 && typeof prop === 'object') { + for (let p in prop) { + if (prop.hasOwnProperty(p)) { + this.set(p, prop[p]); + } + } + + return; + } + + this['_' + prop] = value; + this.doOnChange(); + } + + get(prop) { + return this['_' + prop]; + } + + toRgb() { + return hsv2rgb(this._hue, this._saturation, this._value); + } + + fromString(value) { + if (!value) { + this._hue = 0; + this._saturation = 100; + this._value = 100; + + this.doOnChange(); + return; + } + + const fromHSV = (h, s, v) => { + this._hue = Math.max(0, Math.min(360, h)); + this._saturation = Math.max(0, Math.min(100, s)); + this._value = Math.max(0, Math.min(100, v)); + + this.doOnChange(); + }; + + if (value.indexOf('hsl') !== -1) { + const parts = value.replace(/hsla|hsl|\(|\)/gm, '') + .split(/\s|,/g).filter((val) => val !== '').map((val, index) => index > 2 ? parseFloat(val) : parseInt(val, 10)); + + if (parts.length === 4) { + this._alpha = Math.floor(parseFloat(parts[3]) * 100); + } else if (parts.length === 3) { + this._alpha = 100; + } + if (parts.length >= 3) { + const { h, s, v } = hsl2hsv(parts[0], parts[1], parts[2]); + fromHSV(h, s, v); + } + } else if (value.indexOf('hsv') !== -1) { + const parts = value.replace(/hsva|hsv|\(|\)/gm, '') + .split(/\s|,/g).filter((val) => val !== '').map((val, index) => index > 2 ? parseFloat(val) : parseInt(val, 10)); + + if (parts.length === 4) { + this._alpha = Math.floor(parseFloat(parts[3]) * 100); + } else if (parts.length === 3) { + this._alpha = 100; + } + if (parts.length >= 3) { + fromHSV(parts[0], parts[1], parts[2]); + } + } else if (value.indexOf('rgb') !== -1) { + const parts = value.replace(/rgba|rgb|\(|\)/gm, '') + .split(/\s|,/g).filter((val) => val !== '').map((val, index) => index > 2 ? parseFloat(val) : parseInt(val, 10)); + + if (parts.length === 4) { + this._alpha = Math.floor(parseFloat(parts[3]) * 100); + } else if (parts.length === 3) { + this._alpha = 100; + } + if (parts.length >= 3) { + const { h, s, v } = rgb2hsv(parts[0], parts[1], parts[2]); + fromHSV(h, s, v); + } + } else if (value.indexOf('#') !== -1) { + const hex = value.replace('#', '').trim(); + if (!/^(?:[0-9a-fA-F]{3}){1,2}|[0-9a-fA-F]{8}$/.test(hex)) return; + let r, g, b; + + if (hex.length === 3) { + r = parseHexChannel(hex[0] + hex[0]); + g = parseHexChannel(hex[1] + hex[1]); + b = parseHexChannel(hex[2] + hex[2]); + } else if (hex.length === 6 || hex.length === 8) { + r = parseHexChannel(hex.substring(0, 2)); + g = parseHexChannel(hex.substring(2, 4)); + b = parseHexChannel(hex.substring(4, 6)); + } + + if (hex.length === 8) { + this._alpha = Math.floor(parseHexChannel(hex.substring(6)) / 255 * 100); + } else if (hex.length === 3 || hex.length === 6) { + this._alpha = 100; + } + + const { h, s, v } = rgb2hsv(r, g, b); + fromHSV(h, s, v); + } + } + + compare(color) { + return Math.abs(color._hue - this._hue) < 2 && + Math.abs(color._saturation - this._saturation) < 1 && + Math.abs(color._value - this._value) < 1 && + Math.abs(color._alpha - this._alpha) < 1; + } + + doOnChange() { + const { _hue, _saturation, _value, _alpha, format } = this; + + if (this.enableAlpha) { + switch (format) { + case 'hsl': + const hsl = hsv2hsl(_hue, _saturation / 100, _value / 100); + this.value = `hsla(${ _hue }, ${ Math.round(hsl[1] * 100) }%, ${ Math.round(hsl[2] * 100) }%, ${ _alpha / 100})`; + break; + case 'hsv': + this.value = `hsva(${ _hue }, ${ Math.round(_saturation) }%, ${ Math.round(_value) }%, ${ _alpha / 100})`; + break; + default: + const { r, g, b } = hsv2rgb(_hue, _saturation, _value); + this.value = `rgba(${r}, ${g}, ${b}, ${ _alpha / 100 })`; + } + } else { + switch (format) { + case 'hsl': + const hsl = hsv2hsl(_hue, _saturation / 100, _value / 100); + this.value = `hsl(${ _hue }, ${ Math.round(hsl[1] * 100) }%, ${ Math.round(hsl[2] * 100) }%)`; + break; + case 'hsv': + this.value = `hsv(${ _hue }, ${ Math.round(_saturation) }%, ${ Math.round(_value) }%)`; + break; + case 'rgb': + const { r, g, b } = hsv2rgb(_hue, _saturation, _value); + this.value = `rgb(${r}, ${g}, ${b})`; + break; + default: + this.value = toHex(hsv2rgb(_hue, _saturation, _value)); + } + } + } +}; diff --git a/packages/agColorPicker/src/components/alpha-slider.vue b/packages/agColorPicker/src/components/alpha-slider.vue new file mode 100644 index 0000000..efeb87f --- /dev/null +++ b/packages/agColorPicker/src/components/alpha-slider.vue @@ -0,0 +1,132 @@ + + + diff --git a/packages/agColorPicker/src/components/hue-slider.vue b/packages/agColorPicker/src/components/hue-slider.vue new file mode 100644 index 0000000..c93e42c --- /dev/null +++ b/packages/agColorPicker/src/components/hue-slider.vue @@ -0,0 +1,123 @@ + + + diff --git a/packages/agColorPicker/src/components/picker-dropdown.vue b/packages/agColorPicker/src/components/picker-dropdown.vue new file mode 100644 index 0000000..9de2199 --- /dev/null +++ b/packages/agColorPicker/src/components/picker-dropdown.vue @@ -0,0 +1,128 @@ + + + diff --git a/packages/agColorPicker/src/components/predefine.vue b/packages/agColorPicker/src/components/predefine.vue new file mode 100644 index 0000000..1387c48 --- /dev/null +++ b/packages/agColorPicker/src/components/predefine.vue @@ -0,0 +1,61 @@ + + + \ No newline at end of file diff --git a/packages/agColorPicker/src/components/sv-panel.vue b/packages/agColorPicker/src/components/sv-panel.vue new file mode 100644 index 0000000..66676a6 --- /dev/null +++ b/packages/agColorPicker/src/components/sv-panel.vue @@ -0,0 +1,100 @@ + + + diff --git a/packages/agColorPicker/src/draggable.js b/packages/agColorPicker/src/draggable.js new file mode 100644 index 0000000..339a485 --- /dev/null +++ b/packages/agColorPicker/src/draggable.js @@ -0,0 +1,36 @@ +import Vue from 'vue'; +let isDragging = false; + +export default function(element, options) { + if (Vue.prototype.$isServer) return; + const moveFn = function(event) { + if (options.drag) { + options.drag(event); + } + }; + const upFn = function(event) { + document.removeEventListener('mousemove', moveFn); + document.removeEventListener('mouseup', upFn); + document.onselectstart = null; + document.ondragstart = null; + + isDragging = false; + + if (options.end) { + options.end(event); + } + }; + element.addEventListener('mousedown', function(event) { + if (isDragging) return; + document.onselectstart = function() { return false; }; + document.ondragstart = function() { return false; }; + + document.addEventListener('mousemove', moveFn); + document.addEventListener('mouseup', upFn); + isDragging = true; + + if (options.start) { + options.start(event); + } + }); +} diff --git a/packages/agColorPicker/src/index.vue b/packages/agColorPicker/src/index.vue new file mode 100644 index 0000000..1d70c21 --- /dev/null +++ b/packages/agColorPicker/src/index.vue @@ -0,0 +1,201 @@ + + + + \ No newline at end of file diff --git a/packages/agDatePicker/src/index.vue b/packages/agDatePicker/src/index.vue index 6d2c352..1a2e775 100644 --- a/packages/agDatePicker/src/index.vue +++ b/packages/agDatePicker/src/index.vue @@ -62,9 +62,9 @@ export default { let config = {}; if (this.range) { config = { - "start-placeholder":this.$attrs.startplaceholder||"开始日期", - "end-placeholder":this.$attrs.endplaceholder|| "结束日期", - "range-separator":this.$attrs.rangeseparator||"-", + "start-placeholder":this.$attrs.startPlaceholder||"开始日期", + "end-placeholder":this.$attrs.endPlaceholder|| "结束日期", + "range-separator":this.$attrs.rangeSeparator||"-", "picker-options": { disabledDate(time) { return ( diff --git a/packages/agForm/src/index.vue b/packages/agForm/src/index.vue index 0efa287..d143154 100644 --- a/packages/agForm/src/index.vue +++ b/packages/agForm/src/index.vue @@ -1,16 +1,40 @@ - +
只能上传jpg/png文件,且不超过500kb
- +