This commit is contained in:
Schmelczer András 2020-01-06 21:40:25 +01:00
parent f054546aa6
commit 48a55a4a97
51 changed files with 604 additions and 577 deletions

View file

@ -1,6 +1,7 @@
import { html } from '../../model/misc';
export const createElement = (from: html): HTMLElement => {
// won't work for all elements, eg.: <td>
const element: HTMLElement = document.createElement('div');
element.innerHTML = from;
return element.firstElementChild as HTMLElement;

View file

@ -1,7 +1,7 @@
export const getHeight = (e: HTMLElement): number => {
// ignores margin collapse
const computedStyle = window.getComputedStyle(e);
return (
// ignores margin collapse
e.clientHeight +
parseInt(computedStyle.marginTop) +
parseInt(computedStyle.marginBottom) +

View file

@ -1,2 +1,2 @@
export const last = <T>(list: Array<T>): T =>
list.length > 0 ? list[list.length - 1] : undefined;
list.length >= 1 ? list[list.length - 1] : undefined;

View file

@ -1,25 +1,25 @@
export const mixColors = (
hexColorA: string,
hexColorB: string,
quantityA: number
): string => {
const colorA = hexToRGB(normalizeHex(hexColorA));
const colorB = hexToRGB(normalizeHex(hexColorB));
export type hex = string;
export type rgb = [number, number, number];
const mixedColor: [number, number, number] = [
export const mixColors = (
hexColorA: hex,
hexColorB: hex,
quantityA: number
): hex => {
const colorA = hexToRGB(hexColorA);
const colorB = hexToRGB(hexColorB);
const mixedColor: rgb = [
mix(colorA[0], colorB[0], quantityA),
mix(colorA[1], colorB[1], quantityA),
mix(colorA[2], colorB[2], quantityA),
];
return RGBToHex(mixedColor);
return rgbToHex(mixedColor);
};
const hexToRGB = ([r1, r2, g1, g2, b1, b2]: string): [
number,
number,
number
] => {
const hexToRGB = (hex: hex): rgb => {
const [r1, r2, g1, g2, b1, b2] = normalizeHex(hex);
return [
Number.parseInt(r1 + r2, 16),
Number.parseInt(g1 + g2, 16),
@ -27,7 +27,7 @@ const hexToRGB = ([r1, r2, g1, g2, b1, b2]: string): [
];
};
const normalizeHex = (hex: string): string => {
const normalizeHex = (hex: hex): hex => {
hex = hex.trim();
if (hex.startsWith('#')) {
hex = hex.substr(1);
@ -37,5 +37,5 @@ const normalizeHex = (hex: string): string => {
const mix = (a: number, b: number, q: number): number => a * q + b * (1 - q);
const RGBToHex = (rgb: [number, number, number]): string =>
const rgbToHex = (rgb: rgb): hex =>
'#' + rgb.map(n => Math.round(n).toString(16)).join('');

View file

@ -2,16 +2,17 @@ export class Random {
public constructor(private seed: number) {}
public get next(): number {
// result is in [0, 1)
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)];
return list[Math.floor(this.randomInInterval(0, list.length))];
}
public randomInInterval(aClosed: number, bOpen: number): number {
return Math.floor((bOpen - aClosed) * this.next) + aClosed;
return (bOpen - aClosed) * this.next + aClosed;
}
}

View file

@ -1,15 +0,0 @@
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) {}
},
};
};