Get color value programmatically when it's a reference theme
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In many design systems, a color is not stored as a final hex value at the point where you use it. Instead, it is stored as a theme reference such as primary, surface, or --brand-accent, and your code has to resolve that reference to the concrete color value that is active in the current theme.
Theme References Versus Raw Colors
A theme reference is a level of indirection. Instead of saying "use #2563eb here," the component says "use the theme's primary color here."
That gives you:
- centralized control over colors
- dark-mode and light-mode switching
- easier brand changes
- less duplication in component code
The tradeoff is that programmatic color access now requires resolution, not a hardcoded lookup.
Example With CSS Custom Properties
On the web, a very common theme-reference mechanism is CSS custom properties.
The important point is that you do not read the string var(--primary-color) from the component. You read the computed style or the variable value from the active theme context.
Resolving a Color Applied to an Element
Sometimes you do not want the theme token itself. You want the actual computed color applied to a specific element after all inheritance and overrides.
This returns the final computed value, often in rgb(...) form.
That distinction matters:
- read the variable if you want the theme token's current value
- read the computed style if you want the actual resolved style on an element
Nested Theme Objects in JavaScript
Some applications keep theme values in JavaScript objects instead of CSS variables.
This is the same idea in a different container: resolve the reference through the theme map before trying to use the final color.
Handling Fallbacks Safely
Theme references sometimes point to missing keys because of typos, partial themes, or version drift. Add a fallback path so your UI does not silently break.
This is especially important in component libraries where themes may come from application code you do not control directly.
Common Pitfalls
The most common mistake is treating the reference name as if it were already the final color. A token like primary is only meaningful if you resolve it through the active theme.
Another issue is reading inline style strings instead of computed values. If the color comes from inheritance, CSS variables, or a higher-level theme provider, raw attributes may not show the real applied color.
A third pitfall is forgetting that the resolved value may change at runtime when the user switches theme mode. If your code caches a color too aggressively, it can go stale.
Finally, do not assume every resolved color will be in hex form. Browser APIs often return computed colors as rgb(...), which is normal and still represents the same value.
Summary
- Theme references are indirections, not final color values.
- Resolve them through the active theme source, such as CSS variables or a theme object.
- Use computed styles when you need the final color actually applied to an element.
- Add fallbacks for missing or invalid theme keys.
- Expect the resolved format to vary, especially in browser APIs.

