aboutsummaryrefslogtreecommitdiff
path: root/src/Utils.js
diff options
context:
space:
mode:
authorMartin Ashby <martin@martin-laptop.lan>2018-05-17 18:18:11 +0100
committerMartin Ashby <martin@martin-laptop.lan>2018-05-17 18:18:11 +0100
commitd40c7d9f09d9b012837d6060a4c598b23b19646f (patch)
tree8fa1f99664b3b6a74f7a0fd5856b7d0c41767b1f /src/Utils.js
parentaa86cb02fd95404fdfba8cf9d13cb5c18138b6b5 (diff)
downloadunicornpaint-d40c7d9f09d9b012837d6060a4c598b23b19646f.tar.gz
unicornpaint-d40c7d9f09d9b012837d6060a4c598b23b19646f.tar.bz2
unicornpaint-d40c7d9f09d9b012837d6060a4c598b23b19646f.tar.xz
unicornpaint-d40c7d9f09d9b012837d6060a4c598b23b19646f.zip
Palette, tools, load & save working
Diffstat (limited to 'src/Utils.js')
-rw-r--r--src/Utils.js103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/Utils.js b/src/Utils.js
new file mode 100644
index 0000000..f7bba5c
--- /dev/null
+++ b/src/Utils.js
@@ -0,0 +1,103 @@
+/**
+ * Converts RGB array to named object
+ * @param {[]} item, a length 3 array containing 8 bit RGB value
+ */
+function rgb(item) {
+ return {
+ r: item[0],
+ g: item[1],
+ b: item[2]
+ }
+}
+
+/**
+ * Converts XY array to named object
+ * @param {[]} item, a 2 length array containing x and y values
+ */
+function xy(item) {
+ return {
+ x: item[0],
+ y: item[1]
+ }
+}
+
+function colorEqual(item1, item2) {
+ if (!item1 || !item2) {
+ return false
+ }
+ if (Array.isArray(item1)) {
+ item1 = rgb(item1)
+ }
+ if (Array.isArray(item2)) {
+ item2 = rgb(item2)
+ }
+ return item1.r === item2.r
+ && item1.g === item2.g
+ && item1.b === item2.b
+}
+
+function coordsEqual(item1, item2) {
+ if (!item1 || !item2) {
+ return false
+ }
+
+ if (Array.isArray(item1)) {
+ item1 = xy(item1)
+ }
+ if (Array.isArray(item2)) {
+ item2 = xy(item2)
+ }
+ return item1.x === item2.x
+ && item1.y === item2.y
+}
+
+function getPixel(x, y, pixels) {
+ let row = pixels[y]
+ if (!row) {
+ return
+ }
+ return row[x]
+}
+
+function findContiguousPixels(x, y, pixels, targetColor = getPixel(x, y, pixels), contiguousPixels=[[x, y]]) {
+ let adjescent = [
+ [x-1, y],
+ [x+1, y],
+ [x, y-1],
+ [x, y+1]
+ ]
+
+ adjescent.forEach((coord) => {
+ let px = xy(coord)
+ let pxCol = getPixel(px.x, px.y, pixels)
+ if (!pxCol) {
+ return
+ }
+
+ // add adjescents uniquely if they are the target color
+ let ix = contiguousPixels.findIndex((existingCoord) => coordsEqual(coord, existingCoord))
+ if (ix !== -1) {
+ return
+ }
+
+ if (!colorEqual(pxCol, targetColor)) {
+ return
+ }
+ contiguousPixels.push(coord)
+ let morePixels = findContiguousPixels(px.x, px.y, pixels, targetColor, contiguousPixels)
+ contiguousPixels.concat(morePixels)
+ })
+
+ return contiguousPixels
+}
+
+
+export {
+ xy,
+ rgb,
+ colorEqual,
+ coordsEqual,
+ findContiguousPixels,
+ getPixel
+}
+