@primer/primitives
Version:
Typography, spacing, and color primitives for Primer design system
24 lines (23 loc) • 902 B
JavaScript
import { toHex } from 'color2k';
import { isColor } from '../filters/index.js';
import { getTokenValue } from './utilities/getTokenValue.js';
import { alpha } from './utilities/alpha.js';
/**
* @description converts color tokens value to `hex6` or `hex8`
* @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
* @matcher matches all tokens of $type `color`
* @transformer returns a `hex` string
*/
export const colorToHex = {
name: 'color/hex',
type: 'value',
transitive: true,
filter: isColor,
transform: (token, config) => {
const alphaValue = token.alpha;
if (alphaValue === null || alphaValue === undefined || alphaValue === 1) {
return toHex(getTokenValue(token));
}
return toHex(alpha(getTokenValue(token), alphaValue, token, config));
},
};