Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #!/usr/bin/env node
  2.  
  3. /**
  4. Usage: ninepatch.js background (icon.png)
  5. background: color to extend
  6. icon.png: image to center, present on running directory
  7.  
  8. Convert a base icon into 9-patch assets
  9. Outputs base composites for each resolution and .9 files
  10. */
  11. (function(specification){
  12. var background = process.argv[2],
  13. output_dir = 'ninepatch-output/',
  14. shell = require('child_process'),
  15. // create directories if necessary
  16. mkdirp = 'mkdir -p ' + output_dir;
  17. //console.log(mkdirp);
  18. shell.exec(mkdirp, out);
  19. for (var density in specification){
  20. specification[density].replace(/(\d+) (\d+)x(\d+)/,
  21. function(spec, icon, width, height){
  22. ['port', 'land'].forEach(function(orientation){
  23. var output = output_dir + 'display-'+orientation+'-'+density+'-splashscreen.9.png',
  24. content = (icon * 1.6),
  25. // coerce these strings into numbers, for maths
  26. w = orientation == 'port' ? +width : +height,
  27. h = orientation == 'port' ? +height: +width,
  28. // Assemble shell command (Imagemagick)
  29. convert = '/opt/local/bin/convert icon.png' +
  30. // 1 Take base icon, resize for each resolution
  31. ' -gravity center -resize ' + content +
  32. ' -background "' + background + '" -flatten' +
  33. // 2 Extend background
  34. ' -extent ' + w + 'x' + h +
  35. // 3 Add transparent border
  36. ' -matte -bordercolor none -border 1' +
  37. // 4 Draw 9-patch scalable (top/left) markers
  38. ' -fill black ' +
  39. // top
  40. '-draw "line 1,0 '+((w - content)/2)+',0" -draw "line '+((w + content + 2)/2)+',0 '+(w - 1)+',0"' +
  41. // left
  42. ' -draw "line 0,1 0,'+((h - content)/2)+'" -draw "line 0,'+((h + content + 2)/2)+' 0,'+(h - 1)+'" ' +
  43. // 5 Draw 9-patch fill (right/bottom) markers
  44. // not used
  45. // 6 Execute
  46. output;
  47. console.log(convert);
  48. shell.exec(convert, out);
  49. });
  50. });
  51. }
  52. })({
  53. // name: 'icon widthxheight'
  54. ldpi: '36 320x426',
  55. mdpi: '48 320x470',
  56. hdpi: '72 480x640',
  57. xhdpi:'96 720x960',
  58. xxhdpi:'144 720x960',
  59. });
  60.  
  61.  
  62. function out(error, stdout, stderr) {
  63. stdout && console.log(stdout);
  64. stderr && console.log(stderr);
  65. if (error)
  66. console.log(error);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement