ldc-showroom/classes/ToolboxHelper.ts
2024-01-15 17:53:16 +01:00

33 lines
1.1 KiB
TypeScript

export default class ToolboxHelper {
/**
* Get the contrast for any color
*
* @param hexcolor An HTML Hex color
* @returns
*/
public static getContrastYIQ(hexcolor: string): "#000000"|"#FFFFFF" {
const r = parseInt(hexcolor.substring(1,3),16);
const g = parseInt(hexcolor.substring(3,5),16);
const b = parseInt(hexcolor.substring(5,7),16);
const yiq = ((r*299)+(g*587)+(b*114))/1000;
return (yiq >= 128) ? "#000000" : "#FFFFFF";
}
/**
* Convert HTML Hex color to RGBA
*
* @param hexcolor An HTML Hex color
* @param alpha The transparency to apply
* @returns
*/
public static toRGBA(hexcolor: string, alpha: number = 1): string {
const r = parseInt(hexcolor.substring(1,3),16);
const g = parseInt(hexcolor.substring(3,5),16);
const b = parseInt(hexcolor.substring(5,7),16);
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}
public static randomInteger(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
}