Last active
June 3, 2020 10:53
-
-
Save thomas-lowry/1212d381b4789fbfbae7de7e17cdfd0e to your computer and use it in GitHub Desktop.
generate an array of hex values (and their style names) from Figma styles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const colorStyles = figma.getLocalPaintStyles(); | |
var hexValueAndName = []; // array of hex values and their names | |
var hexValues = []; //array with hex values only | |
function makeHex(r,g,b) { | |
let red = rgbToHex(r); | |
let green = rgbToHex(g); | |
let blue = rgbToHex(b); | |
return red+green+blue; | |
} | |
function rgbToHex(int) { | |
var hex = Number(int).toString(16); | |
if (hex.length < 2) { | |
hex = "0" + hex; | |
} | |
return hex; | |
} | |
colorStyles.forEach(style => { | |
let name = style.name; | |
let r = Math.round(255 * (style.paints[0].color.r)); | |
let g = Math.round(255 * (style.paints[0].color.g)); | |
let b = Math.round(255 * (style.paints[0].color.b)); | |
let hex = makeHex(r,g,b); | |
let result = {name, hex}; | |
hexValueAndName.push(result); | |
hexValues.push(hex); | |
}); | |
console.log(hexValueAndName, hexValues); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment