UNPKG

lifehash

Version:

TypeScript/JavaScript implementation of LifeHash, a visual hash algorithm

64 lines (63 loc) 1.87 kB
import { Point } from './Point.js'; import { Colors } from './constants.js'; export class Grid { constructor(size) { this.size = size; this._capacity = size.width * size.height; this._storage = new Array(this._capacity).fill(0); this._maxX = size.width - 1; this._maxY = size.height - 1; } _offset(point) { return point.y * this.size.width + point.x; } circular_index(index, modulus) { return (index + modulus) % modulus; } set_all(val) { this._storage = new Array(this._capacity).fill(val); } set_value(val, point) { const offset = this._offset(point); this._storage[offset] = val; } get_value(point) { return this._storage[this._offset(point)]; } *get_points() { for (let y = 0; y < this._maxY + 1; y += 1) { for (let x = 0; x < this._maxX + 1; x += 1) { yield new Point(x, y); } } } *get_neighborhood(point) { for (let oy = -1; oy <= 1; oy += 1) { for (let ox = -1; ox <= 1; ox += 1) { const o = new Point(ox, oy); const px = this.circular_index(ox + point.x, this.size.width); const py = this.circular_index(oy + point.y, this.size.height); const p = new Point(px, py); yield [o, p]; } } } color_for_value(val) { if (val) { return Colors.white; } else { return Colors.black; } } colors() { const result = []; for (const color_value of this._storage) { const color = this.color_for_value(color_value); result.push(color.r); result.push(color.g); result.push(color.b); } return result; } }