element-plus
Version:
A Component Library for Vue 3
1 lines • 4.48 kB
Source Map (JSON)
{"version":3,"file":"color.mjs","names":[],"sources":["../../../../../../../packages/components/color-picker-panel/src/utils/color.ts"],"sourcesContent":["import { TinyColor } from '@ctrl/tinycolor'\nimport { hasOwn } from '@element-plus/utils'\n\nimport type { ColorFormats } from '@ctrl/tinycolor'\n\ninterface ColorOptions {\n enableAlpha: boolean\n format: string\n value?: string | null\n}\n\nexport default class Color {\n private _hue = 0\n private _saturation = 100\n private _value = 100\n private _alpha = 100\n private _tiny = new TinyColor()\n private _isValid = false\n public enableAlpha = false\n public format = ''\n public value = ''\n public selected?: boolean\n\n constructor(options: Partial<ColorOptions> = {}) {\n for (const option in options) {\n if (hasOwn(options, option)) {\n this[option] = options[option]\n }\n }\n if (options.value) {\n this.fromString(options.value)\n } else {\n this.doOnChange()\n }\n }\n\n set(prop: { [key: string]: any } | any, value?: number) {\n if (arguments.length === 1 && typeof prop === 'object') {\n for (const p in prop) {\n if (hasOwn(prop, p)) {\n this.set(p, prop[p])\n }\n }\n\n return\n }\n ;(this as any)[`_${prop}`] = value\n this._isValid = true\n this.doOnChange()\n }\n\n get(prop: string) {\n if (['hue', 'saturation', 'value', 'alpha'].includes(prop)) {\n return Math.round((this as any)[`_${prop}`])\n }\n return (this as any)[`_${prop}`]\n }\n\n toRgb() {\n return this._isValid ? this._tiny.toRgb() : { r: 255, g: 255, b: 255, a: 0 }\n }\n\n fromString(value: string) {\n const color = new TinyColor(value)\n this._isValid = color.isValid\n if (color.isValid) {\n const { h, s, v, a } = color.toHsv()\n this._hue = h\n this._saturation = s * 100\n this._value = v * 100\n this._alpha = a * 100\n } else {\n this._hue = 0\n this._saturation = 100\n this._value = 100\n this._alpha = 100\n }\n this.doOnChange()\n }\n\n clear() {\n this._isValid = false\n this.value = ''\n this._hue = 0\n this._saturation = 100\n this._value = 100\n this._alpha = 100\n }\n\n compare(color: this) {\n const compareColor = new TinyColor({\n h: color._hue,\n s: color._saturation / 100,\n v: color._value / 100,\n a: color._alpha / 100,\n })\n return this._tiny.equals(compareColor)\n }\n\n doOnChange() {\n const { _hue, _saturation, _value, _alpha, format, enableAlpha } = this\n let _format = format || (enableAlpha ? 'rgb' : 'hex')\n if (format === 'hex' && enableAlpha) {\n _format = 'hex8'\n }\n this._tiny = new TinyColor({\n h: _hue,\n s: _saturation / 100,\n v: _value / 100,\n a: _alpha / 100,\n })\n this.value = this._isValid\n ? this._tiny.toString(_format as ColorFormats)\n : ''\n }\n}\n"],"mappings":";;;;AAWA,IAAqB,QAArB,MAA2B;CAYzB,YAAY,UAAiC,EAAE,EAAE;cAXlC;qBACO;gBACL;gBACA;eACD,IAAI,WAAW;kBACZ;qBACE;gBACL;eACD;AAIb,OAAK,MAAM,UAAU,QACnB,KAAI,OAAO,SAAS,OAAO,CACzB,MAAK,UAAU,QAAQ;AAG3B,MAAI,QAAQ,MACV,MAAK,WAAW,QAAQ,MAAM;MAE9B,MAAK,YAAY;;CAIrB,IAAI,MAAoC,OAAgB;AACtD,MAAI,UAAU,WAAW,KAAK,OAAO,SAAS,UAAU;AACtD,QAAK,MAAM,KAAK,KACd,KAAI,OAAO,MAAM,EAAE,CACjB,MAAK,IAAI,GAAG,KAAK,GAAG;AAIxB;;AAED,EAAC,KAAa,IAAI,UAAU;AAC7B,OAAK,WAAW;AAChB,OAAK,YAAY;;CAGnB,IAAI,MAAc;AAChB,MAAI;GAAC;GAAO;GAAc;GAAS;GAAQ,CAAC,SAAS,KAAK,CACxD,QAAO,KAAK,MAAO,KAAa,IAAI,QAAQ;AAE9C,SAAQ,KAAa,IAAI;;CAG3B,QAAQ;AACN,SAAO,KAAK,WAAW,KAAK,MAAM,OAAO,GAAG;GAAE,GAAG;GAAK,GAAG;GAAK,GAAG;GAAK,GAAG;GAAG;;CAG9E,WAAW,OAAe;EACxB,MAAM,QAAQ,IAAI,UAAU,MAAM;AAClC,OAAK,WAAW,MAAM;AACtB,MAAI,MAAM,SAAS;GACjB,MAAM,EAAE,G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