From 7f9fdfb2f65d7abc3530cc36a8949e6b26d28e71 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sat, 16 Jan 2021 13:09:41 -0500 Subject: [PATCH] Dominant Color Command --- README.md | 3 +- commands/analyze/dominant-color.js | 45 ++++++++++++++++++++++++++++++ package.json | 2 +- util/Util.js | 5 ++++ 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 commands/analyze/dominant-color.js diff --git a/README.md b/README.md index e02af227..5be0daeb 100644 --- a/README.md +++ b/README.md @@ -260,7 +260,7 @@ in the appropriate channel's topic to use it. ## Commands -Total: 575 +Total: 576 ### Utility: @@ -542,6 +542,7 @@ Total: 575 * **birthstone:** Responds with the Birthstone for a month. * **character-count:** Responds with the character count of text. * **chinese-zodiac:** Responds with the Chinese Zodiac Sign for the given year. +* **dominant-color:** Determines the dominant color of an image. * **face:** Determines the race, gender, and age of a face. * **gender:** Determines the gender of a name. * **has-transparency:** Determines if an image has transparency in it. diff --git a/commands/analyze/dominant-color.js b/commands/analyze/dominant-color.js new file mode 100644 index 00000000..a61cec22 --- /dev/null +++ b/commands/analyze/dominant-color.js @@ -0,0 +1,45 @@ +const Command = require('../../structures/Command'); +const { createCanvas, loadImage } = require('canvas'); +const request = require('node-superfetch'); +const { rgbToHex } = require('../../util/Util'); + +module.exports = class DominantColorCommand extends Command { + constructor(client) { + super(client, { + name: 'dominant-color', + aliases: ['dom-color', 'dominant-colour', 'dom-colour'], + group: 'analyze', + memberName: 'dominant-color', + description: 'Determines the dominant color of an image.', + throttling: { + usages: 1, + duration: 10 + }, + args: [ + { + key: 'image', + prompt: 'What image would you like to test?', + type: 'image', + default: msg => msg.author.displayAvatarURL({ format: 'png', size: 512 }) + } + ] + }); + } + + async run(msg, { image }) { + try { + const { body } = await request.get(image); + const data = await loadImage(body); + const canvas = createCanvas(250, 250); + const ctx = canvas.getContext('2d'); + ctx.drawImage(data, 0, 0, 1, 1); + const imgData = ctx.getImageData(0, 0, 1, 1).data; + const hexColor = `#${rgbToHex(imgData[0], imgData[1], imgData[2]).padStart(6, '0')}`; + ctx.fillStyle = hexColor; + ctx.fillRect(0, 0, canvas.width, canvas.height); + return msg.say(hexColor, { files: [{ attachment: canvas.toBuffer(), name: 'dominant-color.png' }] }); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/package.json b/package.json index 54bb9582..e39098fc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "126.2.1", + "version": "126.3.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/util/Util.js b/util/Util.js index 5653b992..f02a5db1 100644 --- a/util/Util.js +++ b/util/Util.js @@ -176,6 +176,11 @@ module.exports = class Util { return ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0); } + static rgbToHex(r, g, b) { + if (r > 255 || g > 255 || b > 255) return null; + return ((r << 16) | (g << 8) | b).toString(16); + } + static magikToBuffer(magik) { return new Promise((res, rej) => { magik.toBuffer((err, buffer) => {