Refactor
This commit is contained in:
parent
41d4665e49
commit
969ccac690
24 changed files with 236 additions and 287 deletions
|
|
@ -1,98 +0,0 @@
|
|||
import { html } from "../model/misc";
|
||||
|
||||
export const createElement = (from: html): HTMLElement => {
|
||||
const element: HTMLElement = document.createElement("div");
|
||||
element.innerHTML = from;
|
||||
return element.firstElementChild as HTMLElement;
|
||||
};
|
||||
|
||||
export const randomFactory = seed => () =>
|
||||
((2 ** 31 - 1) & (seed = Math.imul(48271, seed))) / 2 ** 31;
|
||||
|
||||
export const fixedSeedRandom = randomFactory(42);
|
||||
|
||||
export const choose = <T>(
|
||||
list: Array<T>,
|
||||
random: () => number = fixedSeedRandom
|
||||
): T => list[randomInInterval(0, list.length, random)];
|
||||
|
||||
export const randomInInterval = (
|
||||
aClosed: number,
|
||||
bOpen: number,
|
||||
random: () => number = fixedSeedRandom
|
||||
): number => Math.floor((bOpen - aClosed) * random()) + aClosed;
|
||||
|
||||
export const sum = (list: ArrayLike<number>): number =>
|
||||
Array.prototype.reduce.call(list, (a, sum) => a + sum, 0);
|
||||
|
||||
export const getHeight = (e: HTMLElement): number => {
|
||||
const computedStyle = window.getComputedStyle(e);
|
||||
return (
|
||||
// ignores margin collapse
|
||||
e.clientHeight +
|
||||
parseInt(computedStyle.marginTop) +
|
||||
parseInt(computedStyle.marginBottom) +
|
||||
parseInt(computedStyle.borderTopWidth) +
|
||||
parseInt(computedStyle.borderBottomWidth)
|
||||
);
|
||||
};
|
||||
|
||||
export const mixColors = (
|
||||
hexColorA: string,
|
||||
hexColorB: string,
|
||||
qA: number
|
||||
): string => {
|
||||
const colorA = hexToRGB(normalizeHex(hexColorA));
|
||||
const colorB = hexToRGB(normalizeHex(hexColorB));
|
||||
const mixedColor: [number, number, number] = [
|
||||
colorA[0] * qA + colorB[0] * (1 - qA),
|
||||
colorA[1] * qA + colorB[1] * (1 - qA),
|
||||
colorA[2] * qA + colorB[2] * (1 - qA)
|
||||
];
|
||||
|
||||
return RGBToHex(mixedColor);
|
||||
};
|
||||
|
||||
const normalizeHex = (hex: string): string => {
|
||||
hex = hex.trim();
|
||||
if (hex.startsWith("#")) {
|
||||
hex = hex.substr(1);
|
||||
}
|
||||
return hex;
|
||||
};
|
||||
|
||||
const hexToRGB = (hex: string): [number, number, number] => {
|
||||
const [r1, r2, g1, g2, b1, b2] = hex;
|
||||
return [
|
||||
Number.parseInt(r1 + r2, 16),
|
||||
Number.parseInt(g1 + g2, 16),
|
||||
Number.parseInt(b1 + b2, 16)
|
||||
];
|
||||
};
|
||||
|
||||
const RGBToHex = (rgb: [number, number, number]): string =>
|
||||
rgb.map(n => Math.round(n).toString(16)).join("");
|
||||
|
||||
export const range = ({
|
||||
from = 0,
|
||||
to = Infinity,
|
||||
step = 1
|
||||
}: {
|
||||
from?: number;
|
||||
to?: number;
|
||||
step?: number;
|
||||
}): Iterable<number> => {
|
||||
return {
|
||||
*[Symbol.iterator]() {
|
||||
for (let i = from; i < to; yield i, i += step) {}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export const last = <T>(list: Array<T>): T =>
|
||||
list.length > 0 ? list[list.length - 1] : undefined;
|
||||
|
||||
export const isChrome = (): boolean =>
|
||||
!!navigator.appVersion.match(/chrome/i) &&
|
||||
// @ts-ignore
|
||||
window.webkitRequestFileSystem !== undefined;
|
||||
41
src/framework/helper/color-mixer.ts
Normal file
41
src/framework/helper/color-mixer.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
export const mixColors = (
|
||||
hexColorA: string,
|
||||
hexColorB: string,
|
||||
quantityA: number
|
||||
): string => {
|
||||
const colorA = hexToRGB(normalizeHex(hexColorA));
|
||||
const colorB = hexToRGB(normalizeHex(hexColorB));
|
||||
|
||||
const mixedColor: [number, number, number] = [
|
||||
mix(colorA[0], colorB[0], quantityA),
|
||||
mix(colorA[1], colorB[1], quantityA),
|
||||
mix(colorA[2], colorB[2], quantityA),
|
||||
];
|
||||
|
||||
return RGBToHex(mixedColor);
|
||||
};
|
||||
|
||||
const hexToRGB = ([r1, r2, g1, g2, b1, b2]: string): [
|
||||
number,
|
||||
number,
|
||||
number
|
||||
] => {
|
||||
return [
|
||||
Number.parseInt(r1 + r2, 16),
|
||||
Number.parseInt(g1 + g2, 16),
|
||||
Number.parseInt(b1 + b2, 16),
|
||||
];
|
||||
};
|
||||
|
||||
const normalizeHex = (hex: string): string => {
|
||||
hex = hex.trim();
|
||||
if (hex.startsWith('#')) {
|
||||
hex = hex.substr(1);
|
||||
}
|
||||
return hex;
|
||||
};
|
||||
|
||||
const mix = (a: number, b: number, q: number): number => a * q + b * (1 - q);
|
||||
|
||||
const RGBToHex = (rgb: [number, number, number]): string =>
|
||||
'#' + rgb.map(n => Math.round(n).toString(16)).join('');
|
||||
7
src/framework/helper/create-element.ts
Normal file
7
src/framework/helper/create-element.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { html } from '../../model/misc';
|
||||
|
||||
export const createElement = (from: html): HTMLElement => {
|
||||
const element: HTMLElement = document.createElement('div');
|
||||
element.innerHTML = from;
|
||||
return element.firstElementChild as HTMLElement;
|
||||
};
|
||||
11
src/framework/helper/get-height.ts
Normal file
11
src/framework/helper/get-height.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
export const getHeight = (e: HTMLElement): number => {
|
||||
const computedStyle = window.getComputedStyle(e);
|
||||
return (
|
||||
// ignores margin collapse
|
||||
e.clientHeight +
|
||||
parseInt(computedStyle.marginTop) +
|
||||
parseInt(computedStyle.marginBottom) +
|
||||
parseInt(computedStyle.borderTopWidth) +
|
||||
parseInt(computedStyle.borderBottomWidth)
|
||||
);
|
||||
};
|
||||
2
src/framework/helper/last.ts
Normal file
2
src/framework/helper/last.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export const last = <T>(list: Array<T>): T =>
|
||||
list.length > 0 ? list[list.length - 1] : undefined;
|
||||
17
src/framework/helper/random.ts
Normal file
17
src/framework/helper/random.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
export class Random {
|
||||
public constructor(private seed: number) {}
|
||||
|
||||
public get next(): number {
|
||||
return (
|
||||
((2 ** 31 - 1) & (this.seed = Math.imul(48271, this.seed))) / 2 ** 31
|
||||
);
|
||||
}
|
||||
|
||||
public choose<T>(list: Array<T>): T {
|
||||
return list[this.randomInInterval(0, list.length)];
|
||||
}
|
||||
|
||||
public randomInInterval(aClosed: number, bOpen: number): number {
|
||||
return Math.floor((bOpen - aClosed) * this.next) + aClosed;
|
||||
}
|
||||
}
|
||||
15
src/framework/helper/range.ts
Normal file
15
src/framework/helper/range.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
export const range = ({
|
||||
from = 0,
|
||||
to = Infinity,
|
||||
step = 1,
|
||||
}: {
|
||||
from?: number;
|
||||
to?: number;
|
||||
step?: number;
|
||||
}): Iterable<number> => {
|
||||
return {
|
||||
*[Symbol.iterator]() {
|
||||
for (let i = from; i < to; yield i, i += step) {}
|
||||
},
|
||||
};
|
||||
};
|
||||
2
src/framework/helper/sum.ts
Normal file
2
src/framework/helper/sum.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export const sum = (list: ArrayLike<number>): number =>
|
||||
Array.prototype.reduce.call(list, (a, sum) => a + sum, 0);
|
||||
Loading…
Add table
Add a link
Reference in a new issue