ldc-showroom/classes/ToolboxHelper.ts
2024-01-31 16:59:54 +01:00

55 lines
1.9 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;
}
public static trans(text: string, params: Record<string,string> = {}, localeToUse: string|null = null): string {
const { t } = useI18n({ useScope: "global"});
if(localeToUse) {
return t(text, params, {
locale: localeToUse,
});
}
return t(text, params);
}
public static getTranslatableField(model: Typei18nModel, field: Typei18nFieldList): string {
const { locale, fallbackLocale } = useAppStore().getI18n();
const i18nField: Typei18nField | null = (model[field as keyof typeof model] !== undefined ?
model[field as keyof typeof model] : null) as Typei18nField | null;
if (!i18nField) {
return "undefined";
}
return i18nField[locale] !== undefined ? i18nField[locale] : i18nField[fallbackLocale];
}
}