beuiChartColor function
Parses chart CSS colors, including hex, RGB/HSL, named colors, and the
chart theme's supported var(--token) aliases.
CSS expressions requiring a browser style context (for example var()
fallbacks, color(), color-mix(), and relative colors) cannot be resolved
here and use fallback (or the primary theme color). currentColor maps
to the chart foreground token because this API has no DOM inheritance tree.
Implementation
Color beuiChartColor(String? css, BeuiColors colors, {Color? fallback}) {
if (css == null || css.isEmpty) return fallback ?? colors.primary;
final trimmed = css.trim();
if (trimmed.startsWith('var(')) {
return switch (trimmed) {
'var(--muted-foreground)' => colors.mutedForeground,
'var(--foreground)' => colors.foreground,
'var(--background)' => colors.background,
'var(--muted)' => colors.muted,
'var(--border)' => colors.border,
'var(--primary)' => colors.primary,
'var(--accent)' => colors.accent,
'var(--success)' => colors.success,
'var(--warning)' => colors.warning,
'var(--destructive)' => colors.destructive,
_ => fallback ?? colors.primary,
};
}
final lower = trimmed.toLowerCase();
if (lower == 'currentcolor') return colors.foreground;
if (lower == 'transparent') return const Color(0x00000000);
return _parseHex(trimmed) ??
_parseRgb(trimmed) ??
_parseHsl(trimmed) ??
(_cssNamedColors[lower] == null
? null
: Color(0xFF000000 | _cssNamedColors[lower]!)) ??
fallback ??
colors.primary;
}