Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const colorMapping = [
- {
- "github": "background",
- "figma": "Background/Default"
- },
- {
- "github": "surface",
- "figma": "Surface/Default"
- },
- {
- "github": "surface-subdued",
- "figma": "Surface/Subdued"
- },
- {
- "github": "surface-neutral",
- "figma": "Surface Neutral/Default"
- },
- {
- "github": "surface-neutral-subdued",
- "figma": "Surface Neutral/Subdued"
- },
- {
- "github": "surface-hovered",
- "figma": "Surface/Hovered"
- },
- {
- "github": "surface-pressed",
- "figma": "Surface/Pressed"
- },
- {
- "github": "surface-disabled",
- "figma": "Surface/Disabled"
- },
- {
- "github": "backdrop",
- "figma": ""
- },
- {
- "github": "overlay",
- "figma": ""
- },
- {
- "github": "shadow-from-ambient-light",
- "figma": ""
- },
- {
- "github": "shadow-from-direct-light",
- "figma": ""
- },
- {
- "github": "hint-from-direct-light",
- "figma": ""
- },
- {
- "github": "on-surface",
- "figma": ""
- },
- {
- "github": "border",
- "figma": "Border/Default"
- },
- {
- "github": "border-disabled",
- "figma": "Border/Disabled"
- },
- {
- "github": "border-subdued",
- "figma": "Border/Subdued"
- },
- {
- "github": "icon",
- "figma": "Icon/Default"
- },
- {
- "github": "icon-disabled",
- "figma": "Icon/Disabled"
- },
- {
- "github": "icon-subdued",
- "figma": "Icon/Subdued"
- },
- {
- "github": "text",
- "figma": "Text/Default"
- },
- {
- "github": "text-disabled",
- "figma": "Text/Disabled"
- },
- {
- "github": "text-subdued",
- "figma": "Text/Subdued"
- },
- {
- "github": "interactive",
- "figma": ""
- },
- {
- "github": "interactive",
- "figma": "Interactive/Default"
- },
- {
- "github": "interactive-disabled",
- "figma": "Interactive/Disabled"
- },
- {
- "github": "interactive-hovered",
- "figma": "Interactive/Hovered"
- },
- {
- "github": "interactive-pressed",
- "figma": "Interactive/Pressed"
- },
- {
- "github": "focused",
- "figma": "Focused/Default"
- },
- {
- "github": "surface-selected",
- "figma": "Surface Selected/Default"
- },
- {
- "github": "surface-selected-hovered",
- "figma": "Surface Selected/Hovered"
- },
- {
- "github": "surface-selected-pressed",
- "figma": "Surface Selected/Pressed"
- },
- {
- "github": "icon-on-interactive",
- "figma": "Icon On/Interactive"
- },
- {
- "github": "text-on-interactive",
- "figma": "Text On/Interactive"
- },
- {
- "github": "secondary",
- "figma": ""
- },
- {
- "github": "action-secondary",
- "figma": "Action Secondary/Default"
- },
- {
- "github": "action-secondary-disabled",
- "figma": "Action Secondary/Disabled"
- },
- {
- "github": "action-secondary-hovered",
- "figma": "Action Secondary/Hovered"
- },
- {
- "github": "action-secondary-pressed",
- "figma": "Action Secondary/Pressed"
- },
- {
- "github": "border-secondary",
- "figma": "Border Secondary/Default"
- },
- {
- "github": "border-secondary-hovered",
- "figma": "Border Secondary/Hovered"
- },
- {
- "github": "border-secondary-disabled",
- "figma": "Border Secondary/Disabled"
- },
- {
- "github": "primary",
- "figma": ""
- },
- {
- "github": "action-primary",
- "figma": "Action Primary/Default"
- },
- {
- "github": "action-primary-disabled",
- "figma": "Action Primary/Disabled"
- },
- {
- "github": "action-primary-hovered",
- "figma": "Action Primary/Hovered"
- },
- {
- "github": "action-primary-pressed",
- "figma": "Action Primary/Pressed"
- },
- {
- "github": "icon-on-primary",
- "figma": "Icon On/Primary"
- },
- {
- "github": "text-on-primary",
- "figma": "Text On/Primary"
- },
- {
- "github": "surface-primary-selected",
- "figma": "Surface Primary/Selected"
- },
- {
- "github": "surface-primary-selected-hovered",
- "figma": "Surface Primary/Selected Hovered"
- },
- {
- "github": "surface-primary-selected-pressed",
- "figma": "Surface Primary/Selected Pressed"
- },
- {
- "github": "critical",
- "figma": ""
- },
- {
- "github": "border-critical",
- "figma": "Border Critical/Default"
- },
- {
- "github": "border-critical-disabled",
- "figma": "Border Critical/Disabled"
- },
- {
- "github": "border-critical-subdued",
- "figma": "Border Critical/Subdued"
- },
- {
- "github": "icon-critical",
- "figma": "Icon/Critical"
- },
- {
- "github": "icon-negative",
- "figma": ""
- },
- {
- "github": "surface-critical",
- "figma": "Surface Critical/Default"
- },
- {
- "github": "surface-critical-subdued",
- "figma": "Surface Critical/Subdued"
- },
- {
- "github": "surface-critical-subdued-hovered",
- "figma": "Surface Critical/Subdued Hovered"
- },
- {
- "github": "surface-critical-subdued-pressed",
- "figma": "Surface Critical/Subdued Pressed"
- },
- {
- "github": "text-critical",
- "figma": "Text/Critical"
- },
- {
- "github": "text-data-negative",
- "figma": ""
- },
- {
- "github": "action-critical",
- "figma": "Action Critical/Default"
- },
- {
- "github": "action-critical-disabled",
- "figma": "Action Critical/Disabled"
- },
- {
- "github": "action-critical-hovered",
- "figma": "Action Critical/Hovered"
- },
- {
- "github": "action-critical-pressed",
- "figma": "Action Critical/Pressed"
- },
- {
- "github": "icon-on-critical",
- "figma": "Icon On/Critical"
- },
- {
- "github": "text-on-critical",
- "figma": "Text On/Critical"
- },
- {
- "github": "interactive-critical",
- "figma": "Interactive/Critical"
- },
- {
- "github": "interactive-critical-disabled",
- "figma": "Interactive/Critical Disabled"
- },
- {
- "github": "interactive-critical-hovered",
- "figma": "Interactive/Critical Hover"
- },
- {
- "github": "interactive-critical-pressed",
- "figma": "Interactive/Critical Pressed"
- },
- {
- "github": "warning",
- "figma": ""
- },
- {
- "github": "border-warning",
- "figma": "Border Warning/Default"
- },
- {
- "github": "border-warning-subdued",
- "figma": "Border Warning/Subdued"
- },
- {
- "github": "icon-warning",
- "figma": "Icon/Warning"
- },
- {
- "github": "surface-warning",
- "figma": "Surface Warning/Default"
- },
- {
- "github": "surface-warning-subdued",
- "figma": "Surface Warning/Subdued"
- },
- {
- "github": "text-warning",
- "figma": "Text/Warning"
- },
- {
- "github": "highlight",
- "figma": ""
- },
- {
- "github": "border-highlight",
- "figma": "Border Highlight/Default"
- },
- {
- "github": "border-highlight-subdued",
- "figma": "Border Highlight/Subdued"
- },
- {
- "github": "icon-highlight",
- "figma": "Icon/Highlight"
- },
- {
- "github": "surface-highlight",
- "figma": "Surface Highlight/Default"
- },
- {
- "github": "surface-highlight-subdued",
- "figma": "Surface Highlight/Subdued"
- },
- {
- "github": "text-highlight",
- "figma": "Text/Highlight"
- },
- {
- "github": "success",
- "figma": ""
- },
- {
- "github": "border-success",
- "figma": "Border Success/Default"
- },
- {
- "github": "border-success-subdued",
- "figma": "Border Success/Subdued"
- },
- {
- "github": "icon-success",
- "figma": "Icon/Success"
- },
- {
- "github": "surface-success",
- "figma": "Surface Success/Default"
- },
- {
- "github": "surface-success-subdued",
- "figma": "Surface Success/Subdued"
- },
- {
- "github": "text-success",
- "figma": "Text/Success"
- },
- {
- "github": "decorative",
- "figma": ""
- },
- {
- "github": "decorative-one-surface",
- "figma": "Decorative/Surface/One"
- },
- {
- "github": "decorative-two-surface",
- "figma": "Decorative/Surface/Two"
- },
- {
- "github": "decorative-three-surface",
- "figma": "Decorative/Surface/Three"
- },
- {
- "github": "decorative-four-surface",
- "figma": "Decorative/Surface/Four"
- },
- {
- "github": "decorative-five-surface",
- "figma": "Decorative/Surface/Five"
- },
- {
- "github": "decorative-one-text",
- "figma": "Decorative/Text/One"
- },
- {
- "github": "decorative-two-text",
- "figma": "Decorative/Text/Two"
- },
- {
- "github": "decorative-three-text",
- "figma": "Decorative/Text/Three"
- },
- {
- "github": "decorative-four-text",
- "figma": "Decorative/Text/Four"
- },
- {
- "github": "decorative-five-text",
- "figma": "Decorative/Text/Five"
- },
- {
- "github": "decorative-one-icon",
- "figma": "Decorative/Icon/One"
- },
- {
- "github": "decorative-two-icon",
- "figma": "Decorative/Icon/Two"
- },
- {
- "github": "decorative-three-icon",
- "figma": "Decorative/Icon/Three"
- },
- {
- "github": "decorative-four-icon",
- "figma": "Decorative/Icon/Four"
- },
- {
- "github": "decorative-five-icon",
- "figma": "Decorative/Icon/Five"
- }
- ].map(mapping => ({
- ...mapping,
- github: transformGithubName(mapping.github)
- }))
- interface ColorTokensFile {
- [x: string]: string;
- }
- function transformGithubName(s: string) : string {
- return s.split('-').map((p, i) => i > 0 ? capitalizeFirstLetter(p) : p).join('');
- }
- function capitalizeFirstLetter(string) {
- return string.charAt(0).toUpperCase() + string.slice(1);
- }
- function createColors(colorTokens: ColorTokensFile) {
- let createdCount = 0;
- let updatedCount = 0;
- let unmatchedCount = 0;
- Object.entries(colorTokens).forEach(([key, colorAsRgbString]) => {
- // Get all styles
- // @ts-ignore
- let mappedName = colorMapping.find(color => transformGithubName(color.github) === key && color.figma !== "");
- if (!mappedName) {
- print(`🚨 Could not find mapping for ${key}`)
- unmatchedCount++
- } else {
- let figmaNameForStyle = `_${mappedName.figma}`;
- let styles = figma.getLocalPaintStyles();
- let existingStyle = styles.find((style) => style.name === figmaNameForStyle);
- let colorParts = colorAsRgbString.split(',');
- let color = {
- red: parseInt(colorParts[0].replace(/[^0-9]+/gi, '')),
- green: parseInt(colorParts[1].replace(/[^0-9]+/gi, '')),
- blue: parseInt(colorParts[2].replace(/[^0-9]+/gi, '')),
- };
- let paints = [
- {
- type: 'SOLID',
- color: {
- r: color.red / 255,
- g: color.green / 255,
- b: color.blue / 255,
- },
- },
- ]
- if (existingStyle) {
- existingStyle.paints = paints
- updatedCount++;
- } else {
- let newStyle = figma.createPaintStyle();
- newStyle.name = figmaNameForStyle;
- newStyle.paints = paints;
- createdCount++;
- }
- }
- });
- alert(`Sync complete\n\n${createdCount} styles created\n${updatedCount} styles updated\n${unmatchedCount} tokens from Github have no mappings to Figma styles`)
- }
- createColors({
- "background": "rgb(250, 250, 250)",
- "surface": "rgb(255, 255, 255)",
- "surfaceSubdued": "rgb(250, 250, 250)",
- "surfaceHovered": "rgb(243, 244, 244)",
- "surfacePressed": "rgb(225, 227, 229)",
- "backdrop": "rgba(0, 0, 0, 0.5)",
- "overlay": "rgba(255, 255, 255, 0.5)",
- "shadowFromAmbientLight": "rgba(23, 24, 24, 0.05)",
- "shadowFromDirectLight": "rgba(0, 0, 0, 0.15)",
- "hintFromDirectLight": "rgba(0, 0, 0, 0.15)",
- "border": "rgb(180, 185, 190)",
- "borderSubdued": "rgb(210, 213, 216)",
- "icon": "rgb(92, 95, 98)",
- "iconHovered": "rgb(26, 28, 29)",
- "iconPressed": "rgb(68, 71, 74)",
- "iconDisabled": "rgb(186, 190, 195)",
- "iconSubdued": "rgb(140, 145, 150)",
- "text": "rgb(32, 34, 35)",
- "textDisabled": "rgb(144, 149, 154)",
- "textSubdued": "rgb(109, 113, 117)",
- "interactive": "rgb(46, 114, 210)",
- "interactiveDisabled": "rgb(73, 138, 242)",
- "interactiveHovered": "rgb(33, 86, 161)",
- "interactivePressed": "rgb(26, 72, 137)",
- "focused": "rgb(73, 138, 242)",
- "surfaceSelected": "rgb(240, 243, 254)",
- "surfaceSelectedHovered": "rgb(214, 223, 252)",
- "surfaceSelectedPressed": "rgb(187, 203, 249)",
- "iconOnInteractive": "rgb(255, 255, 255)",
- "textOnInteractive": "rgb(255, 255, 255)",
- "actionSecondary": "rgb(234, 235, 236)",
- "actionSecondaryDisabled": "rgb(237, 238, 239)",
- "actionSecondaryHovered": "rgb(225, 227, 229)",
- "actionSecondaryPressed": "rgb(216, 218, 221)",
- "borderSecondary": "rgb(180, 185, 190)",
- "borderSecondaryHovered": "rgb(153, 158, 164)",
- "borderSecondaryDisabled": "rgb(210, 213, 216)",
- "actionPrimary": "rgb(0, 128, 96)",
- "actionPrimaryDisabled": "rgb(0, 86, 64)",
- "actionPrimaryHovered": "rgb(0, 114, 85)",
- "actionPrimaryPressed": "rgb(0, 100, 75)",
- "iconOnPrimary": "rgb(230, 255, 244)",
- "textOnPrimary": "rgb(255, 255, 255)",
- "surfacePrimarySelected": "rgb(226, 245, 237)",
- "surfacePrimarySelectedHovered": "rgb(179, 208, 195)",
- "surfacePrimarySelectedPressed": "rgb(162, 188, 176)",
- "borderCritical": "rgb(227, 47, 14)",
- "borderCriticalDisabled": "rgb(254, 188, 185)",
- "iconCritical": "rgb(236, 49, 15)",
- "surfaceCritical": "rgb(255, 250, 250)",
- "surfaceCriticalSubdued": "rgb(255, 248, 247)",
- "surfaceCriticalSubduedHovered": "rgb(255, 240, 240)",
- "surfaceCriticalSubduedPressed": "rgb(254, 211, 209)",
- "textCritical": "rgb(215, 44, 13)",
- "actionCritical": "rgb(216, 44, 13)",
- "actionCriticalDisabled": "rgb(253, 80, 64)",
- "actionCriticalHovered": "rgb(193, 38, 10)",
- "actionCriticalPressed": "rgb(172, 33, 8)",
- "iconOnCritical": "rgb(255, 248, 247)",
- "textOnCritical": "rgb(255, 255, 255)",
- "interactiveCritical": "rgb(220, 45, 14)",
- "interactiveCriticalDisabled": "rgb(253, 147, 141)",
- "interactiveCriticalHovered": "rgb(205, 41, 12)",
- "interactiveCriticalPressed": "rgb(103, 15, 3)",
- "borderWarning": "rgb(241, 179, 0)",
- "iconWarning": "rgb(205, 152, 0)",
- "surfaceWarning": "rgb(255, 203, 116)",
- "surfaceWarningSubdued": "rgb(255, 242, 226)",
- "textWarning": "rgb(145, 106, 0)",
- "borderHighlight": "rgb(68, 157, 167)",
- "iconHighlight": "rgb(65, 151, 161)",
- "surfaceHighlight": "rgb(142, 224, 235)",
- "surfaceHighlightSubdued": "rgb(244, 253, 254)",
- "textHighlight": "rgb(8, 31, 33)",
- "borderSuccess": "rgb(0, 135, 102)",
- "iconSuccess": "rgb(0, 68, 50)",
- "surfaceSuccess": "rgb(137, 229, 194)",
- "surfaceSuccessSubdued": "rgb(247, 254, 251)",
- "textSuccess": "rgb(0, 128, 96)",
- "decorativeOneIcon": "rgb(126, 87, 0)",
- "decorativeOneSurface": "rgb(255, 201, 107)",
- "decorativeOneText": "rgb(61, 40, 0)",
- "decorativeTwoIcon": "rgb(175, 41, 78)",
- "decorativeTwoSurface": "rgb(255, 196, 176)",
- "decorativeTwoText": "rgb(73, 11, 28)",
- "decorativeThreeIcon": "rgb(0, 109, 65)",
- "decorativeThreeSurface": "rgb(144, 227, 179)",
- "decorativeThreeText": "rgb(0, 47, 25)",
- "decorativeFourIcon": "rgb(0, 106, 104)",
- "decorativeFourSurface": "rgb(145, 224, 214)",
- "decorativeFourText": "rgb(0, 45, 45)",
- "decorativeFiveIcon": "rgb(174, 43, 76)",
- "decorativeFiveSurface": "rgb(253, 201, 208)",
- "decorativeFiveText": "rgb(79, 14, 31)"
- })
- function rgbFromHueAndChroma(hue: number, chroma: number) {
- const huePrime = hue / 60;
- const hueDelta = 1 - Math.abs((huePrime % 2) - 1);
- const intermediateValue = chroma * hueDelta;
- let red = 0;
- let green = 0;
- let blue = 0;
- if (huePrime >= 0 && huePrime <= 1) {
- red = chroma;
- green = intermediateValue;
- blue = 0;
- }
- if (huePrime >= 1 && huePrime <= 2) {
- red = intermediateValue;
- green = chroma;
- blue = 0;
- }
- if (huePrime >= 2 && huePrime <= 3) {
- red = 0;
- green = chroma;
- blue = intermediateValue;
- }
- if (huePrime >= 3 && huePrime <= 4) {
- red = 0;
- green = intermediateValue;
- blue = chroma;
- }
- if (huePrime >= 4 && huePrime <= 5) {
- red = intermediateValue;
- green = 0;
- blue = chroma;
- }
- if (huePrime >= 5 && huePrime <= 6) {
- red = chroma;
- green = 0;
- blue = intermediateValue;
- }
- return {red, green, blue};
- }
- interface RGBColor {
- red: number;
- green: number;
- blue: number;
- }
- function hslToRgb(color): RGBColor {
- const {hue, saturation, lightness, alpha = 1} = color;
- const chroma = (1 - Math.abs(2 * (lightness / 100) - 1)) * (saturation / 100);
- let {red, green, blue} = rgbFromHueAndChroma(hue, chroma);
- const lightnessVal = lightness / 100 - chroma / 2;
- red += lightnessVal;
- green += lightnessVal;
- blue += lightnessVal;
- return {
- red: Math.round(red * 255),
- green: Math.round(green * 255),
- blue: Math.round(blue * 255),
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement