node-red-contrib-knx-ultimate 2.1.63 → 2.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +12 -12
- package/CHANGELOG.md +17 -1
- package/KNXEngine/package.json +1 -1
- package/nodes/hue-config.html +2 -2
- package/nodes/hue-config.js +81 -39
- package/nodes/knxUltimate-config.js +158 -156
- package/nodes/knxUltimateHueBattery.html +2 -2
- package/nodes/knxUltimateHueButton.html +1 -1
- package/nodes/knxUltimateHueButton.js +152 -116
- package/nodes/knxUltimateHueLight.html +830 -781
- package/nodes/knxUltimateHueLight.js +145 -149
- package/nodes/knxUltimateHueLightSensor.html +1 -1
- package/nodes/knxUltimateHueLightSensor.js +2 -2
- package/nodes/knxUltimateHueMotion.html +3 -4
- package/nodes/knxUltimateHueMotion.js +73 -61
- package/nodes/knxUltimateHueScene.html +1 -1
- package/nodes/knxUltimateHueScene.js +1 -1
- package/nodes/knxUltimateHueTapDial.html +1 -1
- package/nodes/knxUltimateHueTapDial.js +1 -2
- package/nodes/knxUltimateHueTemperatureSensor.html +2 -2
- package/nodes/knxUltimateLoadControl.html +1 -1
- package/nodes/knxUltimateViewer.html +28 -0
- package/nodes/utils/hueEngine.js +5 -25
- package/package.json +26 -5
- package/nodes/utils/iro.js +0 -1835
package/nodes/utils/iro.js
DELETED
|
@@ -1,1835 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* iro.js v5.5.2
|
|
3
|
-
* 2016-2021 James Daniel
|
|
4
|
-
* Licensed under MPL 2.0
|
|
5
|
-
* github.com/jaames/iro.js
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
(function (global, factory) {
|
|
9
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
10
|
-
typeof define === 'function' && define.amd ? define(factory) :
|
|
11
|
-
(global = global || self, global.iro = factory());
|
|
12
|
-
}(this, function () {
|
|
13
|
-
'use strict';
|
|
14
|
-
|
|
15
|
-
var n, u, t, i, r, o, f = {}, e = [], c = /acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|^--/i; function s(n, l) { for (var u in l) { n[u] = l[u]; } return n } function a(n) { var l = n.parentNode; l && l.removeChild(n); } function h(n, l, u) { var t, i, r, o, f = arguments; if (l = s({}, l), arguments.length > 3) { for (u = [u], t = 3; t < arguments.length; t++) { u.push(f[t]); } } if (null != u && (l.children = u), null != n && null != n.defaultProps) { for (i in n.defaultProps) { void 0 === l[i] && (l[i] = n.defaultProps[i]); } } return o = l.key, null != (r = l.ref) && delete l.ref, null != o && delete l.key, v(n, l, o, r) } function v(l, u, t, i) { var r = { type: l, props: u, key: t, ref: i, __k: null, __p: null, __b: 0, __e: null, l: null, __c: null, constructor: void 0 }; return n.vnode && n.vnode(r), r } function d(n) { return n.children } function y(n) { if (null == n || "boolean" == typeof n) { return null; } if ("string" == typeof n || "number" == typeof n) { return v(null, n, null, null); } if (null != n.__e || null != n.__c) { var l = v(n.type, n.props, n.key, null); return l.__e = n.__e, l } return n } function m(n, l) { this.props = n, this.context = l; } function w(n, l) { if (null == l) { return n.__p ? w(n.__p, n.__p.__k.indexOf(n) + 1) : null; } for (var u; l < n.__k.length; l++) { if (null != (u = n.__k[l]) && null != u.__e) { return u.__e; } } return "function" == typeof n.type ? w(n) : null } function g(n) { var l, u; if (null != (n = n.__p) && null != n.__c) { for (n.__e = n.__c.base = null, l = 0; l < n.__k.length; l++) { if (null != (u = n.__k[l]) && null != u.__e) { n.__e = n.__c.base = u.__e; break } } return g(n) } } function k(l) { (!l.__d && (l.__d = !0) && 1 === u.push(l) || i !== n.debounceRendering) && (i = n.debounceRendering, (n.debounceRendering || t)(_)); } function _() { var n, l, t, i, r, o, f, e; for (u.sort(function (n, l) { return l.__v.__b - n.__v.__b }); n = u.pop();) { n.__d && (t = void 0, i = void 0, o = (r = (l = n).__v).__e, f = l.__P, e = l.u, l.u = !1, f && (t = [], i = $(f, r, s({}, r), l.__n, void 0 !== f.ownerSVGElement, null, t, e, null == o ? w(r) : o), j(t, r), i != o && g(r))); } } function b(n, l, u, t, i, r, o, c, s) { var h, v, p, d, y, m, g, k = u && u.__k || e, _ = k.length; if (c == f && (c = null != r ? r[0] : _ ? w(u, 0) : null), h = 0, l.__k = x(l.__k, function (u) { if (null != u) { if (u.__p = l, u.__b = l.__b + 1, null === (p = k[h]) || p && u.key == p.key && u.type === p.type) { k[h] = void 0; } else { for (v = 0; v < _; v++) { if ((p = k[v]) && u.key == p.key && u.type === p.type) { k[v] = void 0; break } p = null; } } if (d = $(n, u, p = p || f, t, i, r, o, null, c, s), (v = u.ref) && p.ref != v && (g || (g = [])).push(v, u.__c || d, u), null != d) { if (null == m && (m = d), null != u.l) { d = u.l, u.l = null; } else if (r == p || d != c || null == d.parentNode) { n: if (null == c || c.parentNode !== n) { n.appendChild(d); } else { for (y = c, v = 0; (y = y.nextSibling) && v < _; v += 2) { if (y == d) { break n; } } n.insertBefore(d, c); } "option" == l.type && (n.value = ""); } c = d.nextSibling, "function" == typeof l.type && (l.l = d); } } return h++, u }), l.__e = m, null != r && "function" != typeof l.type) { for (h = r.length; h--;) { null != r[h] && a(r[h]); } } for (h = _; h--;) { null != k[h] && D(k[h], k[h]); } if (g) { for (h = 0; h < g.length; h++) { A(g[h], g[++h], g[++h]); } } } function x(n, l, u) { if (null == u && (u = []), null == n || "boolean" == typeof n) { l && u.push(l(null)); } else if (Array.isArray(n)) { for (var t = 0; t < n.length; t++) { x(n[t], l, u); } } else { u.push(l ? l(y(n)) : n); } return u } function C(n, l, u, t, i) { var r; for (r in u) { r in l || N(n, r, null, u[r], t); } for (r in l) { i && "function" != typeof l[r] || "value" === r || "checked" === r || u[r] === l[r] || N(n, r, l[r], u[r], t); } } function P(n, l, u) { "-" === l[0] ? n.setProperty(l, u) : n[l] = "number" == typeof u && !1 === c.test(l) ? u + "px" : null == u ? "" : u; } function N(n, l, u, t, i) { var r, o, f, e, c; if ("key" === (l = i ? "className" === l ? "class" : l : "class" === l ? "className" : l) || "children" === l); else if ("style" === l) { if (r = n.style, "string" == typeof u) { r.cssText = u; } else { if ("string" == typeof t && (r.cssText = "", t = null), t) { for (o in t) { u && o in u || P(r, o, ""); } } if (u) { for (f in u) { t && u[f] === t[f] || P(r, f, u[f]); } } } } else { "o" === l[0] && "n" === l[1] ? (e = l !== (l = l.replace(/Capture$/, "")), c = l.toLowerCase(), l = (c in n ? c : l).slice(2), u ? (t || n.addEventListener(l, T, e), (n.t || (n.t = {}))[l] = u) : n.removeEventListener(l, T, e)) : "list" !== l && "tagName" !== l && "form" !== l && !i && l in n ? n[l] = null == u ? "" : u : "function" != typeof u && "dangerouslySetInnerHTML" !== l && (l !== (l = l.replace(/^xlink:?/, "")) ? null == u || !1 === u ? n.removeAttributeNS("http://www.w3.org/1999/xlink", l.toLowerCase()) : n.setAttributeNS("http://www.w3.org/1999/xlink", l.toLowerCase(), u) : null == u || !1 === u ? n.removeAttribute(l) : n.setAttribute(l, u)); } } function T(l) { return this.t[l.type](n.event ? n.event(l) : l) } function $(l, u, t, i, r, o, f, e, c, a) { var h, v, p, y, w, g, k, _, C, P, N = u.type; if (void 0 !== u.constructor) { return null; } (h = n.__b) && h(u); try { n: if ("function" == typeof N) { if (_ = u.props, C = (h = N.contextType) && i[h.__c], P = h ? C ? C.props.value : h.__p : i, t.__c ? k = (v = u.__c = t.__c).__p = v.__E : ("prototype" in N && N.prototype.render ? u.__c = v = new N(_, P) : (u.__c = v = new m(_, P), v.constructor = N, v.render = H), C && C.sub(v), v.props = _, v.state || (v.state = {}), v.context = P, v.__n = i, p = v.__d = !0, v.__h = []), null == v.__s && (v.__s = v.state), null != N.getDerivedStateFromProps && s(v.__s == v.state ? v.__s = s({}, v.__s) : v.__s, N.getDerivedStateFromProps(_, v.__s)), p) { null == N.getDerivedStateFromProps && null != v.componentWillMount && v.componentWillMount(), null != v.componentDidMount && f.push(v); } else { if (null == N.getDerivedStateFromProps && null == e && null != v.componentWillReceiveProps && v.componentWillReceiveProps(_, P), !e && null != v.shouldComponentUpdate && !1 === v.shouldComponentUpdate(_, v.__s, P)) { for (v.props = _, v.state = v.__s, v.__d = !1, v.__v = u, u.__e = null != c ? c !== t.__e ? c : t.__e : null, u.__k = t.__k, h = 0; h < u.__k.length; h++) { u.__k[h] && (u.__k[h].__p = u); } break n } null != v.componentWillUpdate && v.componentWillUpdate(_, v.__s, P); } for (y = v.props, w = v.state, v.context = P, v.props = _, v.state = v.__s, (h = n.__r) && h(u), v.__d = !1, v.__v = u, v.__P = l, h = v.render(v.props, v.state, v.context), u.__k = x(null != h && h.type == d && null == h.key ? h.props.children : h), null != v.getChildContext && (i = s(s({}, i), v.getChildContext())), p || null == v.getSnapshotBeforeUpdate || (g = v.getSnapshotBeforeUpdate(y, w)), b(l, u, t, i, r, o, f, c, a), v.base = u.__e; h = v.__h.pop();) { v.__s && (v.state = v.__s), h.call(v); } p || null == y || null == v.componentDidUpdate || v.componentDidUpdate(y, w, g), k && (v.__E = v.__p = null); } else { u.__e = z(t.__e, u, t, i, r, o, f, a); } (h = n.diffed) && h(u); } catch (l) { n.__e(l, u, t); } return u.__e } function j(l, u) { for (var t; t = l.pop();) { try { t.componentDidMount(); } catch (l) { n.__e(l, t.__v); } } n.__c && n.__c(u); } function z(n, l, u, t, i, r, o, c) { var s, a, h, v, p = u.props, d = l.props; if (i = "svg" === l.type || i, null == n && null != r) { for (s = 0; s < r.length; s++) { if (null != (a = r[s]) && (null === l.type ? 3 === a.nodeType : a.localName === l.type)) { n = a, r[s] = null; break } } } if (null == n) { if (null === l.type) { return document.createTextNode(d); } n = i ? document.createElementNS("http://www.w3.org/2000/svg", l.type) : document.createElement(l.type), r = null; } return null === l.type ? p !== d && (null != r && (r[r.indexOf(n)] = null), n.data = d) : l !== u && (null != r && (r = e.slice.call(n.childNodes)), h = (p = u.props || f).dangerouslySetInnerHTML, v = d.dangerouslySetInnerHTML, c || (v || h) && (v && h && v.__html == h.__html || (n.innerHTML = v && v.__html || "")), C(n, d, p, i, c), l.__k = l.props.children, v || b(n, l, u, t, "foreignObject" !== l.type && i, r, o, f, c), c || ("value" in d && void 0 !== d.value && d.value !== n.value && (n.value = null == d.value ? "" : d.value), "checked" in d && void 0 !== d.checked && d.checked !== n.checked && (n.checked = d.checked))), n } function A(l, u, t) { try { "function" == typeof l ? l(u) : l.current = u; } catch (l) { n.__e(l, t); } } function D(l, u, t) { var i, r, o; if (n.unmount && n.unmount(l), (i = l.ref) && A(i, null, u), t || "function" == typeof l.type || (t = null != (r = l.__e)), l.__e = l.l = null, null != (i = l.__c)) { if (i.componentWillUnmount) { try { i.componentWillUnmount(); } catch (l) { n.__e(l, u); } } i.base = i.__P = null; } if (i = l.__k) { for (o = 0; o < i.length; o++) { i[o] && D(i[o], u, t); } } null != r && a(r); } function H(n, l, u) { return this.constructor(n, u) } function I(l, u, t) { var i, o, c; n.__p && n.__p(l, u), o = (i = t === r) ? null : t && t.__k || u.__k, l = h(d, null, [l]), c = [], $(u, i ? u.__k = l : (t || u).__k = l, o || f, f, void 0 !== u.ownerSVGElement, t && !i ? [t] : o ? null : e.slice.call(u.childNodes), c, !1, t || f, i), j(c, l); } n = {}, m.prototype.setState = function (n, l) { var u = this.__s !== this.state && this.__s || (this.__s = s({}, this.state)); ("function" != typeof n || (n = n(u, this.props))) && s(u, n), null != n && this.__v && (this.u = !1, l && this.__h.push(l), k(this)); }, m.prototype.forceUpdate = function (n) { this.__v && (n && this.__h.push(n), this.u = !0, k(this)); }, m.prototype.render = d, u = [], t = "function" == typeof Promise ? Promise.prototype.then.bind(Promise.resolve()) : setTimeout, i = n.debounceRendering, n.__e = function (n, l, u) { for (var t; l = l.__p;) { if ((t = l.__c) && !t.__p) { try { if (t.constructor && null != t.constructor.getDerivedStateFromError) { t.setState(t.constructor.getDerivedStateFromError(n)); } else { if (null == t.componentDidCatch) { continue; } t.componentDidCatch(n); } return k(t.__E = t) } catch (l) { n = l; } } } throw n }, r = f, o = 0;
|
|
16
|
-
|
|
17
|
-
function _defineProperties(target, props) {
|
|
18
|
-
for (var i = 0; i < props.length; i++) {
|
|
19
|
-
var descriptor = props[i];
|
|
20
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
21
|
-
descriptor.configurable = true;
|
|
22
|
-
if ("value" in descriptor) { descriptor.writable = true; }
|
|
23
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function _createClass(Constructor, protoProps, staticProps) {
|
|
28
|
-
if (protoProps) { _defineProperties(Constructor.prototype, protoProps); }
|
|
29
|
-
if (staticProps) { _defineProperties(Constructor, staticProps); }
|
|
30
|
-
return Constructor;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function _extends() {
|
|
34
|
-
_extends = Object.assign || function (target) {
|
|
35
|
-
var arguments$1 = arguments;
|
|
36
|
-
|
|
37
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
38
|
-
var source = arguments$1[i];
|
|
39
|
-
|
|
40
|
-
for (var key in source) {
|
|
41
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
42
|
-
target[key] = source[key];
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return target;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
return _extends.apply(this, arguments);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// Some regular expressions for rgb() and hsl() Colors are borrowed from tinyColor
|
|
54
|
-
// https://github.com/bgrins/TinyColor
|
|
55
|
-
// Kelvin temperature math borrowed from Neil Barlett's implementation
|
|
56
|
-
// from https://github.com/neilbartlett/color-temperature
|
|
57
|
-
// https://www.w3.org/TR/css3-values/#integers
|
|
58
|
-
var CSS_INTEGER = '[-\\+]?\\d+%?'; // http://www.w3.org/TR/css3-values/#number-value
|
|
59
|
-
|
|
60
|
-
var CSS_NUMBER = '[-\\+]?\\d*\\.\\d+%?'; // Allow positive/negative integer/number. Don't capture the either/or, just the entire outcome
|
|
61
|
-
|
|
62
|
-
var CSS_UNIT = '(?:' + CSS_NUMBER + ')|(?:' + CSS_INTEGER + ')'; // Parse function params
|
|
63
|
-
// Parens and commas are optional, and this also allows for whitespace between numbers
|
|
64
|
-
|
|
65
|
-
var PERMISSIVE_MATCH_3 = '[\\s|\\(]+(' + CSS_UNIT + ')[,|\\s]+(' + CSS_UNIT + ')[,|\\s]+(' + CSS_UNIT + ')\\s*\\)?';
|
|
66
|
-
var PERMISSIVE_MATCH_4 = '[\\s|\\(]+(' + CSS_UNIT + ')[,|\\s]+(' + CSS_UNIT + ')[,|\\s]+(' + CSS_UNIT + ')[,|\\s]+(' + CSS_UNIT + ')\\s*\\)?'; // Regex patterns for functional color strings
|
|
67
|
-
|
|
68
|
-
var REGEX_FUNCTIONAL_RGB = new RegExp('rgb' + PERMISSIVE_MATCH_3);
|
|
69
|
-
var REGEX_FUNCTIONAL_RGBA = new RegExp('rgba' + PERMISSIVE_MATCH_4);
|
|
70
|
-
var REGEX_FUNCTIONAL_HSL = new RegExp('hsl' + PERMISSIVE_MATCH_3);
|
|
71
|
-
var REGEX_FUNCTIONAL_HSLA = new RegExp('hsla' + PERMISSIVE_MATCH_4); // Color string parsing regex
|
|
72
|
-
|
|
73
|
-
var HEX_START = '^(?:#?|0x?)';
|
|
74
|
-
var HEX_INT_SINGLE = '([0-9a-fA-F]{1})';
|
|
75
|
-
var HEX_INT_DOUBLE = '([0-9a-fA-F]{2})';
|
|
76
|
-
var REGEX_HEX_3 = new RegExp(HEX_START + HEX_INT_SINGLE + HEX_INT_SINGLE + HEX_INT_SINGLE + '$');
|
|
77
|
-
var REGEX_HEX_4 = new RegExp(HEX_START + HEX_INT_SINGLE + HEX_INT_SINGLE + HEX_INT_SINGLE + HEX_INT_SINGLE + '$');
|
|
78
|
-
var REGEX_HEX_6 = new RegExp(HEX_START + HEX_INT_DOUBLE + HEX_INT_DOUBLE + HEX_INT_DOUBLE + '$');
|
|
79
|
-
var REGEX_HEX_8 = new RegExp(HEX_START + HEX_INT_DOUBLE + HEX_INT_DOUBLE + HEX_INT_DOUBLE + HEX_INT_DOUBLE + '$'); // Kelvin temperature bounds
|
|
80
|
-
|
|
81
|
-
var KELVIN_MIN = 2000;
|
|
82
|
-
var KELVIN_MAX = 40000; // Math shorthands
|
|
83
|
-
|
|
84
|
-
var log = Math.log,
|
|
85
|
-
round = Math.round,
|
|
86
|
-
floor = Math.floor;
|
|
87
|
-
/**
|
|
88
|
-
* @desc Clamp a number between a min and max value
|
|
89
|
-
* @param num - input value
|
|
90
|
-
* @param min - min allowed value
|
|
91
|
-
* @param max - max allowed value
|
|
92
|
-
*/
|
|
93
|
-
|
|
94
|
-
function clamp(num, min, max) {
|
|
95
|
-
return Math.min(Math.max(num, min), max);
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* @desc Parse a css unit string - either regular int or a percentage number
|
|
99
|
-
* @param str - css unit string
|
|
100
|
-
* @param max - max unit value, used for calculating percentages
|
|
101
|
-
*/
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
function parseUnit(str, max) {
|
|
105
|
-
var isPercentage = str.indexOf('%') > -1;
|
|
106
|
-
var num = parseFloat(str);
|
|
107
|
-
return isPercentage ? max / 100 * num : num;
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* @desc Parse hex str to an int
|
|
111
|
-
* @param str - hex string to parse
|
|
112
|
-
*/
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
function parseHexInt(str) {
|
|
116
|
-
return parseInt(str, 16);
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* @desc Convert nunber into to 2-digit hex
|
|
120
|
-
* @param int - number to convert
|
|
121
|
-
*/
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
function intToHex(_int) {
|
|
125
|
-
return _int.toString(16).padStart(2, '0');
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
var IroColor =
|
|
129
|
-
/*#__PURE__*/
|
|
130
|
-
function () {
|
|
131
|
-
/**
|
|
132
|
-
* @constructor Color object
|
|
133
|
-
* @param value - initial color value
|
|
134
|
-
*/
|
|
135
|
-
function IroColor(value, onChange) {
|
|
136
|
-
// The default Color value
|
|
137
|
-
this.$ = {
|
|
138
|
-
h: 0,
|
|
139
|
-
s: 0,
|
|
140
|
-
v: 0,
|
|
141
|
-
a: 1
|
|
142
|
-
};
|
|
143
|
-
if (value) { this.set(value); } // The watch callback function for this Color will be stored here
|
|
144
|
-
|
|
145
|
-
this.onChange = onChange;
|
|
146
|
-
this.initialValue = _extends({}, this.$); // copy initial value
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* @desc Set the Color from any valid value
|
|
150
|
-
* @param value - new color value
|
|
151
|
-
*/
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
var _proto = IroColor.prototype;
|
|
155
|
-
|
|
156
|
-
_proto.set = function set(value) {
|
|
157
|
-
if (typeof value === 'string') {
|
|
158
|
-
if (/^(?:#?|0x?)[0-9a-fA-F]{3,8}$/.test(value)) {
|
|
159
|
-
this.hexString = value;
|
|
160
|
-
} else if (/^rgba?/.test(value)) {
|
|
161
|
-
this.rgbString = value;
|
|
162
|
-
} else if (/^hsla?/.test(value)) {
|
|
163
|
-
this.hslString = value;
|
|
164
|
-
}
|
|
165
|
-
} else if (typeof value === 'object') {
|
|
166
|
-
if (value instanceof IroColor) {
|
|
167
|
-
this.hsva = value.hsva;
|
|
168
|
-
} else if ('r' in value && 'g' in value && 'b' in value) {
|
|
169
|
-
this.rgb = value;
|
|
170
|
-
} else if ('h' in value && 's' in value && 'v' in value) {
|
|
171
|
-
this.hsv = value;
|
|
172
|
-
} else if ('h' in value && 's' in value && 'l' in value) {
|
|
173
|
-
this.hsl = value;
|
|
174
|
-
} else if ('kelvin' in value) {
|
|
175
|
-
this.kelvin = value.kelvin;
|
|
176
|
-
}
|
|
177
|
-
} else {
|
|
178
|
-
throw new Error('Invalid color value');
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* @desc Shortcut to set a specific channel value
|
|
183
|
-
* @param format - hsv | hsl | rgb
|
|
184
|
-
* @param channel - individual channel to set, for example if model = hsl, chanel = h | s | l
|
|
185
|
-
* @param value - new value for the channel
|
|
186
|
-
*/
|
|
187
|
-
;
|
|
188
|
-
|
|
189
|
-
_proto.setChannel = function setChannel(format, channel, value) {
|
|
190
|
-
var _extends2;
|
|
191
|
-
|
|
192
|
-
this[format] = _extends({}, this[format], (_extends2 = {}, _extends2[channel] = value, _extends2));
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* @desc Reset color back to its initial value
|
|
196
|
-
*/
|
|
197
|
-
;
|
|
198
|
-
|
|
199
|
-
_proto.reset = function reset() {
|
|
200
|
-
this.hsva = this.initialValue;
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* @desc make new Color instance with the same value as this one
|
|
204
|
-
*/
|
|
205
|
-
;
|
|
206
|
-
|
|
207
|
-
_proto.clone = function clone() {
|
|
208
|
-
return new IroColor(this);
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* @desc remove color onChange
|
|
212
|
-
*/
|
|
213
|
-
;
|
|
214
|
-
|
|
215
|
-
_proto.unbind = function unbind() {
|
|
216
|
-
this.onChange = undefined;
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* @desc Convert hsv object to rgb
|
|
220
|
-
* @param hsv - hsv color object
|
|
221
|
-
*/
|
|
222
|
-
;
|
|
223
|
-
|
|
224
|
-
IroColor.hsvToRgb = function hsvToRgb(hsv) {
|
|
225
|
-
var h = hsv.h / 60;
|
|
226
|
-
var s = hsv.s / 100;
|
|
227
|
-
var v = hsv.v / 100;
|
|
228
|
-
var i = floor(h);
|
|
229
|
-
var f = h - i;
|
|
230
|
-
var p = v * (1 - s);
|
|
231
|
-
var q = v * (1 - f * s);
|
|
232
|
-
var t = v * (1 - (1 - f) * s);
|
|
233
|
-
var mod = i % 6;
|
|
234
|
-
var r = [v, q, p, p, t, v][mod];
|
|
235
|
-
var g = [t, v, v, q, p, p][mod];
|
|
236
|
-
var b = [p, p, t, v, v, q][mod];
|
|
237
|
-
return {
|
|
238
|
-
r: clamp(r * 255, 0, 255),
|
|
239
|
-
g: clamp(g * 255, 0, 255),
|
|
240
|
-
b: clamp(b * 255, 0, 255)
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* @desc Convert rgb object to hsv
|
|
245
|
-
* @param rgb - rgb object
|
|
246
|
-
*/
|
|
247
|
-
;
|
|
248
|
-
|
|
249
|
-
IroColor.rgbToHsv = function rgbToHsv(rgb) {
|
|
250
|
-
var r = rgb.r / 255;
|
|
251
|
-
var g = rgb.g / 255;
|
|
252
|
-
var b = rgb.b / 255;
|
|
253
|
-
var max = Math.max(r, g, b);
|
|
254
|
-
var min = Math.min(r, g, b);
|
|
255
|
-
var delta = max - min;
|
|
256
|
-
var hue = 0;
|
|
257
|
-
var value = max;
|
|
258
|
-
var saturation = max === 0 ? 0 : delta / max;
|
|
259
|
-
|
|
260
|
-
switch (max) {
|
|
261
|
-
case min:
|
|
262
|
-
hue = 0; // achromatic
|
|
263
|
-
|
|
264
|
-
break;
|
|
265
|
-
|
|
266
|
-
case r:
|
|
267
|
-
hue = (g - b) / delta + (g < b ? 6 : 0);
|
|
268
|
-
break;
|
|
269
|
-
|
|
270
|
-
case g:
|
|
271
|
-
hue = (b - r) / delta + 2;
|
|
272
|
-
break;
|
|
273
|
-
|
|
274
|
-
case b:
|
|
275
|
-
hue = (r - g) / delta + 4;
|
|
276
|
-
break;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
return {
|
|
280
|
-
h: hue * 60 % 360,
|
|
281
|
-
s: clamp(saturation * 100, 0, 100),
|
|
282
|
-
v: clamp(value * 100, 0, 100)
|
|
283
|
-
};
|
|
284
|
-
}
|
|
285
|
-
/**
|
|
286
|
-
* @desc Convert hsv object to hsl
|
|
287
|
-
* @param hsv - hsv object
|
|
288
|
-
*/
|
|
289
|
-
;
|
|
290
|
-
|
|
291
|
-
IroColor.hsvToHsl = function hsvToHsl(hsv) {
|
|
292
|
-
var s = hsv.s / 100;
|
|
293
|
-
var v = hsv.v / 100;
|
|
294
|
-
var l = (2 - s) * v;
|
|
295
|
-
var divisor = l <= 1 ? l : 2 - l; // Avoid division by zero when lightness is close to zero
|
|
296
|
-
|
|
297
|
-
var saturation = divisor < 1e-9 ? 0 : s * v / divisor;
|
|
298
|
-
return {
|
|
299
|
-
h: hsv.h,
|
|
300
|
-
s: clamp(saturation * 100, 0, 100),
|
|
301
|
-
l: clamp(l * 50, 0, 100)
|
|
302
|
-
};
|
|
303
|
-
}
|
|
304
|
-
/**
|
|
305
|
-
* @desc Convert hsl object to hsv
|
|
306
|
-
* @param hsl - hsl object
|
|
307
|
-
*/
|
|
308
|
-
;
|
|
309
|
-
|
|
310
|
-
IroColor.hslToHsv = function hslToHsv(hsl) {
|
|
311
|
-
var l = hsl.l * 2;
|
|
312
|
-
var s = hsl.s * (l <= 100 ? l : 200 - l) / 100; // Avoid division by zero when l + s is near 0
|
|
313
|
-
|
|
314
|
-
var saturation = l + s < 1e-9 ? 0 : 2 * s / (l + s);
|
|
315
|
-
return {
|
|
316
|
-
h: hsl.h,
|
|
317
|
-
s: clamp(saturation * 100, 0, 100),
|
|
318
|
-
v: clamp((l + s) / 2, 0, 100)
|
|
319
|
-
};
|
|
320
|
-
}
|
|
321
|
-
/**
|
|
322
|
-
* @desc Convert a kelvin temperature to an approx, RGB value
|
|
323
|
-
* @param kelvin - kelvin temperature
|
|
324
|
-
*/
|
|
325
|
-
;
|
|
326
|
-
|
|
327
|
-
IroColor.kelvinToRgb = function kelvinToRgb(kelvin) {
|
|
328
|
-
var temp = kelvin / 100;
|
|
329
|
-
var r, g, b;
|
|
330
|
-
|
|
331
|
-
if (temp < 66) {
|
|
332
|
-
r = 255;
|
|
333
|
-
g = -155.25485562709179 - 0.44596950469579133 * (g = temp - 2) + 104.49216199393888 * log(g);
|
|
334
|
-
b = temp < 20 ? 0 : -254.76935184120902 + 0.8274096064007395 * (b = temp - 10) + 115.67994401066147 * log(b);
|
|
335
|
-
} else {
|
|
336
|
-
r = 351.97690566805693 + 0.114206453784165 * (r = temp - 55) - 40.25366309332127 * log(r);
|
|
337
|
-
g = 325.4494125711974 + 0.07943456536662342 * (g = temp - 50) - 28.0852963507957 * log(g);
|
|
338
|
-
b = 255;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
return {
|
|
342
|
-
r: clamp(floor(r), 0, 255),
|
|
343
|
-
g: clamp(floor(g), 0, 255),
|
|
344
|
-
b: clamp(floor(b), 0, 255)
|
|
345
|
-
};
|
|
346
|
-
}
|
|
347
|
-
/**
|
|
348
|
-
* @desc Convert an RGB color to an approximate kelvin temperature
|
|
349
|
-
* @param kelvin - kelvin temperature
|
|
350
|
-
*/
|
|
351
|
-
;
|
|
352
|
-
|
|
353
|
-
IroColor.rgbToKelvin = function rgbToKelvin(rgb) {
|
|
354
|
-
var r = rgb.r,
|
|
355
|
-
b = rgb.b;
|
|
356
|
-
var eps = 0.4;
|
|
357
|
-
var minTemp = KELVIN_MIN;
|
|
358
|
-
var maxTemp = KELVIN_MAX;
|
|
359
|
-
var temp;
|
|
360
|
-
|
|
361
|
-
while (maxTemp - minTemp > eps) {
|
|
362
|
-
temp = (maxTemp + minTemp) * 0.5;
|
|
363
|
-
|
|
364
|
-
var _rgb = IroColor.kelvinToRgb(temp);
|
|
365
|
-
|
|
366
|
-
if (_rgb.b / _rgb.r >= b / r) {
|
|
367
|
-
maxTemp = temp;
|
|
368
|
-
} else {
|
|
369
|
-
minTemp = temp;
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
return temp;
|
|
374
|
-
};
|
|
375
|
-
|
|
376
|
-
_createClass(IroColor, [{
|
|
377
|
-
key: "hsv",
|
|
378
|
-
get: function get() {
|
|
379
|
-
// value is cloned to allow changes to be made to the values before passing them back
|
|
380
|
-
var value = this.$;
|
|
381
|
-
return {
|
|
382
|
-
h: value.h,
|
|
383
|
-
s: value.s,
|
|
384
|
-
v: value.v
|
|
385
|
-
};
|
|
386
|
-
},
|
|
387
|
-
set: function set(newValue) {
|
|
388
|
-
var oldValue = this.$;
|
|
389
|
-
newValue = _extends({}, oldValue, newValue); // If this Color is being watched for changes we need to compare the new and old values to check the difference
|
|
390
|
-
// Otherwise we can just be lazy
|
|
391
|
-
|
|
392
|
-
if (this.onChange) {
|
|
393
|
-
// Compute changed values
|
|
394
|
-
var changes = {
|
|
395
|
-
h: false,
|
|
396
|
-
v: false,
|
|
397
|
-
s: false,
|
|
398
|
-
a: false
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
for (var key in oldValue) {
|
|
402
|
-
changes[key] = newValue[key] != oldValue[key];
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
this.$ = newValue; // If the value has changed, call hook callback
|
|
406
|
-
|
|
407
|
-
if (changes.h || changes.s || changes.v || changes.a) { this.onChange(this, changes); }
|
|
408
|
-
} else {
|
|
409
|
-
this.$ = newValue;
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
}, {
|
|
413
|
-
key: "hsva",
|
|
414
|
-
get: function get() {
|
|
415
|
-
return _extends({}, this.$);
|
|
416
|
-
},
|
|
417
|
-
set: function set(value) {
|
|
418
|
-
this.hsv = value;
|
|
419
|
-
}
|
|
420
|
-
}, {
|
|
421
|
-
key: "hue",
|
|
422
|
-
get: function get() {
|
|
423
|
-
return this.$.h;
|
|
424
|
-
},
|
|
425
|
-
set: function set(value) {
|
|
426
|
-
this.hsv = {
|
|
427
|
-
h: value
|
|
428
|
-
};
|
|
429
|
-
}
|
|
430
|
-
}, {
|
|
431
|
-
key: "saturation",
|
|
432
|
-
get: function get() {
|
|
433
|
-
return this.$.s;
|
|
434
|
-
},
|
|
435
|
-
set: function set(value) {
|
|
436
|
-
this.hsv = {
|
|
437
|
-
s: value
|
|
438
|
-
};
|
|
439
|
-
}
|
|
440
|
-
}, {
|
|
441
|
-
key: "value",
|
|
442
|
-
get: function get() {
|
|
443
|
-
return this.$.v;
|
|
444
|
-
},
|
|
445
|
-
set: function set(value) {
|
|
446
|
-
this.hsv = {
|
|
447
|
-
v: value
|
|
448
|
-
};
|
|
449
|
-
}
|
|
450
|
-
}, {
|
|
451
|
-
key: "alpha",
|
|
452
|
-
get: function get() {
|
|
453
|
-
return this.$.a;
|
|
454
|
-
},
|
|
455
|
-
set: function set(value) {
|
|
456
|
-
this.hsv = _extends({}, this.hsv, {
|
|
457
|
-
a: value
|
|
458
|
-
});
|
|
459
|
-
}
|
|
460
|
-
}, {
|
|
461
|
-
key: "kelvin",
|
|
462
|
-
get: function get() {
|
|
463
|
-
return IroColor.rgbToKelvin(this.rgb);
|
|
464
|
-
},
|
|
465
|
-
set: function set(value) {
|
|
466
|
-
this.rgb = IroColor.kelvinToRgb(value);
|
|
467
|
-
}
|
|
468
|
-
}, {
|
|
469
|
-
key: "red",
|
|
470
|
-
get: function get() {
|
|
471
|
-
var rgb = this.rgb;
|
|
472
|
-
return rgb.r;
|
|
473
|
-
},
|
|
474
|
-
set: function set(value) {
|
|
475
|
-
this.rgb = _extends({}, this.rgb, {
|
|
476
|
-
r: value
|
|
477
|
-
});
|
|
478
|
-
}
|
|
479
|
-
}, {
|
|
480
|
-
key: "green",
|
|
481
|
-
get: function get() {
|
|
482
|
-
var rgb = this.rgb;
|
|
483
|
-
return rgb.g;
|
|
484
|
-
},
|
|
485
|
-
set: function set(value) {
|
|
486
|
-
this.rgb = _extends({}, this.rgb, {
|
|
487
|
-
g: value
|
|
488
|
-
});
|
|
489
|
-
}
|
|
490
|
-
}, {
|
|
491
|
-
key: "blue",
|
|
492
|
-
get: function get() {
|
|
493
|
-
var rgb = this.rgb;
|
|
494
|
-
return rgb.b;
|
|
495
|
-
},
|
|
496
|
-
set: function set(value) {
|
|
497
|
-
this.rgb = _extends({}, this.rgb, {
|
|
498
|
-
b: value
|
|
499
|
-
});
|
|
500
|
-
}
|
|
501
|
-
}, {
|
|
502
|
-
key: "rgb",
|
|
503
|
-
get: function get() {
|
|
504
|
-
var _IroColor$hsvToRgb = IroColor.hsvToRgb(this.$),
|
|
505
|
-
r = _IroColor$hsvToRgb.r,
|
|
506
|
-
g = _IroColor$hsvToRgb.g,
|
|
507
|
-
b = _IroColor$hsvToRgb.b;
|
|
508
|
-
|
|
509
|
-
return {
|
|
510
|
-
r: round(r),
|
|
511
|
-
g: round(g),
|
|
512
|
-
b: round(b)
|
|
513
|
-
};
|
|
514
|
-
},
|
|
515
|
-
set: function set(value) {
|
|
516
|
-
this.hsv = _extends({}, IroColor.rgbToHsv(value), {
|
|
517
|
-
a: value.a === undefined ? 1 : value.a
|
|
518
|
-
});
|
|
519
|
-
}
|
|
520
|
-
}, {
|
|
521
|
-
key: "rgba",
|
|
522
|
-
get: function get() {
|
|
523
|
-
return _extends({}, this.rgb, {
|
|
524
|
-
a: this.alpha
|
|
525
|
-
});
|
|
526
|
-
},
|
|
527
|
-
set: function set(value) {
|
|
528
|
-
this.rgb = value;
|
|
529
|
-
}
|
|
530
|
-
}, {
|
|
531
|
-
key: "hsl",
|
|
532
|
-
get: function get() {
|
|
533
|
-
var _IroColor$hsvToHsl = IroColor.hsvToHsl(this.$),
|
|
534
|
-
h = _IroColor$hsvToHsl.h,
|
|
535
|
-
s = _IroColor$hsvToHsl.s,
|
|
536
|
-
l = _IroColor$hsvToHsl.l;
|
|
537
|
-
|
|
538
|
-
return {
|
|
539
|
-
h: round(h),
|
|
540
|
-
s: round(s),
|
|
541
|
-
l: round(l)
|
|
542
|
-
};
|
|
543
|
-
},
|
|
544
|
-
set: function set(value) {
|
|
545
|
-
this.hsv = _extends({}, IroColor.hslToHsv(value), {
|
|
546
|
-
a: value.a === undefined ? 1 : value.a
|
|
547
|
-
});
|
|
548
|
-
}
|
|
549
|
-
}, {
|
|
550
|
-
key: "hsla",
|
|
551
|
-
get: function get() {
|
|
552
|
-
return _extends({}, this.hsl, {
|
|
553
|
-
a: this.alpha
|
|
554
|
-
});
|
|
555
|
-
},
|
|
556
|
-
set: function set(value) {
|
|
557
|
-
this.hsl = value;
|
|
558
|
-
}
|
|
559
|
-
}, {
|
|
560
|
-
key: "rgbString",
|
|
561
|
-
get: function get() {
|
|
562
|
-
var rgb = this.rgb;
|
|
563
|
-
return "rgb(" + rgb.r + ", " + rgb.g + ", " + rgb.b + ")";
|
|
564
|
-
},
|
|
565
|
-
set: function set(value) {
|
|
566
|
-
var match;
|
|
567
|
-
var r,
|
|
568
|
-
g,
|
|
569
|
-
b,
|
|
570
|
-
a = 1;
|
|
571
|
-
|
|
572
|
-
if (match = REGEX_FUNCTIONAL_RGB.exec(value)) {
|
|
573
|
-
r = parseUnit(match[1], 255);
|
|
574
|
-
g = parseUnit(match[2], 255);
|
|
575
|
-
b = parseUnit(match[3], 255);
|
|
576
|
-
} else if (match = REGEX_FUNCTIONAL_RGBA.exec(value)) {
|
|
577
|
-
r = parseUnit(match[1], 255);
|
|
578
|
-
g = parseUnit(match[2], 255);
|
|
579
|
-
b = parseUnit(match[3], 255);
|
|
580
|
-
a = parseUnit(match[4], 1);
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
if (match) {
|
|
584
|
-
this.rgb = {
|
|
585
|
-
r: r,
|
|
586
|
-
g: g,
|
|
587
|
-
b: b,
|
|
588
|
-
a: a
|
|
589
|
-
};
|
|
590
|
-
} else {
|
|
591
|
-
throw new Error('Invalid rgb string');
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
}, {
|
|
595
|
-
key: "rgbaString",
|
|
596
|
-
get: function get() {
|
|
597
|
-
var rgba = this.rgba;
|
|
598
|
-
return "rgba(" + rgba.r + ", " + rgba.g + ", " + rgba.b + ", " + rgba.a + ")";
|
|
599
|
-
},
|
|
600
|
-
set: function set(value) {
|
|
601
|
-
this.rgbString = value;
|
|
602
|
-
}
|
|
603
|
-
}, {
|
|
604
|
-
key: "hexString",
|
|
605
|
-
get: function get() {
|
|
606
|
-
var rgb = this.rgb;
|
|
607
|
-
return "#" + intToHex(rgb.r) + intToHex(rgb.g) + intToHex(rgb.b);
|
|
608
|
-
},
|
|
609
|
-
set: function set(value) {
|
|
610
|
-
var match;
|
|
611
|
-
var r,
|
|
612
|
-
g,
|
|
613
|
-
b,
|
|
614
|
-
a = 255;
|
|
615
|
-
|
|
616
|
-
if (match = REGEX_HEX_3.exec(value)) {
|
|
617
|
-
r = parseHexInt(match[1]) * 17;
|
|
618
|
-
g = parseHexInt(match[2]) * 17;
|
|
619
|
-
b = parseHexInt(match[3]) * 17;
|
|
620
|
-
} else if (match = REGEX_HEX_4.exec(value)) {
|
|
621
|
-
r = parseHexInt(match[1]) * 17;
|
|
622
|
-
g = parseHexInt(match[2]) * 17;
|
|
623
|
-
b = parseHexInt(match[3]) * 17;
|
|
624
|
-
a = parseHexInt(match[4]) * 17;
|
|
625
|
-
} else if (match = REGEX_HEX_6.exec(value)) {
|
|
626
|
-
r = parseHexInt(match[1]);
|
|
627
|
-
g = parseHexInt(match[2]);
|
|
628
|
-
b = parseHexInt(match[3]);
|
|
629
|
-
} else if (match = REGEX_HEX_8.exec(value)) {
|
|
630
|
-
r = parseHexInt(match[1]);
|
|
631
|
-
g = parseHexInt(match[2]);
|
|
632
|
-
b = parseHexInt(match[3]);
|
|
633
|
-
a = parseHexInt(match[4]);
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
if (match) {
|
|
637
|
-
this.rgb = {
|
|
638
|
-
r: r,
|
|
639
|
-
g: g,
|
|
640
|
-
b: b,
|
|
641
|
-
a: a / 255
|
|
642
|
-
};
|
|
643
|
-
} else {
|
|
644
|
-
throw new Error('Invalid hex string');
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
}, {
|
|
648
|
-
key: "hex8String",
|
|
649
|
-
get: function get() {
|
|
650
|
-
var rgba = this.rgba;
|
|
651
|
-
return "#" + intToHex(rgba.r) + intToHex(rgba.g) + intToHex(rgba.b) + intToHex(floor(rgba.a * 255));
|
|
652
|
-
},
|
|
653
|
-
set: function set(value) {
|
|
654
|
-
this.hexString = value;
|
|
655
|
-
}
|
|
656
|
-
}, {
|
|
657
|
-
key: "hslString",
|
|
658
|
-
get: function get() {
|
|
659
|
-
var hsl = this.hsl;
|
|
660
|
-
return "hsl(" + hsl.h + ", " + hsl.s + "%, " + hsl.l + "%)";
|
|
661
|
-
},
|
|
662
|
-
set: function set(value) {
|
|
663
|
-
var match;
|
|
664
|
-
var h,
|
|
665
|
-
s,
|
|
666
|
-
l,
|
|
667
|
-
a = 1;
|
|
668
|
-
|
|
669
|
-
if (match = REGEX_FUNCTIONAL_HSL.exec(value)) {
|
|
670
|
-
h = parseUnit(match[1], 360);
|
|
671
|
-
s = parseUnit(match[2], 100);
|
|
672
|
-
l = parseUnit(match[3], 100);
|
|
673
|
-
} else if (match = REGEX_FUNCTIONAL_HSLA.exec(value)) {
|
|
674
|
-
h = parseUnit(match[1], 360);
|
|
675
|
-
s = parseUnit(match[2], 100);
|
|
676
|
-
l = parseUnit(match[3], 100);
|
|
677
|
-
a = parseUnit(match[4], 1);
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
if (match) {
|
|
681
|
-
this.hsl = {
|
|
682
|
-
h: h,
|
|
683
|
-
s: s,
|
|
684
|
-
l: l,
|
|
685
|
-
a: a
|
|
686
|
-
};
|
|
687
|
-
} else {
|
|
688
|
-
throw new Error('Invalid hsl string');
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
}, {
|
|
692
|
-
key: "hslaString",
|
|
693
|
-
get: function get() {
|
|
694
|
-
var hsla = this.hsla;
|
|
695
|
-
return "hsla(" + hsla.h + ", " + hsla.s + "%, " + hsla.l + "%, " + hsla.a + ")";
|
|
696
|
-
},
|
|
697
|
-
set: function set(value) {
|
|
698
|
-
this.hslString = value;
|
|
699
|
-
}
|
|
700
|
-
}]);
|
|
701
|
-
|
|
702
|
-
return IroColor;
|
|
703
|
-
}();
|
|
704
|
-
|
|
705
|
-
var sliderDefaultOptions = {
|
|
706
|
-
sliderShape: 'bar',
|
|
707
|
-
sliderType: 'value',
|
|
708
|
-
minTemperature: 2200,
|
|
709
|
-
maxTemperature: 11000
|
|
710
|
-
};
|
|
711
|
-
/**
|
|
712
|
-
* @desc Get the bounding dimensions of the slider
|
|
713
|
-
* @param props - slider props
|
|
714
|
-
*/
|
|
715
|
-
|
|
716
|
-
function getSliderDimensions(props) {
|
|
717
|
-
var _sliderSize;
|
|
718
|
-
|
|
719
|
-
var width = props.width,
|
|
720
|
-
sliderSize = props.sliderSize,
|
|
721
|
-
borderWidth = props.borderWidth,
|
|
722
|
-
handleRadius = props.handleRadius,
|
|
723
|
-
padding = props.padding,
|
|
724
|
-
sliderShape = props.sliderShape;
|
|
725
|
-
var ishorizontal = props.layoutDirection === 'horizontal'; // automatically calculate sliderSize if its not defined
|
|
726
|
-
|
|
727
|
-
sliderSize = (_sliderSize = sliderSize) != null ? _sliderSize : padding * 2 + handleRadius * 2;
|
|
728
|
-
|
|
729
|
-
if (sliderShape === 'circle') {
|
|
730
|
-
return {
|
|
731
|
-
handleStart: props.padding + props.handleRadius,
|
|
732
|
-
handleRange: width - padding * 2 - handleRadius * 2,
|
|
733
|
-
width: width,
|
|
734
|
-
height: width,
|
|
735
|
-
cx: width / 2,
|
|
736
|
-
cy: width / 2,
|
|
737
|
-
radius: width / 2 - borderWidth / 2
|
|
738
|
-
};
|
|
739
|
-
} else {
|
|
740
|
-
return {
|
|
741
|
-
handleStart: sliderSize / 2,
|
|
742
|
-
handleRange: width - sliderSize,
|
|
743
|
-
radius: sliderSize / 2,
|
|
744
|
-
x: 0,
|
|
745
|
-
y: 0,
|
|
746
|
-
width: ishorizontal ? sliderSize : width,
|
|
747
|
-
height: ishorizontal ? width : sliderSize
|
|
748
|
-
};
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
/**
|
|
752
|
-
* @desc Get the current slider value for a given color, as a percentage
|
|
753
|
-
* @param props - slider props
|
|
754
|
-
* @param color
|
|
755
|
-
*/
|
|
756
|
-
|
|
757
|
-
function getCurrentSliderValue(props, color) {
|
|
758
|
-
var hsva = color.hsva;
|
|
759
|
-
var rgb = color.rgb;
|
|
760
|
-
|
|
761
|
-
switch (props.sliderType) {
|
|
762
|
-
case 'red':
|
|
763
|
-
return rgb.r / 2.55;
|
|
764
|
-
|
|
765
|
-
case 'green':
|
|
766
|
-
return rgb.g / 2.55;
|
|
767
|
-
|
|
768
|
-
case 'blue':
|
|
769
|
-
return rgb.b / 2.55;
|
|
770
|
-
|
|
771
|
-
case 'alpha':
|
|
772
|
-
return hsva.a * 100;
|
|
773
|
-
|
|
774
|
-
case 'kelvin':
|
|
775
|
-
var minTemperature = props.minTemperature,
|
|
776
|
-
maxTemperature = props.maxTemperature;
|
|
777
|
-
var temperatureRange = maxTemperature - minTemperature;
|
|
778
|
-
var percent = (color.kelvin - minTemperature) / temperatureRange * 100; // clmap percentage
|
|
779
|
-
|
|
780
|
-
return Math.max(0, Math.min(percent, 100));
|
|
781
|
-
|
|
782
|
-
case 'hue':
|
|
783
|
-
return hsva.h /= 3.6;
|
|
784
|
-
|
|
785
|
-
case 'saturation':
|
|
786
|
-
return hsva.s;
|
|
787
|
-
|
|
788
|
-
case 'value':
|
|
789
|
-
default:
|
|
790
|
-
return hsva.v;
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
/**
|
|
794
|
-
* @desc Get the current slider value from user input
|
|
795
|
-
* @param props - slider props
|
|
796
|
-
* @param x - global input x position
|
|
797
|
-
* @param y - global input y position
|
|
798
|
-
*/
|
|
799
|
-
|
|
800
|
-
function getSliderValueFromInput(props, x, y) {
|
|
801
|
-
var _getSliderDimensions = getSliderDimensions(props),
|
|
802
|
-
handleRange = _getSliderDimensions.handleRange,
|
|
803
|
-
handleStart = _getSliderDimensions.handleStart;
|
|
804
|
-
|
|
805
|
-
var handlePos;
|
|
806
|
-
|
|
807
|
-
if (props.layoutDirection === 'horizontal') {
|
|
808
|
-
handlePos = -1 * y + handleRange + handleStart;
|
|
809
|
-
} else {
|
|
810
|
-
handlePos = x - handleStart;
|
|
811
|
-
} // clamp handle position
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
handlePos = Math.max(Math.min(handlePos, handleRange), 0);
|
|
815
|
-
var percent = Math.round(100 / handleRange * handlePos);
|
|
816
|
-
|
|
817
|
-
switch (props.sliderType) {
|
|
818
|
-
case 'kelvin':
|
|
819
|
-
var minTemperature = props.minTemperature,
|
|
820
|
-
maxTemperature = props.maxTemperature;
|
|
821
|
-
var temperatureRange = maxTemperature - minTemperature;
|
|
822
|
-
return minTemperature + temperatureRange * (percent / 100);
|
|
823
|
-
|
|
824
|
-
case 'alpha':
|
|
825
|
-
return percent / 100;
|
|
826
|
-
|
|
827
|
-
case 'hue':
|
|
828
|
-
return percent * 3.6;
|
|
829
|
-
|
|
830
|
-
case 'red':
|
|
831
|
-
case 'blue':
|
|
832
|
-
case 'green':
|
|
833
|
-
return percent * 2.55;
|
|
834
|
-
|
|
835
|
-
default:
|
|
836
|
-
return percent;
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
/**
|
|
840
|
-
* @desc Get the current handle position for a given color
|
|
841
|
-
* @param props - slider props
|
|
842
|
-
* @param color
|
|
843
|
-
*/
|
|
844
|
-
|
|
845
|
-
function getSliderHandlePosition(props, color) {
|
|
846
|
-
var _getSliderDimensions2 = getSliderDimensions(props),
|
|
847
|
-
width = _getSliderDimensions2.width,
|
|
848
|
-
height = _getSliderDimensions2.height,
|
|
849
|
-
handleRange = _getSliderDimensions2.handleRange,
|
|
850
|
-
handleStart = _getSliderDimensions2.handleStart;
|
|
851
|
-
|
|
852
|
-
var ishorizontal = props.layoutDirection === 'horizontal';
|
|
853
|
-
var sliderValue = getCurrentSliderValue(props, color);
|
|
854
|
-
var midPoint = ishorizontal ? width / 2 : height / 2;
|
|
855
|
-
var handlePos = handleStart + sliderValue / 100 * handleRange;
|
|
856
|
-
|
|
857
|
-
if (ishorizontal) {
|
|
858
|
-
handlePos = -1 * handlePos + handleRange + handleStart * 2;
|
|
859
|
-
}
|
|
860
|
-
|
|
861
|
-
return {
|
|
862
|
-
x: ishorizontal ? midPoint : handlePos,
|
|
863
|
-
y: ishorizontal ? handlePos : midPoint
|
|
864
|
-
};
|
|
865
|
-
}
|
|
866
|
-
/**
|
|
867
|
-
* @desc Get the gradient stops for a slider
|
|
868
|
-
* @param props - slider props
|
|
869
|
-
* @param color
|
|
870
|
-
*/
|
|
871
|
-
|
|
872
|
-
function getSliderGradient(props, color) {
|
|
873
|
-
var hsv = color.hsv;
|
|
874
|
-
var rgb = color.rgb;
|
|
875
|
-
|
|
876
|
-
switch (props.sliderType) {
|
|
877
|
-
case 'red':
|
|
878
|
-
return [[0, "rgb(" + 0 + "," + rgb.g + "," + rgb.b + ")"], [100, "rgb(" + 255 + "," + rgb.g + "," + rgb.b + ")"]];
|
|
879
|
-
|
|
880
|
-
case 'green':
|
|
881
|
-
return [[0, "rgb(" + rgb.r + "," + 0 + "," + rgb.b + ")"], [100, "rgb(" + rgb.r + "," + 255 + "," + rgb.b + ")"]];
|
|
882
|
-
|
|
883
|
-
case 'blue':
|
|
884
|
-
return [[0, "rgb(" + rgb.r + "," + rgb.g + "," + 0 + ")"], [100, "rgb(" + rgb.r + "," + rgb.g + "," + 255 + ")"]];
|
|
885
|
-
|
|
886
|
-
case 'alpha':
|
|
887
|
-
return [[0, "rgba(" + rgb.r + "," + rgb.g + "," + rgb.b + ",0)"], [100, "rgb(" + rgb.r + "," + rgb.g + "," + rgb.b + ")"]];
|
|
888
|
-
|
|
889
|
-
case 'kelvin':
|
|
890
|
-
var stops = [];
|
|
891
|
-
var min = props.minTemperature;
|
|
892
|
-
var max = props.maxTemperature;
|
|
893
|
-
var numStops = 8;
|
|
894
|
-
var range = max - min;
|
|
895
|
-
|
|
896
|
-
for (var kelvin = min, stop = 0; kelvin < max; kelvin += range / numStops, stop += 1) {
|
|
897
|
-
var _IroColor$kelvinToRgb = IroColor.kelvinToRgb(kelvin),
|
|
898
|
-
r = _IroColor$kelvinToRgb.r,
|
|
899
|
-
g = _IroColor$kelvinToRgb.g,
|
|
900
|
-
b = _IroColor$kelvinToRgb.b;
|
|
901
|
-
|
|
902
|
-
stops.push([100 / numStops * stop, "rgb(" + r + "," + g + "," + b + ")"]);
|
|
903
|
-
}
|
|
904
|
-
|
|
905
|
-
return stops;
|
|
906
|
-
|
|
907
|
-
case 'hue':
|
|
908
|
-
return [[0, '#f00'], [16.666, '#ff0'], [33.333, '#0f0'], [50, '#0ff'], [66.666, '#00f'], [83.333, '#f0f'], [100, '#f00']];
|
|
909
|
-
|
|
910
|
-
case 'saturation':
|
|
911
|
-
var noSat = IroColor.hsvToHsl({
|
|
912
|
-
h: hsv.h,
|
|
913
|
-
s: 0,
|
|
914
|
-
v: hsv.v
|
|
915
|
-
});
|
|
916
|
-
var fullSat = IroColor.hsvToHsl({
|
|
917
|
-
h: hsv.h,
|
|
918
|
-
s: 100,
|
|
919
|
-
v: hsv.v
|
|
920
|
-
});
|
|
921
|
-
return [[0, "hsl(" + noSat.h + "," + noSat.s + "%," + noSat.l + "%)"], [100, "hsl(" + fullSat.h + "," + fullSat.s + "%," + fullSat.l + "%)"]];
|
|
922
|
-
|
|
923
|
-
case 'value':
|
|
924
|
-
default:
|
|
925
|
-
var hsl = IroColor.hsvToHsl({
|
|
926
|
-
h: hsv.h,
|
|
927
|
-
s: hsv.s,
|
|
928
|
-
v: 100
|
|
929
|
-
});
|
|
930
|
-
return [[0, '#000'], [100, "hsl(" + hsl.h + "," + hsl.s + "%," + hsl.l + "%)"]];
|
|
931
|
-
}
|
|
932
|
-
}
|
|
933
|
-
|
|
934
|
-
var TAU = Math.PI * 2; // javascript's modulo operator doesn't produce positive numbers with negative input
|
|
935
|
-
// https://dev.to/maurobringolf/a-neat-trick-to-compute-modulo-of-negative-numbers-111e
|
|
936
|
-
|
|
937
|
-
var mod = function mod(a, n) {
|
|
938
|
-
return (a % n + n) % n;
|
|
939
|
-
}; // distance between points (x, y) and (0, 0)
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
var dist = function dist(x, y) {
|
|
943
|
-
return Math.sqrt(x * x + y * y);
|
|
944
|
-
};
|
|
945
|
-
/**
|
|
946
|
-
* @param props - wheel props
|
|
947
|
-
* @internal
|
|
948
|
-
*/
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
function getHandleRange(props) {
|
|
952
|
-
return props.width / 2 - props.padding - props.handleRadius - props.borderWidth;
|
|
953
|
-
}
|
|
954
|
-
/**
|
|
955
|
-
* Returns true if point (x, y) lands inside the wheel
|
|
956
|
-
* @param props - wheel props
|
|
957
|
-
* @param x
|
|
958
|
-
* @param y
|
|
959
|
-
*/
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
function isInputInsideWheel(props, x, y) {
|
|
963
|
-
var _getWheelDimensions = getWheelDimensions(props),
|
|
964
|
-
cx = _getWheelDimensions.cx,
|
|
965
|
-
cy = _getWheelDimensions.cy;
|
|
966
|
-
|
|
967
|
-
var r = props.width / 2;
|
|
968
|
-
return dist(cx - x, cy - y) < r;
|
|
969
|
-
}
|
|
970
|
-
/**
|
|
971
|
-
* @desc Get the point as the center of the wheel
|
|
972
|
-
* @param props - wheel props
|
|
973
|
-
*/
|
|
974
|
-
|
|
975
|
-
function getWheelDimensions(props) {
|
|
976
|
-
var r = props.width / 2;
|
|
977
|
-
return {
|
|
978
|
-
width: props.width,
|
|
979
|
-
radius: r - props.borderWidth,
|
|
980
|
-
cx: r,
|
|
981
|
-
cy: r
|
|
982
|
-
};
|
|
983
|
-
}
|
|
984
|
-
/**
|
|
985
|
-
* @desc Translate an angle according to wheelAngle and wheelDirection
|
|
986
|
-
* @param props - wheel props
|
|
987
|
-
* @param angle - input angle
|
|
988
|
-
*/
|
|
989
|
-
|
|
990
|
-
function translateWheelAngle(props, angle, invert) {
|
|
991
|
-
var wheelAngle = props.wheelAngle;
|
|
992
|
-
var wheelDirection = props.wheelDirection; // inverted and clockwisee
|
|
993
|
-
|
|
994
|
-
if (invert && wheelDirection === 'clockwise') { angle = wheelAngle + angle; } // clockwise (input handling)
|
|
995
|
-
else if (wheelDirection === 'clockwise') { angle = 360 - wheelAngle + angle; } // inverted and anticlockwise
|
|
996
|
-
else if (invert && wheelDirection === 'anticlockwise') { angle = wheelAngle + 180 - angle; } // anticlockwise (input handling)
|
|
997
|
-
else if (wheelDirection === 'anticlockwise') { angle = wheelAngle - angle; }
|
|
998
|
-
return mod(angle, 360);
|
|
999
|
-
}
|
|
1000
|
-
/**
|
|
1001
|
-
* @desc Get the current handle position for a given color
|
|
1002
|
-
* @param props - wheel props
|
|
1003
|
-
* @param color
|
|
1004
|
-
*/
|
|
1005
|
-
|
|
1006
|
-
function getWheelHandlePosition(props, color) {
|
|
1007
|
-
var hsv = color.hsv;
|
|
1008
|
-
|
|
1009
|
-
var _getWheelDimensions2 = getWheelDimensions(props),
|
|
1010
|
-
cx = _getWheelDimensions2.cx,
|
|
1011
|
-
cy = _getWheelDimensions2.cy;
|
|
1012
|
-
|
|
1013
|
-
var handleRange = getHandleRange(props);
|
|
1014
|
-
var handleAngle = (180 + translateWheelAngle(props, hsv.h, true)) * (TAU / 360);
|
|
1015
|
-
var handleDist = hsv.s / 100 * handleRange;
|
|
1016
|
-
var direction = props.wheelDirection === 'clockwise' ? -1 : 1;
|
|
1017
|
-
return {
|
|
1018
|
-
x: cx + handleDist * Math.cos(handleAngle) * direction,
|
|
1019
|
-
y: cy + handleDist * Math.sin(handleAngle) * direction
|
|
1020
|
-
};
|
|
1021
|
-
}
|
|
1022
|
-
/**
|
|
1023
|
-
* @desc Get the current wheel value from user input
|
|
1024
|
-
* @param props - wheel props
|
|
1025
|
-
* @param x - global input x position
|
|
1026
|
-
* @param y - global input y position
|
|
1027
|
-
*/
|
|
1028
|
-
|
|
1029
|
-
function getWheelValueFromInput(props, x, y) {
|
|
1030
|
-
var _getWheelDimensions3 = getWheelDimensions(props),
|
|
1031
|
-
cx = _getWheelDimensions3.cx,
|
|
1032
|
-
cy = _getWheelDimensions3.cy;
|
|
1033
|
-
|
|
1034
|
-
var handleRange = getHandleRange(props);
|
|
1035
|
-
x = cx - x;
|
|
1036
|
-
y = cy - y; // Calculate the hue by converting the angle to radians
|
|
1037
|
-
|
|
1038
|
-
var hue = translateWheelAngle(props, Math.atan2(-y, -x) * (360 / TAU)); // Find the point's distance from the center of the wheel
|
|
1039
|
-
// This is used to show the saturation level
|
|
1040
|
-
|
|
1041
|
-
var handleDist = Math.min(dist(x, y), handleRange);
|
|
1042
|
-
return {
|
|
1043
|
-
h: Math.round(hue),
|
|
1044
|
-
s: Math.round(100 / handleRange * handleDist)
|
|
1045
|
-
};
|
|
1046
|
-
}
|
|
1047
|
-
/**
|
|
1048
|
-
* @desc Get the bounding dimensions of the box
|
|
1049
|
-
* @param props - box props
|
|
1050
|
-
*/
|
|
1051
|
-
|
|
1052
|
-
function getBoxDimensions(props) {
|
|
1053
|
-
var width = props.width,
|
|
1054
|
-
boxHeight = props.boxHeight,
|
|
1055
|
-
padding = props.padding,
|
|
1056
|
-
handleRadius = props.handleRadius;
|
|
1057
|
-
return {
|
|
1058
|
-
width: width,
|
|
1059
|
-
height: boxHeight != null ? boxHeight : width,
|
|
1060
|
-
radius: padding + handleRadius
|
|
1061
|
-
};
|
|
1062
|
-
}
|
|
1063
|
-
/**
|
|
1064
|
-
* @desc Get the current box value from user input
|
|
1065
|
-
* @param props - box props
|
|
1066
|
-
* @param x - global input x position
|
|
1067
|
-
* @param y - global input y position
|
|
1068
|
-
*/
|
|
1069
|
-
|
|
1070
|
-
function getBoxValueFromInput(props, x, y) {
|
|
1071
|
-
var _getBoxDimensions = getBoxDimensions(props),
|
|
1072
|
-
width = _getBoxDimensions.width,
|
|
1073
|
-
height = _getBoxDimensions.height,
|
|
1074
|
-
radius = _getBoxDimensions.radius;
|
|
1075
|
-
|
|
1076
|
-
var handleStart = radius;
|
|
1077
|
-
var handleRangeX = width - radius * 2;
|
|
1078
|
-
var handleRangeY = height - radius * 2;
|
|
1079
|
-
var percentX = (x - handleStart) / handleRangeX * 100;
|
|
1080
|
-
var percentY = (y - handleStart) / handleRangeY * 100;
|
|
1081
|
-
return {
|
|
1082
|
-
s: Math.max(0, Math.min(percentX, 100)),
|
|
1083
|
-
v: Math.max(0, Math.min(100 - percentY, 100))
|
|
1084
|
-
};
|
|
1085
|
-
}
|
|
1086
|
-
/**
|
|
1087
|
-
* @desc Get the current box handle position for a given color
|
|
1088
|
-
* @param props - box props
|
|
1089
|
-
* @param color
|
|
1090
|
-
*/
|
|
1091
|
-
|
|
1092
|
-
function getBoxHandlePosition(props, color) {
|
|
1093
|
-
var _getBoxDimensions2 = getBoxDimensions(props),
|
|
1094
|
-
width = _getBoxDimensions2.width,
|
|
1095
|
-
height = _getBoxDimensions2.height,
|
|
1096
|
-
radius = _getBoxDimensions2.radius;
|
|
1097
|
-
|
|
1098
|
-
var hsv = color.hsv;
|
|
1099
|
-
var handleStart = radius;
|
|
1100
|
-
var handleRangeX = width - radius * 2;
|
|
1101
|
-
var handleRangeY = height - radius * 2;
|
|
1102
|
-
return {
|
|
1103
|
-
x: handleStart + hsv.s / 100 * handleRangeX,
|
|
1104
|
-
y: handleStart + (handleRangeY - hsv.v / 100 * handleRangeY)
|
|
1105
|
-
};
|
|
1106
|
-
}
|
|
1107
|
-
/**
|
|
1108
|
-
* @desc Get the gradient stops for a box
|
|
1109
|
-
* @param props - box props
|
|
1110
|
-
* @param color
|
|
1111
|
-
*/
|
|
1112
|
-
|
|
1113
|
-
function getBoxGradients(props, color) {
|
|
1114
|
-
var hue = color.hue;
|
|
1115
|
-
return [// saturation gradient
|
|
1116
|
-
[[0, '#fff'], [100, "hsl(" + hue + ",100%,50%)"]], // lightness gradient
|
|
1117
|
-
[[0, 'rgba(0,0,0,0)'], [100, '#000']]];
|
|
1118
|
-
}
|
|
1119
|
-
|
|
1120
|
-
// Keep track of html <base> elements for resolveSvgUrl
|
|
1121
|
-
// getElementsByTagName returns a live HTMLCollection, which stays in sync with the DOM tree
|
|
1122
|
-
// So it only needs to be called once
|
|
1123
|
-
var BASE_ELEMENTS;
|
|
1124
|
-
/**
|
|
1125
|
-
* @desc Resolve an SVG reference URL
|
|
1126
|
-
* This is required to work around how Safari and iOS webviews handle gradient URLS under certain conditions
|
|
1127
|
-
* If a page is using a client-side routing library which makes use of the HTML <base> tag,
|
|
1128
|
-
* Safari won't be able to render SVG gradients properly (as they are referenced by URLs)
|
|
1129
|
-
* More info on the problem:
|
|
1130
|
-
* https://stackoverflow.com/questions/19742805/angular-and-svg-filters/19753427#19753427
|
|
1131
|
-
* https://github.com/jaames/iro.js/issues/18
|
|
1132
|
-
* https://github.com/jaames/iro.js/issues/45
|
|
1133
|
-
* https://github.com/jaames/iro.js/pull/89
|
|
1134
|
-
* @props url - SVG reference URL
|
|
1135
|
-
*/
|
|
1136
|
-
|
|
1137
|
-
function resolveSvgUrl(url) {
|
|
1138
|
-
if (!BASE_ELEMENTS) { BASE_ELEMENTS = document.getElementsByTagName('base'); } // Sniff useragent string to check if the user is running Safari
|
|
1139
|
-
|
|
1140
|
-
var ua = window.navigator.userAgent;
|
|
1141
|
-
var isSafari = /^((?!chrome|android).)*safari/i.test(ua);
|
|
1142
|
-
var isIos = /iPhone|iPod|iPad/i.test(ua);
|
|
1143
|
-
var location = window.location;
|
|
1144
|
-
return (isSafari || isIos) && BASE_ELEMENTS.length > 0 ? location.protocol + "//" + location.host + location.pathname + location.search + url : url;
|
|
1145
|
-
}
|
|
1146
|
-
/**
|
|
1147
|
-
* @desc Given a specifc (x, y) position, test if there's a handle there and return its index, else return null.
|
|
1148
|
-
* This is used for components like the box and wheel which support multiple handles when multicolor is active
|
|
1149
|
-
* @props x - point x position
|
|
1150
|
-
* @props y - point y position
|
|
1151
|
-
* @props handlePositions - array of {x, y} coords for each handle
|
|
1152
|
-
*/
|
|
1153
|
-
|
|
1154
|
-
function getHandleAtPoint(props, x, y, handlePositions) {
|
|
1155
|
-
for (var i = 0; i < handlePositions.length; i++) {
|
|
1156
|
-
var dX = handlePositions[i].x - x;
|
|
1157
|
-
var dY = handlePositions[i].y - y;
|
|
1158
|
-
var dist = Math.sqrt(dX * dX + dY * dY);
|
|
1159
|
-
|
|
1160
|
-
if (dist < props.handleRadius) {
|
|
1161
|
-
return i;
|
|
1162
|
-
}
|
|
1163
|
-
}
|
|
1164
|
-
|
|
1165
|
-
return null;
|
|
1166
|
-
}
|
|
1167
|
-
|
|
1168
|
-
function cssBorderStyles(props) {
|
|
1169
|
-
return {
|
|
1170
|
-
boxSizing: 'border-box',
|
|
1171
|
-
border: props.borderWidth + "px solid " + props.borderColor
|
|
1172
|
-
};
|
|
1173
|
-
}
|
|
1174
|
-
function cssGradient(type, direction, stops) {
|
|
1175
|
-
return type + "-gradient(" + direction + ", " + stops.map(function (_ref) {
|
|
1176
|
-
var o = _ref[0],
|
|
1177
|
-
col = _ref[1];
|
|
1178
|
-
return col + " " + o + "%";
|
|
1179
|
-
}).join(',') + ")";
|
|
1180
|
-
}
|
|
1181
|
-
function cssValue(value) {
|
|
1182
|
-
if (typeof value === 'string') { return value; }
|
|
1183
|
-
return value + "px";
|
|
1184
|
-
}
|
|
1185
|
-
|
|
1186
|
-
var iroColorPickerOptionDefaults = {
|
|
1187
|
-
width: 300,
|
|
1188
|
-
height: 300,
|
|
1189
|
-
color: '#fff',
|
|
1190
|
-
colors: [],
|
|
1191
|
-
padding: 6,
|
|
1192
|
-
layoutDirection: 'vertical',
|
|
1193
|
-
borderColor: '#fff',
|
|
1194
|
-
borderWidth: 0,
|
|
1195
|
-
handleRadius: 8,
|
|
1196
|
-
activeHandleRadius: null,
|
|
1197
|
-
handleSvg: null,
|
|
1198
|
-
handleProps: {
|
|
1199
|
-
x: 0,
|
|
1200
|
-
y: 0
|
|
1201
|
-
},
|
|
1202
|
-
wheelLightness: true,
|
|
1203
|
-
wheelAngle: 0,
|
|
1204
|
-
wheelDirection: 'anticlockwise',
|
|
1205
|
-
sliderSize: null,
|
|
1206
|
-
sliderMargin: 12,
|
|
1207
|
-
boxHeight: null
|
|
1208
|
-
};
|
|
1209
|
-
|
|
1210
|
-
var SECONDARY_EVENTS = ["mousemove" /* MouseMove */, "touchmove" /* TouchMove */, "mouseup" /* MouseUp */, "touchend" /* TouchEnd */];
|
|
1211
|
-
// Base component class for iro UI components
|
|
1212
|
-
// This extends the Preact component class to allow them to react to mouse/touch input events by themselves
|
|
1213
|
-
var IroComponentWrapper = /*@__PURE__*/(function (Component) {
|
|
1214
|
-
function IroComponentWrapper(props) {
|
|
1215
|
-
Component.call(this, props);
|
|
1216
|
-
// Generate unique ID for the component
|
|
1217
|
-
// This can be used to generate unique IDs for gradients, etc
|
|
1218
|
-
this.uid = (Math.random() + 1).toString(36).substring(5);
|
|
1219
|
-
}
|
|
1220
|
-
|
|
1221
|
-
if (Component) IroComponentWrapper.__proto__ = Component;
|
|
1222
|
-
IroComponentWrapper.prototype = Object.create(Component && Component.prototype);
|
|
1223
|
-
IroComponentWrapper.prototype.constructor = IroComponentWrapper;
|
|
1224
|
-
IroComponentWrapper.prototype.render = function render(props) {
|
|
1225
|
-
var eventHandler = this.handleEvent.bind(this);
|
|
1226
|
-
var rootProps = {
|
|
1227
|
-
onMouseDown: eventHandler,
|
|
1228
|
-
// https://github.com/jaames/iro.js/issues/126
|
|
1229
|
-
// https://github.com/preactjs/preact/issues/2113#issuecomment-553408767
|
|
1230
|
-
ontouchstart: eventHandler,
|
|
1231
|
-
};
|
|
1232
|
-
var isHorizontal = props.layoutDirection === 'horizontal';
|
|
1233
|
-
var margin = props.margin === null ? props.sliderMargin : props.margin;
|
|
1234
|
-
var rootStyles = {
|
|
1235
|
-
overflow: 'visible',
|
|
1236
|
-
display: isHorizontal ? 'inline-block' : 'block'
|
|
1237
|
-
};
|
|
1238
|
-
// first component shouldn't have any margin
|
|
1239
|
-
if (props.index > 0) {
|
|
1240
|
-
rootStyles[isHorizontal ? 'marginLeft' : 'marginTop'] = margin;
|
|
1241
|
-
}
|
|
1242
|
-
return (h(d, null, props.children(this.uid, rootProps, rootStyles)));
|
|
1243
|
-
};
|
|
1244
|
-
// More info on handleEvent:
|
|
1245
|
-
// https://medium.com/@WebReflection/dom-handleevent-a-cross-platform-standard-since-year-2000-5bf17287fd38
|
|
1246
|
-
// TL;DR this lets us have a single point of entry for multiple events, and we can avoid callback/binding hell
|
|
1247
|
-
IroComponentWrapper.prototype.handleEvent = function handleEvent(e) {
|
|
1248
|
-
var this$1 = this;
|
|
1249
|
-
|
|
1250
|
-
var inputHandler = this.props.onInput;
|
|
1251
|
-
// Get the screen position of the component
|
|
1252
|
-
var bounds = this.base.getBoundingClientRect();
|
|
1253
|
-
// Prefect default browser action
|
|
1254
|
-
e.preventDefault();
|
|
1255
|
-
// Detect if the event is a touch event by checking if it has the `touches` property
|
|
1256
|
-
// If it is a touch event, use the first touch input
|
|
1257
|
-
var point = e.touches ? e.changedTouches[0] : e;
|
|
1258
|
-
var x = point.clientX - bounds.left;
|
|
1259
|
-
var y = point.clientY - bounds.top;
|
|
1260
|
-
switch (e.type) {
|
|
1261
|
-
case "mousedown" /* MouseDown */:
|
|
1262
|
-
case "touchstart" /* TouchStart */:
|
|
1263
|
-
var result = inputHandler(x, y, 0 /* Start */);
|
|
1264
|
-
if (result !== false) {
|
|
1265
|
-
SECONDARY_EVENTS.forEach(function (event) {
|
|
1266
|
-
document.addEventListener(event, this$1, { passive: false });
|
|
1267
|
-
});
|
|
1268
|
-
}
|
|
1269
|
-
break;
|
|
1270
|
-
case "mousemove" /* MouseMove */:
|
|
1271
|
-
case "touchmove" /* TouchMove */:
|
|
1272
|
-
inputHandler(x, y, 1 /* Move */);
|
|
1273
|
-
break;
|
|
1274
|
-
case "mouseup" /* MouseUp */:
|
|
1275
|
-
case "touchend" /* TouchEnd */:
|
|
1276
|
-
inputHandler(x, y, 2 /* End */);
|
|
1277
|
-
SECONDARY_EVENTS.forEach(function (event) {
|
|
1278
|
-
document.removeEventListener(event, this$1, { passive: false });
|
|
1279
|
-
});
|
|
1280
|
-
break;
|
|
1281
|
-
}
|
|
1282
|
-
};
|
|
1283
|
-
|
|
1284
|
-
return IroComponentWrapper;
|
|
1285
|
-
}(m));
|
|
1286
|
-
|
|
1287
|
-
function IroHandle(props) {
|
|
1288
|
-
var radius = props.r;
|
|
1289
|
-
var url = props.url;
|
|
1290
|
-
var cx = radius;
|
|
1291
|
-
var cy = radius;
|
|
1292
|
-
return (h("svg", {
|
|
1293
|
-
className: ("IroHandle IroHandle--" + (props.index) + " " + (props.isActive ? 'IroHandle--isActive' : '')), style: {
|
|
1294
|
-
'-webkit-tap-highlight-color': 'rgba(0, 0, 0, 0);',
|
|
1295
|
-
transform: ("translate(" + (cssValue(props.x)) + ", " + (cssValue(props.y)) + ")"),
|
|
1296
|
-
willChange: 'transform',
|
|
1297
|
-
top: cssValue(-radius),
|
|
1298
|
-
left: cssValue(-radius),
|
|
1299
|
-
width: cssValue(radius * 2),
|
|
1300
|
-
height: cssValue(radius * 2),
|
|
1301
|
-
position: 'absolute',
|
|
1302
|
-
overflow: 'visible'
|
|
1303
|
-
}
|
|
1304
|
-
},
|
|
1305
|
-
url && (h("use", Object.assign({ xlinkHref: resolveSvgUrl(url) }, props.props))),
|
|
1306
|
-
!url && (h("circle", { cx: cx, cy: cy, r: radius, fill: "none", "stroke-width": 2, stroke: "#000" })),
|
|
1307
|
-
!url && (h("circle", { cx: cx, cy: cy, r: radius - 2, fill: props.fill, "stroke-width": 2, stroke: "#fff" }))));
|
|
1308
|
-
}
|
|
1309
|
-
IroHandle.defaultProps = {
|
|
1310
|
-
fill: 'none',
|
|
1311
|
-
x: 0,
|
|
1312
|
-
y: 0,
|
|
1313
|
-
r: 8,
|
|
1314
|
-
url: null,
|
|
1315
|
-
props: { x: 0, y: 0 }
|
|
1316
|
-
};
|
|
1317
|
-
|
|
1318
|
-
function IroSlider(props) {
|
|
1319
|
-
var activeIndex = props.activeIndex;
|
|
1320
|
-
var activeColor = (activeIndex !== undefined && activeIndex < props.colors.length) ? props.colors[activeIndex] : props.color;
|
|
1321
|
-
var ref = getSliderDimensions(props);
|
|
1322
|
-
var width = ref.width;
|
|
1323
|
-
var height = ref.height;
|
|
1324
|
-
var radius = ref.radius;
|
|
1325
|
-
var handlePos = getSliderHandlePosition(props, activeColor);
|
|
1326
|
-
var gradient = getSliderGradient(props, activeColor);
|
|
1327
|
-
function handleInput(x, y, type) {
|
|
1328
|
-
var value = getSliderValueFromInput(props, x, y);
|
|
1329
|
-
props.parent.inputActive = true;
|
|
1330
|
-
activeColor[props.sliderType] = value;
|
|
1331
|
-
props.onInput(type, props.id);
|
|
1332
|
-
}
|
|
1333
|
-
return (h(IroComponentWrapper, Object.assign({}, props, { onInput: handleInput }), function (uid, rootProps, rootStyles) {
|
|
1334
|
-
return (h("div", Object.assign({}, rootProps, {
|
|
1335
|
-
className: "IroSlider", style: Object.assign({}, {
|
|
1336
|
-
position: 'relative',
|
|
1337
|
-
width: cssValue(width),
|
|
1338
|
-
height: cssValue(height),
|
|
1339
|
-
borderRadius: cssValue(radius),
|
|
1340
|
-
// checkered bg to represent alpha
|
|
1341
|
-
background: "conic-gradient(#ccc 25%, #fff 0 50%, #ccc 0 75%, #fff 0)",
|
|
1342
|
-
backgroundSize: '8px 8px'
|
|
1343
|
-
},
|
|
1344
|
-
rootStyles)
|
|
1345
|
-
}),
|
|
1346
|
-
h("div", {
|
|
1347
|
-
className: "IroSliderGradient", style: Object.assign({}, {
|
|
1348
|
-
position: 'absolute',
|
|
1349
|
-
top: 0,
|
|
1350
|
-
left: 0,
|
|
1351
|
-
width: "100%",
|
|
1352
|
-
height: "100%",
|
|
1353
|
-
borderRadius: cssValue(radius),
|
|
1354
|
-
background: cssGradient('linear', props.layoutDirection === 'horizontal' ? 'to top' : 'to right', gradient)
|
|
1355
|
-
},
|
|
1356
|
-
cssBorderStyles(props))
|
|
1357
|
-
}),
|
|
1358
|
-
h(IroHandle, { isActive: true, index: activeColor.index, r: props.handleRadius, url: props.handleSvg, props: props.handleProps, x: handlePos.x, y: handlePos.y })));
|
|
1359
|
-
}));
|
|
1360
|
-
}
|
|
1361
|
-
IroSlider.defaultProps = Object.assign({}, sliderDefaultOptions);
|
|
1362
|
-
|
|
1363
|
-
function IroBox(props) {
|
|
1364
|
-
var ref = getBoxDimensions(props);
|
|
1365
|
-
var width = ref.width;
|
|
1366
|
-
var height = ref.height;
|
|
1367
|
-
var radius = ref.radius;
|
|
1368
|
-
var colors = props.colors;
|
|
1369
|
-
var colorPicker = props.parent;
|
|
1370
|
-
var activeIndex = props.activeIndex;
|
|
1371
|
-
var activeColor = (activeIndex !== undefined && activeIndex < props.colors.length) ? props.colors[activeIndex] : props.color;
|
|
1372
|
-
var gradients = getBoxGradients(props, activeColor);
|
|
1373
|
-
var handlePositions = colors.map(function (color) { return getBoxHandlePosition(props, color); });
|
|
1374
|
-
function handleInput(x, y, inputType) {
|
|
1375
|
-
if (inputType === 0 /* Start */) {
|
|
1376
|
-
// getHandleAtPoint() returns the index for the handle if the point 'hits' it, or null otherwise
|
|
1377
|
-
var activeHandle = getHandleAtPoint(props, x, y, handlePositions);
|
|
1378
|
-
// If the input hit a handle, set it as the active handle, but don't update the color
|
|
1379
|
-
if (activeHandle !== null) {
|
|
1380
|
-
colorPicker.setActiveColor(activeHandle);
|
|
1381
|
-
}
|
|
1382
|
-
// If the input didn't hit a handle, set the currently active handle to that position
|
|
1383
|
-
else {
|
|
1384
|
-
colorPicker.inputActive = true;
|
|
1385
|
-
activeColor.hsv = getBoxValueFromInput(props, x, y);
|
|
1386
|
-
props.onInput(inputType, props.id);
|
|
1387
|
-
}
|
|
1388
|
-
}
|
|
1389
|
-
// move is fired when the user has started dragging
|
|
1390
|
-
else if (inputType === 1 /* Move */) {
|
|
1391
|
-
colorPicker.inputActive = true;
|
|
1392
|
-
activeColor.hsv = getBoxValueFromInput(props, x, y);
|
|
1393
|
-
}
|
|
1394
|
-
// let the color picker fire input:start, input:move or input:end events
|
|
1395
|
-
props.onInput(inputType, props.id);
|
|
1396
|
-
}
|
|
1397
|
-
return (h(IroComponentWrapper, Object.assign({}, props, { onInput: handleInput }), function (uid, rootProps, rootStyles) {
|
|
1398
|
-
return (h("div", Object.assign({}, rootProps, {
|
|
1399
|
-
className: "IroBox", style: Object.assign({}, {
|
|
1400
|
-
width: cssValue(width),
|
|
1401
|
-
height: cssValue(height),
|
|
1402
|
-
position: 'relative'
|
|
1403
|
-
},
|
|
1404
|
-
rootStyles)
|
|
1405
|
-
}),
|
|
1406
|
-
h("div", {
|
|
1407
|
-
className: "IroBox", style: Object.assign({}, {
|
|
1408
|
-
width: '100%',
|
|
1409
|
-
height: '100%',
|
|
1410
|
-
borderRadius: cssValue(radius)
|
|
1411
|
-
},
|
|
1412
|
-
cssBorderStyles(props),
|
|
1413
|
-
{
|
|
1414
|
-
background: cssGradient('linear', 'to bottom', gradients[1])
|
|
1415
|
-
+ ',' +
|
|
1416
|
-
cssGradient('linear', 'to right', gradients[0])
|
|
1417
|
-
})
|
|
1418
|
-
}),
|
|
1419
|
-
colors.filter(function (color) { return color !== activeColor; }).map(function (color) { return (h(IroHandle, { isActive: false, index: color.index, fill: color.hslString, r: props.handleRadius, url: props.handleSvg, props: props.handleProps, x: handlePositions[color.index].x, y: handlePositions[color.index].y })); }),
|
|
1420
|
-
h(IroHandle, { isActive: true, index: activeColor.index, fill: activeColor.hslString, r: props.activeHandleRadius || props.handleRadius, url: props.handleSvg, props: props.handleProps, x: handlePositions[activeColor.index].x, y: handlePositions[activeColor.index].y })));
|
|
1421
|
-
}));
|
|
1422
|
-
}
|
|
1423
|
-
|
|
1424
|
-
var HUE_GRADIENT_CLOCKWISE = 'conic-gradient(red, yellow, lime, aqua, blue, magenta, red)';
|
|
1425
|
-
var HUE_GRADIENT_ANTICLOCKWISE = 'conic-gradient(red, magenta, blue, aqua, lime, yellow, red)';
|
|
1426
|
-
function IroWheel(props) {
|
|
1427
|
-
var ref = getWheelDimensions(props);
|
|
1428
|
-
var width = ref.width;
|
|
1429
|
-
var colors = props.colors;
|
|
1430
|
-
var borderWidth = props.borderWidth;
|
|
1431
|
-
var colorPicker = props.parent;
|
|
1432
|
-
var activeColor = props.color;
|
|
1433
|
-
var hsv = activeColor.hsv;
|
|
1434
|
-
var handlePositions = colors.map(function (color) { return getWheelHandlePosition(props, color); });
|
|
1435
|
-
var circleStyles = {
|
|
1436
|
-
position: 'absolute',
|
|
1437
|
-
top: 0,
|
|
1438
|
-
left: 0,
|
|
1439
|
-
width: '100%',
|
|
1440
|
-
height: '100%',
|
|
1441
|
-
borderRadius: '50%',
|
|
1442
|
-
boxSizing: 'border-box'
|
|
1443
|
-
};
|
|
1444
|
-
function handleInput(x, y, inputType) {
|
|
1445
|
-
if (inputType === 0 /* Start */) {
|
|
1446
|
-
// input hitbox is a square,
|
|
1447
|
-
// so we want to ignore any initial clicks outside the circular shape of the wheel
|
|
1448
|
-
if (!isInputInsideWheel(props, x, y)) {
|
|
1449
|
-
// returning false will cease all event handling for this interaction
|
|
1450
|
-
return false;
|
|
1451
|
-
}
|
|
1452
|
-
// getHandleAtPoint() returns the index for the handle if the point 'hits' it, or null otherwise
|
|
1453
|
-
var activeHandle = getHandleAtPoint(props, x, y, handlePositions);
|
|
1454
|
-
// If the input hit a handle, set it as the active handle, but don't update the color
|
|
1455
|
-
if (activeHandle !== null) {
|
|
1456
|
-
colorPicker.setActiveColor(activeHandle);
|
|
1457
|
-
}
|
|
1458
|
-
// If the input didn't hit a handle, set the currently active handle to that position
|
|
1459
|
-
else {
|
|
1460
|
-
colorPicker.inputActive = true;
|
|
1461
|
-
activeColor.hsv = getWheelValueFromInput(props, x, y);
|
|
1462
|
-
props.onInput(inputType, props.id);
|
|
1463
|
-
}
|
|
1464
|
-
}
|
|
1465
|
-
// move is fired when the user has started dragging
|
|
1466
|
-
else if (inputType === 1 /* Move */) {
|
|
1467
|
-
colorPicker.inputActive = true;
|
|
1468
|
-
activeColor.hsv = getWheelValueFromInput(props, x, y);
|
|
1469
|
-
}
|
|
1470
|
-
// let the color picker fire input:start, input:move or input:end events
|
|
1471
|
-
props.onInput(inputType, props.id);
|
|
1472
|
-
}
|
|
1473
|
-
return (h(IroComponentWrapper, Object.assign({}, props, { onInput: handleInput }), function (uid, rootProps, rootStyles) {
|
|
1474
|
-
return (h("div", Object.assign({}, rootProps, {
|
|
1475
|
-
className: "IroWheel", style: Object.assign({}, {
|
|
1476
|
-
width: cssValue(width),
|
|
1477
|
-
height: cssValue(width),
|
|
1478
|
-
position: 'relative'
|
|
1479
|
-
},
|
|
1480
|
-
rootStyles)
|
|
1481
|
-
}),
|
|
1482
|
-
h("div", {
|
|
1483
|
-
className: "IroWheelHue", style: Object.assign({}, circleStyles,
|
|
1484
|
-
{
|
|
1485
|
-
transform: ("rotateZ(" + (props.wheelAngle + 90) + "deg)"),
|
|
1486
|
-
background: props.wheelDirection === 'clockwise' ? HUE_GRADIENT_CLOCKWISE : HUE_GRADIENT_ANTICLOCKWISE
|
|
1487
|
-
})
|
|
1488
|
-
}),
|
|
1489
|
-
h("div", {
|
|
1490
|
-
className: "IroWheelSaturation", style: Object.assign({}, circleStyles,
|
|
1491
|
-
{ background: 'radial-gradient(circle closest-side, #fff, transparent)' })
|
|
1492
|
-
}),
|
|
1493
|
-
props.wheelLightness && (h("div", {
|
|
1494
|
-
className: "IroWheelLightness", style: Object.assign({}, circleStyles,
|
|
1495
|
-
{
|
|
1496
|
-
background: '#000',
|
|
1497
|
-
opacity: 1 - hsv.v / 100
|
|
1498
|
-
})
|
|
1499
|
-
})),
|
|
1500
|
-
h("div", {
|
|
1501
|
-
className: "IroWheelBorder", style: Object.assign({}, circleStyles,
|
|
1502
|
-
cssBorderStyles(props))
|
|
1503
|
-
}),
|
|
1504
|
-
colors.filter(function (color) { return color !== activeColor; }).map(function (color) { return (h(IroHandle, { isActive: false, index: color.index, fill: color.hslString, r: props.handleRadius, url: props.handleSvg, props: props.handleProps, x: handlePositions[color.index].x, y: handlePositions[color.index].y })); }),
|
|
1505
|
-
h(IroHandle, { isActive: true, index: activeColor.index, fill: activeColor.hslString, r: props.activeHandleRadius || props.handleRadius, url: props.handleSvg, props: props.handleProps, x: handlePositions[activeColor.index].x, y: handlePositions[activeColor.index].y })));
|
|
1506
|
-
}));
|
|
1507
|
-
}
|
|
1508
|
-
|
|
1509
|
-
function createWidget(WidgetComponent) {
|
|
1510
|
-
var widgetFactory = function (parent, props) {
|
|
1511
|
-
var widget; // will become an instance of the widget component class
|
|
1512
|
-
var widgetRoot = document.createElement('div');
|
|
1513
|
-
// Render widget into a temp DOM node
|
|
1514
|
-
I(h(WidgetComponent, Object.assign({}, { ref: function (ref) { return widget = ref; } },
|
|
1515
|
-
props)), widgetRoot);
|
|
1516
|
-
function mountWidget() {
|
|
1517
|
-
var container = parent instanceof Element ? parent : document.querySelector(parent);
|
|
1518
|
-
container.appendChild(widget.base);
|
|
1519
|
-
widget.onMount(container);
|
|
1520
|
-
}
|
|
1521
|
-
// Mount it into the DOM when the page document is ready
|
|
1522
|
-
if (document.readyState !== 'loading') {
|
|
1523
|
-
mountWidget();
|
|
1524
|
-
}
|
|
1525
|
-
else {
|
|
1526
|
-
document.addEventListener('DOMContentLoaded', mountWidget);
|
|
1527
|
-
}
|
|
1528
|
-
return widget;
|
|
1529
|
-
};
|
|
1530
|
-
// Allow the widget factory to inherit component prototype + static class methods
|
|
1531
|
-
// This makes it easier for plugin authors to extend the base widget component
|
|
1532
|
-
widgetFactory.prototype = WidgetComponent.prototype;
|
|
1533
|
-
Object.assign(widgetFactory, WidgetComponent);
|
|
1534
|
-
// Add reference to base component too
|
|
1535
|
-
widgetFactory.__component = WidgetComponent;
|
|
1536
|
-
return widgetFactory;
|
|
1537
|
-
}
|
|
1538
|
-
|
|
1539
|
-
var IroColorPicker = /*@__PURE__*/(function (Component) {
|
|
1540
|
-
function IroColorPicker(props) {
|
|
1541
|
-
var this$1 = this;
|
|
1542
|
-
|
|
1543
|
-
Component.call(this, props);
|
|
1544
|
-
this.colors = [];
|
|
1545
|
-
this.inputActive = false;
|
|
1546
|
-
this.events = {};
|
|
1547
|
-
this.activeEvents = {};
|
|
1548
|
-
this.deferredEvents = {};
|
|
1549
|
-
this.id = props.id;
|
|
1550
|
-
var colors = props.colors.length > 0 ? props.colors : [props.color];
|
|
1551
|
-
colors.forEach(function (colorValue) { return this$1.addColor(colorValue); });
|
|
1552
|
-
this.setActiveColor(0);
|
|
1553
|
-
// Pass all the props into the component's state,
|
|
1554
|
-
// Except we want to add the color object and make sure that refs aren't passed down to children
|
|
1555
|
-
this.state = Object.assign({}, props,
|
|
1556
|
-
{
|
|
1557
|
-
color: this.color,
|
|
1558
|
-
colors: this.colors,
|
|
1559
|
-
layout: props.layout
|
|
1560
|
-
});
|
|
1561
|
-
}
|
|
1562
|
-
|
|
1563
|
-
if (Component) IroColorPicker.__proto__ = Component;
|
|
1564
|
-
IroColorPicker.prototype = Object.create(Component && Component.prototype);
|
|
1565
|
-
IroColorPicker.prototype.constructor = IroColorPicker;
|
|
1566
|
-
// Plubic multicolor API
|
|
1567
|
-
/**
|
|
1568
|
-
* @desc Add a color to the color picker
|
|
1569
|
-
* @param color new color to add
|
|
1570
|
-
* @param index optional color index
|
|
1571
|
-
*/
|
|
1572
|
-
IroColorPicker.prototype.addColor = function addColor(color, index) {
|
|
1573
|
-
if (index === void 0) index = this.colors.length;
|
|
1574
|
-
|
|
1575
|
-
// Create a new iro.Color
|
|
1576
|
-
// Also bind it to onColorChange, so whenever the color changes it updates the color picker
|
|
1577
|
-
var newColor = new IroColor(color, this.onColorChange.bind(this));
|
|
1578
|
-
// Insert color @ the given index
|
|
1579
|
-
this.colors.splice(index, 0, newColor);
|
|
1580
|
-
// Reindex colors
|
|
1581
|
-
this.colors.forEach(function (color, index) { return color.index = index; });
|
|
1582
|
-
// Update picker state if necessary
|
|
1583
|
-
if (this.state) {
|
|
1584
|
-
this.setState({ colors: this.colors });
|
|
1585
|
-
}
|
|
1586
|
-
// Fire color init event
|
|
1587
|
-
this.deferredEmit('color:init', newColor);
|
|
1588
|
-
};
|
|
1589
|
-
/**
|
|
1590
|
-
* @desc Remove a color from the color picker
|
|
1591
|
-
* @param index color index
|
|
1592
|
-
*/
|
|
1593
|
-
IroColorPicker.prototype.removeColor = function removeColor(index) {
|
|
1594
|
-
var color = this.colors.splice(index, 1)[0];
|
|
1595
|
-
// Destroy the color object -- this unbinds it from the color picker
|
|
1596
|
-
color.unbind();
|
|
1597
|
-
// Reindex colors
|
|
1598
|
-
this.colors.forEach(function (color, index) { return color.index = index; });
|
|
1599
|
-
// Update picker state if necessary
|
|
1600
|
-
if (this.state) {
|
|
1601
|
-
this.setState({ colors: this.colors });
|
|
1602
|
-
}
|
|
1603
|
-
// If the active color was removed, default active color to 0
|
|
1604
|
-
if (color.index === this.color.index) {
|
|
1605
|
-
this.setActiveColor(0);
|
|
1606
|
-
}
|
|
1607
|
-
// Fire color remove event
|
|
1608
|
-
this.emit('color:remove', color);
|
|
1609
|
-
};
|
|
1610
|
-
/**
|
|
1611
|
-
* @desc Set the currently active color
|
|
1612
|
-
* @param index color index
|
|
1613
|
-
*/
|
|
1614
|
-
IroColorPicker.prototype.setActiveColor = function setActiveColor(index) {
|
|
1615
|
-
this.color = this.colors[index];
|
|
1616
|
-
if (this.state) {
|
|
1617
|
-
this.setState({ color: this.color });
|
|
1618
|
-
}
|
|
1619
|
-
// Fire color switch event
|
|
1620
|
-
this.emit('color:setActive', this.color);
|
|
1621
|
-
};
|
|
1622
|
-
/**
|
|
1623
|
-
* @desc Replace all of the current colorPicker colors
|
|
1624
|
-
* @param newColorValues list of new colors to add
|
|
1625
|
-
*/
|
|
1626
|
-
IroColorPicker.prototype.setColors = function setColors(newColorValues, activeColorIndex) {
|
|
1627
|
-
var this$1 = this;
|
|
1628
|
-
if (activeColorIndex === void 0) activeColorIndex = 0;
|
|
1629
|
-
|
|
1630
|
-
// Unbind color events
|
|
1631
|
-
this.colors.forEach(function (color) { return color.unbind(); });
|
|
1632
|
-
// Destroy old colors
|
|
1633
|
-
this.colors = [];
|
|
1634
|
-
// Add new colors
|
|
1635
|
-
newColorValues.forEach(function (colorValue) { return this$1.addColor(colorValue); });
|
|
1636
|
-
// Reset active color
|
|
1637
|
-
this.setActiveColor(activeColorIndex);
|
|
1638
|
-
this.emit('color:setAll', this.colors);
|
|
1639
|
-
};
|
|
1640
|
-
// Public ColorPicker events API
|
|
1641
|
-
/**
|
|
1642
|
-
* @desc Set a callback function for an event
|
|
1643
|
-
* @param eventList event(s) to listen to
|
|
1644
|
-
* @param callback - Function called when the event is fired
|
|
1645
|
-
*/
|
|
1646
|
-
IroColorPicker.prototype.on = function on(eventList, callback) {
|
|
1647
|
-
var this$1 = this;
|
|
1648
|
-
|
|
1649
|
-
var events = this.events;
|
|
1650
|
-
// eventList can be an eventType string or an array of eventType strings
|
|
1651
|
-
(!Array.isArray(eventList) ? [eventList] : eventList).forEach(function (eventType) {
|
|
1652
|
-
// Add event callback
|
|
1653
|
-
(events[eventType] || (events[eventType] = [])).push(callback);
|
|
1654
|
-
// Call deferred events
|
|
1655
|
-
// These are events that can be stored until a listener for them is added
|
|
1656
|
-
if (this$1.deferredEvents[eventType]) {
|
|
1657
|
-
// Deffered events store an array of arguments from when the event was called
|
|
1658
|
-
this$1.deferredEvents[eventType].forEach(function (args) {
|
|
1659
|
-
callback.apply(null, args);
|
|
1660
|
-
});
|
|
1661
|
-
// Clear deferred events
|
|
1662
|
-
this$1.deferredEvents[eventType] = [];
|
|
1663
|
-
}
|
|
1664
|
-
});
|
|
1665
|
-
};
|
|
1666
|
-
/**
|
|
1667
|
-
* @desc Remove a callback function for an event added with on()
|
|
1668
|
-
* @param eventList - event(s) to listen to
|
|
1669
|
-
* @param callback - original callback function to remove
|
|
1670
|
-
*/
|
|
1671
|
-
IroColorPicker.prototype.off = function off(eventList, callback) {
|
|
1672
|
-
var this$1 = this;
|
|
1673
|
-
|
|
1674
|
-
(!Array.isArray(eventList) ? [eventList] : eventList).forEach(function (eventType) {
|
|
1675
|
-
var callbackList = this$1.events[eventType];
|
|
1676
|
-
// this.emitHook('event:off', eventType, callback);
|
|
1677
|
-
if (callbackList) { callbackList.splice(callbackList.indexOf(callback), 1); }
|
|
1678
|
-
});
|
|
1679
|
-
};
|
|
1680
|
-
/**
|
|
1681
|
-
* @desc Emit an event
|
|
1682
|
-
* @param eventType event to emit
|
|
1683
|
-
*/
|
|
1684
|
-
IroColorPicker.prototype.emit = function emit(eventType) {
|
|
1685
|
-
var this$1 = this;
|
|
1686
|
-
var args = [], len = arguments.length - 1;
|
|
1687
|
-
while (len-- > 0) args[len] = arguments[len + 1];
|
|
1688
|
-
|
|
1689
|
-
var activeEvents = this.activeEvents;
|
|
1690
|
-
var isEventActive = activeEvents.hasOwnProperty(eventType) ? activeEvents[eventType] : false;
|
|
1691
|
-
// Prevent event callbacks from firing if the event is already active
|
|
1692
|
-
// This stops infinite loops if something in an event callback causes the same event to be fired again
|
|
1693
|
-
// (e.g. setting the color inside a color:change callback)
|
|
1694
|
-
if (!isEventActive) {
|
|
1695
|
-
activeEvents[eventType] = true;
|
|
1696
|
-
var callbackList = this.events[eventType] || [];
|
|
1697
|
-
callbackList.forEach(function (fn) { return fn.apply(this$1, args); });
|
|
1698
|
-
activeEvents[eventType] = false;
|
|
1699
|
-
}
|
|
1700
|
-
};
|
|
1701
|
-
/**
|
|
1702
|
-
* @desc Emit an event now, or save it for when the relevent event listener is added
|
|
1703
|
-
* @param eventType - The name of the event to emit
|
|
1704
|
-
*/
|
|
1705
|
-
IroColorPicker.prototype.deferredEmit = function deferredEmit(eventType) {
|
|
1706
|
-
var ref;
|
|
1707
|
-
|
|
1708
|
-
var args = [], len = arguments.length - 1;
|
|
1709
|
-
while (len-- > 0) args[len] = arguments[len + 1];
|
|
1710
|
-
var deferredEvents = this.deferredEvents;
|
|
1711
|
-
(ref = this).emit.apply(ref, [eventType].concat(args));
|
|
1712
|
-
(deferredEvents[eventType] || (deferredEvents[eventType] = [])).push(args);
|
|
1713
|
-
};
|
|
1714
|
-
// Public utility methods
|
|
1715
|
-
IroColorPicker.prototype.setOptions = function setOptions(newOptions) {
|
|
1716
|
-
this.setState(newOptions);
|
|
1717
|
-
};
|
|
1718
|
-
/**
|
|
1719
|
-
* @desc Resize the color picker
|
|
1720
|
-
* @param width - new width
|
|
1721
|
-
*/
|
|
1722
|
-
IroColorPicker.prototype.resize = function resize(width) {
|
|
1723
|
-
this.setOptions({ width: width });
|
|
1724
|
-
};
|
|
1725
|
-
/**
|
|
1726
|
-
* @desc Reset the color picker to the initial color provided in the color picker options
|
|
1727
|
-
*/
|
|
1728
|
-
IroColorPicker.prototype.reset = function reset() {
|
|
1729
|
-
this.colors.forEach(function (color) { return color.reset(); });
|
|
1730
|
-
this.setState({ colors: this.colors });
|
|
1731
|
-
};
|
|
1732
|
-
/**
|
|
1733
|
-
* @desc Called by the createWidget wrapper when the element is mounted into the page
|
|
1734
|
-
* @param container - the container element for this ColorPicker instance
|
|
1735
|
-
*/
|
|
1736
|
-
IroColorPicker.prototype.onMount = function onMount(container) {
|
|
1737
|
-
this.el = container;
|
|
1738
|
-
this.deferredEmit('mount', this);
|
|
1739
|
-
};
|
|
1740
|
-
// Internal methods
|
|
1741
|
-
/**
|
|
1742
|
-
* @desc React to a color update
|
|
1743
|
-
* @param color - current color
|
|
1744
|
-
* @param changes - shows which h,s,v,a color channels changed
|
|
1745
|
-
*/
|
|
1746
|
-
IroColorPicker.prototype.onColorChange = function onColorChange(color, changes) {
|
|
1747
|
-
this.setState({ color: this.color });
|
|
1748
|
-
if (this.inputActive) {
|
|
1749
|
-
this.inputActive = false;
|
|
1750
|
-
this.emit('input:change', color, changes);
|
|
1751
|
-
}
|
|
1752
|
-
this.emit('color:change', color, changes);
|
|
1753
|
-
};
|
|
1754
|
-
/**
|
|
1755
|
-
* @desc Handle input from a UI control element
|
|
1756
|
-
* @param type - event type
|
|
1757
|
-
*/
|
|
1758
|
-
IroColorPicker.prototype.emitInputEvent = function emitInputEvent(type, originId) {
|
|
1759
|
-
if (type === 0 /* Start */) {
|
|
1760
|
-
this.emit('input:start', this.color, originId);
|
|
1761
|
-
}
|
|
1762
|
-
else if (type === 1 /* Move */) {
|
|
1763
|
-
this.emit('input:move', this.color, originId);
|
|
1764
|
-
}
|
|
1765
|
-
else if (type === 2 /* End */) {
|
|
1766
|
-
this.emit('input:end', this.color, originId);
|
|
1767
|
-
}
|
|
1768
|
-
};
|
|
1769
|
-
IroColorPicker.prototype.render = function render(props, state) {
|
|
1770
|
-
var this$1 = this;
|
|
1771
|
-
|
|
1772
|
-
var layout = state.layout;
|
|
1773
|
-
// use layout shorthands
|
|
1774
|
-
if (!Array.isArray(layout)) {
|
|
1775
|
-
switch (layout) {
|
|
1776
|
-
// TODO: implement some?
|
|
1777
|
-
default:
|
|
1778
|
-
layout = [
|
|
1779
|
-
{ component: IroWheel },
|
|
1780
|
-
{ component: IroSlider }];
|
|
1781
|
-
}
|
|
1782
|
-
// add transparency slider to the layout
|
|
1783
|
-
if (state.transparency) {
|
|
1784
|
-
layout.push({
|
|
1785
|
-
component: IroSlider,
|
|
1786
|
-
options: {
|
|
1787
|
-
sliderType: 'alpha'
|
|
1788
|
-
}
|
|
1789
|
-
});
|
|
1790
|
-
}
|
|
1791
|
-
}
|
|
1792
|
-
return (h("div", {
|
|
1793
|
-
class: "IroColorPicker", id: state.id, style: {
|
|
1794
|
-
display: state.display
|
|
1795
|
-
}
|
|
1796
|
-
}, layout.map(function (ref, componentIndex) {
|
|
1797
|
-
var UiComponent = ref.component;
|
|
1798
|
-
var options = ref.options;
|
|
1799
|
-
|
|
1800
|
-
return (h(UiComponent, Object.assign({}, state, options, { ref: undefined, onInput: this$1.emitInputEvent.bind(this$1), parent: this$1, index: componentIndex })));
|
|
1801
|
-
})));
|
|
1802
|
-
};
|
|
1803
|
-
|
|
1804
|
-
return IroColorPicker;
|
|
1805
|
-
}(m));
|
|
1806
|
-
IroColorPicker.defaultProps = Object.assign({}, iroColorPickerOptionDefaults,
|
|
1807
|
-
{
|
|
1808
|
-
colors: [],
|
|
1809
|
-
display: 'block',
|
|
1810
|
-
id: null,
|
|
1811
|
-
layout: 'default',
|
|
1812
|
-
margin: null
|
|
1813
|
-
});
|
|
1814
|
-
var IroColorPickerWidget = createWidget(IroColorPicker);
|
|
1815
|
-
|
|
1816
|
-
var iro;
|
|
1817
|
-
(function (iro) {
|
|
1818
|
-
iro.version = "5.5.2"; // replaced by @rollup/plugin-replace; see rollup.config.js
|
|
1819
|
-
iro.Color = IroColor;
|
|
1820
|
-
iro.ColorPicker = IroColorPickerWidget;
|
|
1821
|
-
var ui;
|
|
1822
|
-
(function (ui) {
|
|
1823
|
-
ui.h = h;
|
|
1824
|
-
ui.ComponentBase = IroComponentWrapper;
|
|
1825
|
-
ui.Handle = IroHandle;
|
|
1826
|
-
ui.Slider = IroSlider;
|
|
1827
|
-
ui.Wheel = IroWheel;
|
|
1828
|
-
ui.Box = IroBox;
|
|
1829
|
-
})(ui = iro.ui || (iro.ui = {}));
|
|
1830
|
-
})(iro || (iro = {}));
|
|
1831
|
-
var iro$1 = iro;
|
|
1832
|
-
|
|
1833
|
-
return iro$1;
|
|
1834
|
-
|
|
1835
|
-
}));
|