MCPcopy Create free account
hub / github.com/adobe/react-spectrum / getColorRgb

Function getColorRgb

packages/dev/s2-docs/src/color.macro.ts:45–74  ·  view source on GitHub ↗

* Gets the RGB values for a color token. * * @param tokenName - The token name to look up. * @param mode - 'light' or 'dark' mode. * @returns [r, g, b] or null if not found.

(
  tokenName: string,
  mode: 'light' | 'dark' = 'light'
)

Source from the content-addressed store, hash-verified

43 * @returns [r, g, b] or null if not found.
44 */
45function getColorRgb(
46 tokenName: string,
47 mode: 'light' | 'dark' = 'light'
48): [number, number, number] | null {
49 const token = (tokens as any)[tokenName];
50 if (!token) {
51 return null;
52 }
53
54 let value: string | undefined;
55 if (token.sets?.[mode]?.value) {
56 value = token.sets[mode].value;
57 } else if (token.sets?.light?.value) {
58 // Fallback to light
59 value = token.sets.light.value;
60 } else if (token.value) {
61 value = token.value;
62 }
63
64 if (!value) {
65 return null;
66 }
67
68 // Parse rgb(r, g, b) or rgba(r, g, b, a)
69 const match = value.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/);
70 if (match) {
71 return [parseInt(match[1], 10), parseInt(match[2], 10), parseInt(match[3], 10)];
72 }
73 return null;
74}
75
76// Background color token mappings (semantic and base layer colors)
77const backgroundColorTokens: Record<string, string> = {

Callers 1

buildColorMapForModeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected