fix imports

This commit is contained in:
casperlamboo 2017-11-14 15:39:24 +01:00
parent 790948ccc6
commit 08e88805c4
3 changed files with 97 additions and 1 deletions

View File

@ -0,0 +1,41 @@
/* make sure the given function only returns a new array when:
* - the resulting array has a different length
* - items in the array changed
*/
export default function arrayMemoizer(fn) {
let prevArgs;
let prevResult;
return function memoized() {
// if same arguments; return prev result;
if (prevArgs && shallowEquals(arguments, prevArgs) && prevResult) {
return prevResult;
}
prevArgs = arguments;
const result = fn.apply(this, arguments);
// if no prevResult; return new result
if (!prevResult) {
prevResult = result;
return prevResult;
}
if (shallowEquals(result, prevResult)) {
// if array isn't changed; return prev result
return prevResult;
} else {
// if changed; return new result
prevResult = result;
return prevResult;
}
};
}
function shallowEquals(arr1, arr2) {
if (arr1.length !== arr2.length) return false;
for (let i in arr1) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
}

55
src/utils/colorUtils.js Normal file
View File

@ -0,0 +1,55 @@
import * as THREE from 'three';
export function getPixelHue(imageData, index) {
const { data } = imageData;
const i = index * 4;
const red = data[i];
const green = data[i + 1];
const blue = data[i + 2];
const min = Math.min(red, green, blue);
const max = Math.max(red, green, blue);
let hue;
if (max === red) {
hue = (green - blue) / (max - min);
} else if (max === green) {
hue = 2.0 + (blue - red) / (max - min);
} else {
hue = 4.0 + (red - green) / (max - min);
}
hue *= 60.0;
if (hue < 0) hue += 360.0;
return hue;
}
export function getPixelBrightness(imageData, index) {
const { data } = imageData;
const i = index * 4;
const red = data[i];
const green = data[i + 1];
const blue = data[i + 2];
return red * 0.2989 + green * 0.5870 + blue * 0.1140;
}
export function getPixel(imageData, index) {
const { data } = imageData;
const i = index * 4;
return {
r: data[i],
g: data[i + 1],
b: data[i + 2]
};
}
const TEMP_COLOR = new THREE.Color();
export const hexToStyle = (hex) => TEMP_COLOR.setHex(hex).getStyle();

View File

@ -1,5 +1,5 @@
import * as THREE from 'three'; import * as THREE from 'three';
import { shapeToPoints, getPointsBounds } from '../shape/DataUtils.js' import { shapeToPoints, getPointsBounds } from '../shape/shapeDataUtils.js'
import { Vector } from 'cal'; import { Vector } from 'cal';
import arrayMemoizer from './arrayMemoizer.js'; import arrayMemoizer from './arrayMemoizer.js';
import memoize from 'memoizee'; import memoize from 'memoizee';