Skip to content

Instantly share code, notes, and snippets.

@lucasdamianjohnson
Last active November 21, 2021 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasdamianjohnson/55fcb6b8823c59131d474a60c326510f to your computer and use it in GitHub Desktop.
Save lucasdamianjohnson/55fcb6b8823c59131d474a60c326510f to your computer and use it in GitHub Desktop.
Helpful Functions
export type PositionMatrix = {
x : number,y : number,z :number
}
export type RotationMatrix = {
x : number,y : number,z :number
}
export type ColorMatrix3 = {
r : number,g : number,b :number
}
export type ColorMatrix4 = {
r : number,g : number,b :number,a:number
}
/**# Util Helper
* ---
* Used to hold useful functions that are used commonly
*/
export interface UtilHelperInterface {
htmlFromString(html: string): Element;
getID(): string;
unqiueId(): string;
cloneJson(obj: any): any;
degtoRad(degrees: number): number;
radToDeg(radians: number): number;
RGBtoRatio(color: ColorMatrix3): ColorMatrix3;
HEXToRGB(hexString: string): ColorMatrix3;
HSLToRGB(h: number, s: number, l: number): ColorMatrix3;
HSVToRGB(h: number, s: number, v: number): ColorMatrix3;
RGBToHEX(color3: ColorMatrix3): string;
arrayEquals(a: any[], b: any[]): boolean;
getRange(num1: number, num2: number): number;
probability(n: number): boolean;
}
export class UtilHelper implements UtilHelperInterface {
constructor(private DS: DivineInterface) {}
htmlFromString(html: string): Element {
var template = document.createElement("div");
html = html.trim(); // Never return a text node of whitespace as the result
template.innerHTML = html;
if (!template.firstElementChild) {
return template;
} else {
return template.firstElementChild;
}
}
getID() {
return `${this.unqiueId()}-${this.unqiueId()}`;
}
unqiueId() {
return Math.floor((1 + Math.random()) * 0x1000000).toString(16);
}
cloneJson(obj: any): any {
return JSON.parse(JSON.stringify(obj));
}
degtoRad(degrees: number) {
return degrees * (Math.PI / 180);
}
radToDeg(radians: number) {
return radians * (180 / Math.PI);
}
/**
*Found these color conversion functions here:
* https://gist.github.com/mjackson/5311256
*
*
*/
HEXToRGB(hexString: string): ColorMatrix3 {
hexString = hexString.replace("#","");
const hexToRGB = hexString.match(/.{1,2}/g);
if (!hexToRGB)
return {
r: 0,
g: 0,
b: 0,
};
return {
r: parseInt(hexToRGB[0], 16),
g: parseInt(hexToRGB[1], 16),
b: parseInt(hexToRGB[2], 16),
};
}
HSLToRGB(h: number, s: number, l: number): ColorMatrix3 {
var r, g, b;
if (s == 0) {
r = g = b = l;
} else {
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
const p = 2 * l - q;
r = this._hue2rgb(p, q, h + 1 / 3);
g = this._hue2rgb(p, q, h);
b = this._hue2rgb(p, q, h - 1 / 3);
}
return { r: r * 255, g: g * 255, b: b * 255 };
}
_hue2rgb(p: number, q: number, t: number) {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1 / 6) return p + (q - p) * 6 * t;
if (t < 1 / 2) return q;
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
return p;
}
HSVToRGB(h: number, s: number, v: number): ColorMatrix3 {
let r, g, b;
const i = (h * 6) >>> 0;
const f = h * 6 - i;
const p = v * (1 - s);
const q = v * (1 - f * s);
const t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0:
(r = v), (g = t), (b = p);
break;
case 1:
(r = q), (g = v), (b = p);
break;
case 2:
(r = p), (g = v), (b = t);
break;
case 3:
(r = p), (g = q), (b = v);
break;
case 4:
(r = t), (g = p), (b = v);
break;
case 5:
(r = v), (g = p), (b = q);
break;
}
if (!r) {
r = 0;
}
if (!g) {
g = 0;
}
if (!b) {
b = 0;
}
return { r: r * 255, g: g * 255, b: b * 255 };
}
RGBToHEX(color3 : ColorMatrix3) : string {
function _decToHex(value : number) {
if (value > 255) {
return 'FF';
} else if (value < 0) {
return '00';
} else {
return value.toString(16).padStart(2, '0').toUpperCase();
}
}
return '#' + _decToHex(color3.r) + _decToHex(color3.g) + _decToHex(color3.b);
}
RGBtoRatio(color : ColorMatrix3): ColorMatrix3 {
return {
r: color.r / 255,
g: color.g / 255,
b: color.b / 255,
};
}
arrayEquals(a: any[], b: any[]): boolean {
return (
Array.isArray(a) &&
Array.isArray(b) &&
a.length === b.length &&
a.every((val, index) => val === b[index])
);
}
getRange(num1: number, num2: number): number {
return num1 + Math.floor(Math.random() * num2 + Math.random() * 1);
}
probability(n: number): boolean {
return !!n && Math.random() <= n;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment