import { getThemeName, ThemeNames } from '../../utils'; import { select } from './select'; import { color } from '../tokens'; export type { ValidHex } from '../types'; export const isHex = (str: string) => str.match(/^#(?:[0-9a-fA-F]{3}){1,2}$/); export { default as createOverrides } from './createOverrides'; export { default as hexToRgba } from './hexToRgba'; const createErrorString = (category: string, key: string, theme?: string, state?: string) => { let message = `The colour ${category}, ${key},`; if (state) { message = `${message}, ${state},`; } message = `${message} does not exist`; if (theme) { message = `${message} for theme: ${theme}`; } return message; }; export function getColour(category: any, key: any, options?: any): string { let hex; const state = options?.state; const themeName = options?.theme ?? getThemeName(); let themePalette = { ...color.lendi.base, ...color.lendi.semantic, }; switch (themeName) { case ThemeNames.Lendi: themePalette = { ...color.lendi.base, ...color.lendi.semantic, }; break; case ThemeNames.LendiNext: themePalette = { ...color.lendiNext.base, ...color.lendiNext.semantic, }; break; case ThemeNames.Domain: themePalette = { ...color.domain.base, ...color.domain.semantic, }; break; case ThemeNames.Aussie: themePalette = { ...color.aussie.base, ...color.aussie.semantic, }; break; } const merged = { ...color.common.base, ...themePalette, }; if ( category === 'border' || category === 'buttonFill' || category === 'buttonForeground' || category === 'buttonBorder' ) { // Border is a special semantic where we had nested semantic before. if (state) { hex = select(`${category}.${key}.${state}`)({ colors: merged, }); } else { hex = select(`${category}.${key}`)({ colors: merged, }) ?? select(`${category}.${key}.default`)({ colors: merged, }); } } else { hex = select(`${category}.${key}`)({ colors: merged, }); } if (!hex) { console.warn(createErrorString(category, key, themeName, options?.state)); } return hex; }