From 86fc3cf7e966caa5369753b7c36e52108516b518 Mon Sep 17 00:00:00 2001 From: Jan Odvarko Date: Sat, 21 Aug 2021 10:40:15 +0200 Subject: [PATCH 1/6] Added property 'random' and method randomize() --- jscolor.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/jscolor.js b/jscolor.js index 3b51843..0be015f 100644 --- a/jscolor.js +++ b/jscolor.js @@ -1671,6 +1671,7 @@ var jsc = { this.format = 'auto'; // 'auto' | 'any' | 'hex' | 'hexa' | 'rgb' | 'rgba' - Format of the input/output value this.value = undefined; // INITIAL color value in any supported format. To change it later, use method fromString(), fromHSVA(), fromRGBA() or channel() this.alpha = undefined; // INITIAL alpha value. To change it later, call method channel('A', ) + this.random = false; // whether to randomize the initial color. Either true | false, or an array of ranges: [minV, maxV, minS, maxS, minH, maxH, minA, maxA] this.onChange = undefined; // called when color changes. Value can be either a function or a string with JS code. this.onInput = undefined; // called repeatedly as the color is being changed, e.g. while dragging a slider. Value can be either a function or a string with JS code. this.valueElement = undefined; // element that will be used to display and input the color value @@ -2001,6 +2002,25 @@ var jsc = { }; + this.randomize = function (minV, maxV, minS, maxS, minH, maxH, minA, maxA) { + if (minV === undefined) { minV = 0; } + if (maxV === undefined) { maxV = 100; } + if (minS === undefined) { minS = 0; } + if (maxS === undefined) { maxS = 100; } + if (minH === undefined) { minH = 0; } + if (maxH === undefined) { maxH = 359; } + if (minA === undefined) { minA = 1; } + if (maxA === undefined) { maxA = 1; } + + this.fromHSVA( + minH + Math.floor(Math.random() * (maxH - minH + 1)), + minS + Math.floor(Math.random() * (maxS - minS + 1)), + minV + Math.floor(Math.random() * (maxV - minV + 1)), + ((100 * minA) + Math.floor(Math.random() * (100 * (maxA - minA) + 1))) / 100 + ); + }; + + this.toString = function (format) { if (format === undefined) { format = this.getFormat(); // format not specified -> use the current format @@ -3277,12 +3297,17 @@ var jsc = { // let's also parse and expose the initial alpha value, if any // // Note: If the initial color value contains alpha value in it (e.g. in rgba format), - // this will overwrite it. So we should only process alpha input if there was any initial + // this will overwrite it. So we should only process alpha input if there was initial // alpha explicitly set, otherwise we could needlessly lose initial value's alpha if (initAlpha !== undefined) { this.processAlphaInput(initAlpha); } + if (this.random) { + // randomize the initial color value + this.randomize.apply(this, Array.isArray(this.random) ? this.random : []); + } + } }; From cc1f88eb2869dd5d1e5655de2a5c33abb082c932 Mon Sep 17 00:00:00 2001 From: Jan Odvarko Date: Fri, 4 Feb 2022 18:54:35 +0100 Subject: [PATCH 2/6] removed captureTarget/releaseTarget (no longer needed) --- jscolor.js | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/jscolor.js b/jscolor.js index 0be015f..5fdcca1 100644 --- a/jscolor.js +++ b/jscolor.js @@ -378,24 +378,6 @@ var jsc = { }, - captureTarget : function (target) { - // IE - if (target.setCapture) { - jsc._capturedTarget = target; - jsc._capturedTarget.setCapture(); - } - }, - - - releaseTarget : function () { - // IE - if (jsc._capturedTarget) { - jsc._capturedTarget.releaseCapture(); - jsc._capturedTarget = null; - } - }, - - triggerEvent : function (el, eventName, bubbles, cancelable) { if (!el) { return; @@ -1231,7 +1213,6 @@ var jsc = { _pointerOrigin : null, - _capturedTarget : null, onDocumentKeyUp : function (e) { @@ -1296,7 +1277,6 @@ var jsc = { var thisObj = jsc.getData(target, 'instance'); jsc.preventDefault(e); - jsc.captureTarget(target); var registerDragEvents = function (doc, offset) { jsc.attachGroupEvent('drag', doc, jsc._pointerMoveEvent[pointerType], @@ -1366,7 +1346,6 @@ var jsc = { return function (e) { var thisObj = jsc.getData(target, 'instance'); jsc.detachGroupEvents('drag'); - jsc.releaseTarget(); // Always trigger changes AFTER detaching outstanding mouse handlers, // in case some color change that occured in user-defined onChange/onInput handler From f145639f60549b69ca2490d436086f202dc3efa4 Mon Sep 17 00:00:00 2001 From: Tobias Speicher Date: Thu, 24 Mar 2022 15:02:29 +0100 Subject: [PATCH 3/6] refactor: replace deprecated String.prototype.substr() .substr() is deprecated so we replace it with .slice() which works similarily but isn't deprecated Signed-off-by: Tobias Speicher --- jscolor.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/jscolor.js b/jscolor.js index 5fdcca1..fcb5cb0 100644 --- a/jscolor.js +++ b/jscolor.js @@ -559,19 +559,19 @@ var jsc = { hexColor : function (r, g, b) { return '#' + ( - ('0' + Math.round(r).toString(16)).substr(-2) + - ('0' + Math.round(g).toString(16)).substr(-2) + - ('0' + Math.round(b).toString(16)).substr(-2) + ('0' + Math.round(r).toString(16)).slice(-2) + + ('0' + Math.round(g).toString(16)).slice(-2) + + ('0' + Math.round(b).toString(16)).slice(-2) ).toUpperCase(); }, hexaColor : function (r, g, b, a) { return '#' + ( - ('0' + Math.round(r).toString(16)).substr(-2) + - ('0' + Math.round(g).toString(16)).substr(-2) + - ('0' + Math.round(b).toString(16)).substr(-2) + - ('0' + Math.round(a * 255).toString(16)).substr(-2) + ('0' + Math.round(r).toString(16)).slice(-2) + + ('0' + Math.round(g).toString(16)).slice(-2) + + ('0' + Math.round(b).toString(16)).slice(-2) + + ('0' + Math.round(a * 255).toString(16)).slice(-2) ).toUpperCase(); }, @@ -778,19 +778,19 @@ var jsc = { // 8-char notation (= with alpha) ret.format = 'hexa'; ret.rgba = [ - parseInt(m[1].substr(0,2),16), - parseInt(m[1].substr(2,2),16), - parseInt(m[1].substr(4,2),16), - parseInt(m[1].substr(6,2),16) / 255 + parseInt(m[1].slice(0,2),16), + parseInt(m[1].slice(2,4),16), + parseInt(m[1].slice(4,6),16), + parseInt(m[1].slice(6,8),16) / 255 ]; } else if (m[1].length === 6) { // 6-char notation ret.format = 'hex'; ret.rgba = [ - parseInt(m[1].substr(0,2),16), - parseInt(m[1].substr(2,2),16), - parseInt(m[1].substr(4,2),16), + parseInt(m[1].slice(0,2),16), + parseInt(m[1].slice(2,4),16), + parseInt(m[1].slice(4,6),16), null ]; From f96eabb1bf4fe13c57c5ce074afe6790002f7b0b Mon Sep 17 00:00:00 2001 From: Jan Odvarko Date: Thu, 24 Mar 2022 15:47:44 +0100 Subject: [PATCH 4/6] incremented version number --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 513ba6f..80fb9f7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@eastdesire/jscolor", - "version": "2.4.7", + "version": "2.4.8", "description": "Web color picker with opacity channel (alpha channel) and custom palette. Supports CSS colors such as rgba() and hex, including #rrggbbaa.", "main": "jscolor.js", "scripts": { From e18f85ac8f951e52fcbd2c7ca0ba7fde8447bae5 Mon Sep 17 00:00:00 2001 From: Andrew Cartwright Date: Mon, 27 Nov 2023 13:05:44 +0000 Subject: [PATCH 5/6] Add support for initing jscolor after domcontentloaded has fired --- jscolor.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/jscolor.js b/jscolor.js index 3270fb2..2e46315 100644 --- a/jscolor.js +++ b/jscolor.js @@ -52,7 +52,11 @@ var jsc = { register : function () { if (typeof window !== 'undefined' && window.document) { - window.document.addEventListener('DOMContentLoaded', jsc.pub.init, false); + if (window.document.readyState !== 'loading') { + jsc.pub.init(); + } else { + window.document.addEventListener('DOMContentLoaded', jsc.pub.init, false); + } } }, From 019fd657dffc77612702acf42cbcccb407d64fa3 Mon Sep 17 00:00:00 2001 From: Jan Odvarko Date: Sat, 2 Dec 2023 01:27:02 +0100 Subject: [PATCH 6/6] Fixed indentation --- jscolor.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jscolor.js b/jscolor.js index 2e46315..6556cba 100644 --- a/jscolor.js +++ b/jscolor.js @@ -53,9 +53,9 @@ var jsc = { register : function () { if (typeof window !== 'undefined' && window.document) { if (window.document.readyState !== 'loading') { - jsc.pub.init(); + jsc.pub.init(); } else { - window.document.addEventListener('DOMContentLoaded', jsc.pub.init, false); + window.document.addEventListener('DOMContentLoaded', jsc.pub.init, false); } } },