gaber-elsayed

COLOR

Feb 8th, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. exports.description = "Prints informations about given color. When no arguments are provided, generates a random color.\nSupports HEX, RGB, Numeric value";
  2. exports.usage = '<color>';
  3. exports.level = 0;
  4. exports.perms = [];
  5. exports.cooldown = 5000;
  6. exports.dmable = true;
  7.  
  8. exports.run = async message => {
  9. if (!message.args.length) return resolve(null, 'random');
  10. let error = `This is not a valid color format!\nUse **${message.prefix}help ${__filename.split(/[\\/]/).pop().slice(0,-3)}** for valid formats.`
  11. //resolving numeric value
  12. if (!isNaN(message.args[0]) && message.args[0] < 16777216 && message.args[0] >= 0) return resolve(message.args[0], 'number');
  13. //resolving hex value (and triple-hex below as well)
  14. if (/^[^a-f0-9]*[a-f0-9]{6}$/i.test(message.args[0])) return resolve(/[a-f0-9]{6}$/i.exec(message.args[0])[0], 'hex');
  15. if (/^[^a-f0-9]*[a-f0-9]{3}$/i.test(message.args[0])) return resolve(/[a-f0-9]{3}$/i.exec(message.args[0])[0], 'triphex');
  16. //trying to resolve rgb value
  17. if (/^\D*(\d{1,3})[\s\W]+(\d{1,3})[\s\W]+(\d{1,3})\D*$/.test(message.args.join(' '))){
  18. let rgbData = /^\D*(\d{1,3})[\s\W]+(\d{1,3})[\s\W]+(\d{1,3})\D*$/.exec(message.args.join(' ')).slice(1);
  19. rgbData.forEach(e => {if (e > 255) throw ['normal', error]});
  20. return resolve(rgbData, 'rgb');
  21. }
  22. if (message.args[0] == 'random') return resolve(null, 'random');
  23. throw ['normal', error];
  24.  
  25. async function resolve(value, inputType){
  26. const fetch = require('node-fetch');
  27. let color = {};
  28. switch(inputType){
  29. case "triphex":
  30. value = value.split('').map(a => a+a).join(''); //simple hardfix
  31. case "hex":
  32. color.hex = value;
  33. color.rgb = `rgb(${value.match(/../g).map(e => parseInt(e, 16)).join(', ')})`;
  34. color.number = parseInt('0x'+value);
  35. break;
  36. case "rgb":
  37. color.hex = value.map(val => parseInt(val).toString(16).padStart(2, '0')).join('');
  38. color.rgb = `rgb(${value.join(', ')})`;
  39. color.number = parseInt(color.hex, 16);
  40. break;
  41. case "random":
  42. color.random = Math.floor(Math.random()*16777214);
  43. value = color.random;
  44. case "number":
  45. let hex = parseInt(value).toString(16);
  46. color.hex = hex.padStart(6, '0');
  47. color.rgb = `rgb(${hex.match(/../g).map(e => parseInt(e, 16)).join(', ')})`;
  48. color.number = parseInt(value);
  49. break;
  50. }
  51. let colorapi = await fetch(`https://www.thecolorapi.com/id?hex=${color.hex}`).then(res => res.json());
  52. let embed = {
  53. color: color.number >= 16777215 ? color.number-1 : color.number,
  54. timestamp: message.createdAt,
  55. footer: {
  56. text: message.author.tag,
  57. icon_url: message.author.avatarURL({format: 'png', dynamic: true, size: 4096})
  58. },
  59. author: {
  60. name: `Information about ${color.random ? 'random color' : (inputType == 'rgb' ? message.args.join(' ') : message.args[0])}`
  61. },
  62. thumbnail: {
  63. url: 'attachment://color.png'
  64. },
  65. description:
  66. `Hex: **#${color.hex}**\n`+
  67. `RGB: **${color.rgb}**\n`+
  68. `Numeric value: **${color.number}**\n`+
  69. `CSS Name: **${colorapi.name.value}**`
  70. };
  71. const {createCanvas} = require('canvas');
  72. let canvas = createCanvas(60, 60);
  73. let ctx = canvas.getContext('2d');
  74. ctx.fillStyle = `#${color.hex}`;
  75. ctx.fillRect(0, 0, 60, 60);
  76. message.channel.send({embed: embed, files: [
  77. {
  78. name: 'color.png',
  79. attachment: canvas.toBuffer()
  80. }
  81. ]});
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment