UNPKG

@abextm/cache2

Version:

Utilities for reading OSRS "caches"

86 lines (85 loc) 2.28 kB
import { PerFileLoadable } from "../Loadable.js"; import { Typed } from "../reflect.js"; export class Underlay extends PerFileLoadable { id; constructor(id) { super(); this.id = id; } static index = 2; static archive = 1; rgb = 0; hsl; static decode(reader, id) { const v = new Underlay(id); for (let opcode; (opcode = reader.u8()) != 0;) { switch (opcode) { case 1: v.rgb = reader.u24(); break; default: throw new Error(`unknown enum opcode ${opcode}`); } } v.hsl = new UnderlayHSL(v.rgb); return v; } } export class UnderlayHSL { hue; sat; lum; hueMultiplier; constructor(rgb) { let r = ((rgb >> 16) & 255) / 256; let g = ((rgb >> 8) & 255) / 256; let b = (rgb & 255) / 256; let cMin = Math.min(r, g, b); let cMax = Math.max(r, g, b); let hue = 0; let sat = 0; let lum = (cMin + cMax) / 2; if (cMax != cMin) { if (lum < 0.5) { sat = (cMax - cMin) / (cMax + cMin); } if (lum >= 0.5) { sat = (cMax - cMin) / (2 - cMax - cMin); } if (cMax == r) { hue = (g - b) / (cMax - cMin); } else if (cMax == g) { hue = (b - r) / (cMax - cMin) + 2; } else if (cMax == b) { hue = (r - g) / (cMax - cMin) + 4; } } hue /= 6; this.sat = ~~(256 * sat); this.lum = ~~(256 * lum); if (this.sat < 0) { this.sat = 0; } else if (this.sat > 255) { this.sat = 255; } if (this.lum < 0) { this.lum = 0; } else if (this.lum > 255) { this.lum = 255; } if (lum > 0.5) { this.hueMultiplier = ~~(512 * (1 - lum) * sat); } else { this.hueMultiplier = ~~(sat * lum * 512); } if (this.hueMultiplier < 1) { this.hueMultiplier = 1; } this.hue = ~~(this.hueMultiplier * hue); } }