Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. componentFromStr = (numStr, percent) => {
  2. var num = Math.max(0, parseInt(numStr, 10))
  3. return percent
  4. ? Math.floor((255 * Math.min(100, num)) / 100)
  5. : Math.min(255, num)
  6. }
  7.  
  8. rgbToHex = rgb => {
  9. var rgbRegex = /^rgb\(\s*(-?\d+)(%?)\s*,\s*(-?\d+)(%?)\s*,\s*(-?\d+)(%?)\s*\)$/
  10. var result,
  11. r,
  12. g,
  13. b,
  14. hex = ''
  15. if ((result = rgbRegex.exec(rgb))) {
  16. r = componentFromStr(result[1], result[2])
  17. g = componentFromStr(result[3], result[4])
  18. b = componentFromStr(result[5], result[6])
  19.  
  20. hex = '0x' + (0x1000000 + (r << 16) + (g << 8) + b).toString(16).slice(1)
  21. }
  22. return hex
  23. }
  24.  
  25. baseOneToBase256 = base1Object => {
  26. return Object.keys(base1Object).map(index =>
  27. parseInt(base1Object[index] * 256),
  28. )
  29. }
  30.  
  31. colors = figma.getLocalPaintStyles()
  32. output = []
  33. colors.forEach(color => {
  34. rgb = baseOneToBase256(color.paints[0].color)
  35. hex = rgbToHex(`rgb(${rgb[0]},${rgb[1]},${rgb[2]})`).substr(2)
  36. newColor = { name: color.name, rgb, hex }
  37. output.push(newColor)
  38. })
  39. console.log(output)
  40.  
  41. outputStr = ''
  42. output.forEach(color => {
  43. name = color.name.replace(' / ', '').toLowerCase()
  44. outputStr += `const ${name} = "#${color.hex}"
  45. `
  46. })
  47. console.log(outputStr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement