mirror of
https://github.com/Doodle3D/Doodle3D-Core.git
synced 2024-12-22 11:03:48 +01:00
fix imports
This commit is contained in:
parent
790948ccc6
commit
08e88805c4
41
src/utils/arrayMemoizer.js
Normal file
41
src/utils/arrayMemoizer.js
Normal 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
55
src/utils/colorUtils.js
Normal 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();
|
@ -1,5 +1,5 @@
|
||||
import * as THREE from 'three';
|
||||
import { shapeToPoints, getPointsBounds } from '../shape/DataUtils.js'
|
||||
import { shapeToPoints, getPointsBounds } from '../shape/shapeDataUtils.js'
|
||||
import { Vector } from 'cal';
|
||||
import arrayMemoizer from './arrayMemoizer.js';
|
||||
import memoize from 'memoizee';
|
||||
|
Loading…
Reference in New Issue
Block a user