painbrush

1.0.21.1.0
src/pixel.js
+src/pixel.jsNew file
+39
Index: package/src/pixel.js
===================================================================
--- package/src/pixel.js
+++ package/src/pixel.js
@@ -0,0 +1,39 @@
+export const COORDS_ZERO = { x: 0, y: 0 };
+export const getPixelIndexFromCoords = (coords, { width }) => {
+    const xPosition = (coords.y * width + coords.x) * 3;
+    return xPosition;
+};
+export const getPixelXYCoords = (index, { width }) => {
+    const pixelIndex = ~~(index / 3);
+    return {
+        x: pixelIndex % width,
+        y: ~~(pixelIndex / width),
+    };
+};
+export const getPixelColor = (coords, layer) => {
+    const normalXYCoords = normalize(coords, layer);
+    if (!normalXYCoords)
+        return null;
+    const { x, y } = normalXYCoords;
+    const pos = (x + y * layer.width) * 3;
+    return [layer.data[pos], layer.data[pos + 1], layer.data[pos + 2]];
+};
+export const getPixelFromSingleChannelLayer = (coords, layer) => {
+    const normalXYCoords = normalize(coords, layer);
+    if (!normalXYCoords)
+        return null;
+    const { x, y } = normalXYCoords;
+    const pos = x + y * layer.width;
+    return layer.data[pos];
+};
+const normalize = (coords, layer) => {
+    const x = ~~coords.x;
+    const y = ~~coords.y;
+    if (x >= layer.width || x < 0) {
+        return null;
+    }
+    if (y >= layer.height || y < 0) {
+        return null;
+    }
+    return { x, y };
+};