From a86afa3e8790251d22a3a7ed1c039f84a82f9138 Mon Sep 17 00:00:00 2001 From: Wellington Soares Date: Thu, 17 Sep 2020 08:05:20 -0700 Subject: [PATCH] Updating the gradient method to accept Uint8Array --- simpleheat.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/simpleheat.js b/simpleheat.js index dc06eb9..ad393aa 100644 --- a/simpleheat.js +++ b/simpleheat.js @@ -83,15 +83,27 @@ simpleheat.prototype = { canvas.width = 1; canvas.height = 256; - for (var i in grad) { - gradient.addColorStop(+i, grad[i]); - } + if (Object.prototype.toString.call(grad) === '[object Object]') { - ctx.fillStyle = gradient; - ctx.fillRect(0, 0, 1, 256); + for (var i in grad) { + gradient.addColorStop(+i, grad[i]); + } - this._grad = ctx.getImageData(0, 0, 1, 256).data; + ctx.fillStyle = gradient; + ctx.fillRect(0, 0, 1, 256); + + } else if (Object.prototype.toString.call(grad) === '[object Uint8Array]') { + + canvas.width = 256; + const imgData = ctx.createImageData(1, 256); + imgData.data.set(grad); + ctx.putImageData(imgData, 0, 0); + } else { + throw new Error('Color Scale is undefined.'); + } + + this._grad = ctx.getImageData(0, 0, 1, 256).data; return this; },