Skip to content

Instantly share code, notes, and snippets.

@Santosl2
Created March 22, 2024 23:52
Show Gist options
  • Select an option

  • Save Santosl2/a729b2b97c3d7e03d5ae1170398d488a to your computer and use it in GitHub Desktop.

Select an option

Save Santosl2/a729b2b97c3d7e03d5ae1170398d488a to your computer and use it in GitHub Desktop.
A simple algorithm to catch all figma libraries colors
const exportedColors = [{}];
const colorsTitle = document.querySelectorAll(
"div.raw_components--modalPanel--NX71G.variable_picker_ui--modalPanel--X0bjl > div > div > div > div"
);
Array.from(colorsTitle).map((el) => {
const color = el.textContent;
if (!color) return;
const firstLetter = color?.charAt(0);
const firstLetterIsUpperCase = firstLetter === firstLetter.toUpperCase();
if (firstLetterIsUpperCase) {
exportedColors.push({
[color.toLowerCase()]: {},
});
} else {
const splittedColors = color.split("-");
const colorName = splittedColors[0].toLowerCase();
const colorType = splittedColors[1].toLowerCase();
const hexColor = el.parentElement
?.querySelector("svg > circle")
?.getAttribute("fill");
const hex = exportedColors[colorName];
if (!hex) {
exportedColors[colorName] = {};
}
exportedColors[colorName][colorType] = hexColor;
}
});
copy(exportedColors)
console.log(exportedColors);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment