Dominant Color Command

This commit is contained in:
Dragon Fire
2021-01-16 13:09:41 -05:00
parent 2acd6b22ed
commit 7f9fdfb2f6
4 changed files with 53 additions and 2 deletions
+2 -1
View File
@@ -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.
+45
View File
@@ -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!`);
}
}
};
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "126.2.1",
"version": "126.3.0",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {
+5
View File
@@ -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) => {