Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
b2a8306
Setup Storybook
ciampo Apr 9, 2025
892b45f
Add saxon prototype
ciampo Apr 9, 2025
654e2cb
Add storybook example
ciampo Apr 9, 2025
cef7df6
DRY color generation
ciampo Apr 10, 2025
d33f909
Add scheme to storybook controls
ciampo Apr 14, 2025
75e6c72
Move to a8c folder
ciampo Apr 14, 2025
c80f2dc
Copy radix color generation algo
ciampo Apr 14, 2025
3175b65
Fix ts errors
ciampo Apr 14, 2025
16448f0
Generate neutral and primary scales from Radix and output as css vars
ciampo Apr 14, 2025
95390a3
Fix ts errors in Storybook
ciampo Apr 14, 2025
7d8a224
Update Storybook to show the diff between a8c and radix colors
ciampo Apr 14, 2025
f67a441
Generate tokens from both a8c and radix color scales
ciampo Apr 14, 2025
2ec465e
Fix mapColors recursion
ciampo Apr 14, 2025
9a17dce
Update storybook CSS var prefixes
ciampo Apr 14, 2025
3f1242f
Remove unneeded color gen algos, tidy up
ciampo Apr 22, 2025
de5ea27
Improve Storybook
ciampo Apr 22, 2025
70a0c01
Reorganize color mapping code
ciampo Apr 23, 2025
663d79d
Move background assignment to algos file
ciampo Apr 23, 2025
ac1924c
Object destructure color gen arguments
ciampo Apr 23, 2025
282b579
Memoize computation of color tokens
ciampo Apr 23, 2025
c51385f
Fix some missing changes from previous background refactor
ciampo Apr 23, 2025
171e58e
Remove wide gamut and alpha -based colors
ciampo Apr 23, 2025
11753c0
Refactor how primary color's contrast text is computed
ciampo Apr 23, 2025
18159a5
Tweak Storybook
ciampo Apr 23, 2025
db3b25d
Add sample test to check text color contrast to Storybook
ciampo Apr 23, 2025
a5006d2
Typo
ciampo Apr 23, 2025
a24fdd3
Make local copy of preset scales, remove radix npm dependency
ciampo Apr 23, 2025
19e8901
Generate info / success / warning / error scales + tokens
ciampo Apr 23, 2025
55fa5ab
Shift color scale indexes
ciampo Apr 23, 2025
202e7e4
Use WP components to quickly demo theming UI
ciampo Apr 23, 2025
7658e01
Update yarn
ciampo May 2, 2025
f394dfb
testing color tokens, notes
ciampo May 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reorganize color mapping code
  • Loading branch information
ciampo committed Apr 23, 2025
commit 70a0c019f8e4deeaf9febecda4d99f28734222e4
7 changes: 4 additions & 3 deletions packages/theme/src/color/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Color from 'colorjs.io';
import { ThemeProps, TokensObject } from '../types';
import { generateColorScales } from './algos';
import { mapColors } from './map';
import { COLOR_MAP } from './map';
import { mapColorsToScale } from './utils';

export function generateColors( color: ThemeProps[ 'color' ] ) {
// Bridge the gap between color algos and our API surface.
Expand All @@ -19,7 +20,7 @@ export function generateColors( color: ThemeProps[ 'color' ] ) {
return {
[ 'neutral-scale' ]: colorScales.grayScale,
[ 'primary-scale' ]: colorScales.accentScale,
neutral: mapColors( colorScales.grayScale ),
primary: mapColors( colorScales.accentScale ),
neutral: mapColorsToScale( colorScales.grayScale, COLOR_MAP ),
primary: mapColorsToScale( colorScales.accentScale, COLOR_MAP ),
} as TokensObject;
}
31 changes: 2 additions & 29 deletions packages/theme/src/color/map.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
// map showing which lightness in scale each use case should use
// type ColorMap< T > = Partial<
// Record<
// 'bg' | 'text' | 'border',
// Partial<
// Record<
// 'default' | 'hover' | 'active' | 'input' | 'muted' | 'strong' | 'inverse' | 'disabled',
// T | Partial< Record< 'default' | 'disabled' | 'hover' | 'strong', T > >
// >
// >
// >
// >;
import { ColorMap } from './types';

import type { TokensObject } from '../types';
import type { ArrayOf12, ColorScaleIndex } from './types';

type ColorMap< T > = {
[ key: string ]: T | ColorMap< T >;
};

const COLOR_MAP: ColorMap< ColorScaleIndex > = {
export const COLOR_MAP: ColorMap = {
bg: {
default: 2,
hover: 3,
Expand Down Expand Up @@ -55,12 +37,3 @@ const COLOR_MAP: ColorMap< ColorScaleIndex > = {
hover: 6,
},
};

// maps a color map to a color scale
export const mapColors = ( colorScale: ArrayOf12< string >, mapToObject = COLOR_MAP ) => {
const map: TokensObject = {};
Object.entries( mapToObject ).forEach( ( [ alias, color ] ) => {
map[ alias ] = typeof color === 'object' ? mapColors( colorScale, color ) : colorScale[ color ];
} );
return map;
};
4 changes: 4 additions & 0 deletions packages/theme/src/color/types.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export type ArrayOf12< T > = [ T, T, T, T, T, T, T, T, T, T, T, T ];
export type ColorScaleIndex = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11;

export type ColorMap = {
[ key: string ]: ColorScaleIndex | ColorMap;
};
25 changes: 25 additions & 0 deletions packages/theme/src/color/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// map showing which lightness in scale each use case should use
// type ColorMap< T > = Partial<
// Record<
// 'bg' | 'text' | 'border',
// Partial<
// Record<
// 'default' | 'hover' | 'active' | 'input' | 'muted' | 'strong' | 'inverse' | 'disabled',
// T | Partial< Record< 'default' | 'disabled' | 'hover' | 'strong', T > >
// >
// >
// >
// >;

import type { TokensObject } from '../types';
import type { ArrayOf12, ColorMap } from './types';

// maps a color map to a color scale
export const mapColorsToScale = ( colorScale: ArrayOf12< string >, mapToObject: ColorMap ) => {
const map: TokensObject = {};
Object.entries( mapToObject ).forEach( ( [ alias, color ] ) => {
map[ alias ] =
typeof color === 'object' ? mapColorsToScale( colorScale, color ) : colorScale[ color ];
} );
return map;
};