Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- componentFromStr = (numStr, percent) => {
- var num = Math.max(0, parseInt(numStr, 10))
- return percent
- ? Math.floor((255 * Math.min(100, num)) / 100)
- : Math.min(255, num)
- }
- rgbToHex = rgb => {
- var rgbRegex = /^rgb\(\s*(-?\d+)(%?)\s*,\s*(-?\d+)(%?)\s*,\s*(-?\d+)(%?)\s*\)$/
- var result,
- r,
- g,
- b,
- hex = ''
- if ((result = rgbRegex.exec(rgb))) {
- r = componentFromStr(result[1], result[2])
- g = componentFromStr(result[3], result[4])
- b = componentFromStr(result[5], result[6])
- hex = '0x' + (0x1000000 + (r << 16) + (g << 8) + b).toString(16).slice(1)
- }
- return hex
- }
- baseOneToBase256 = base1Object => {
- return Object.keys(base1Object).map(index =>
- parseInt(base1Object[index] * 256),
- )
- }
- colors = figma.getLocalPaintStyles()
- output = []
- colors.forEach(color => {
- rgb = baseOneToBase256(color.paints[0].color)
- hex = rgbToHex(`rgb(${rgb[0]},${rgb[1]},${rgb[2]})`).substr(2)
- newColor = { name: color.name, rgb, hex }
- output.push(newColor)
- })
- console.log(output)
- outputStr = ''
- output.forEach(color => {
- name = color.name.replace(' / ', '').toLowerCase()
- outputStr += `const ${name} = "#${color.hex}"
- `
- })
- console.log(outputStr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement