cardboardbox/commands/Information/color.js

78 lines
2 KiB
JavaScript
Raw Normal View History

2019-12-21 21:34:15 +00:00
const { RichEmbed } = require('discord.js');
const rgbToHSL = (red, green, blue) => {
let r = red / 255;
let g = green / 255;
let b = blue / 255;
let max = Math.max(r, g, b), min = Math.min(r, g, b);
let h, s, l = (max + min) / 2;
if (max == min) {
h = s = 0;
} else {
let d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
h = Math.round(h * 360);
s = Math.round(s * 100);
l = Math.round(l * 100);
return { hue: h, saturation: s, lightness: l };
};
const resolveColor = input => {
if (input.startsWith('#')) input = input.substr(1);
if (input.length === 3) input = input.split('').map(c => c + c).join('');
let hex = input;
let [red, green, blue] = [hex.substr(0, 2), hex.substr(2, 2), hex.substr(4, 2)]
.map(value => parseInt(value, 16));
let { hue, saturation, lightness } = rgbToHSL(red, green, blue);
return { hex, red, green, blue, hue, saturation, lightness };
};
exports.run = async (client, message, args, level) => {
if (args.length < 1) {
(await message.reply('please provide a color.')).delete(5000);
return message.delete();
}
if (!/^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/.test(args[0])) {
(await message.reply('the color must be in the format of `#RRGGBB` or `#RGB`.')).delete(5000);
return message.delete();
}
let color = resolveColor(args[0]);
message.delete();
message.channel.send({
embed: new RichEmbed()
.setDescription(`Hex: \`#${color.hex}\`\nRGB: \`${color.red}, ${color.green}, ${color.blue}\`\nHSL: \`${color.hue}, ${color.saturation}, ${color.lightness}\``)
.setImage(`http://placehold.it/500/${color.hex}/${color.hex}`)
.setColor(`${color.hex}`)
});
};
exports.conf = {
enabled: true,
guildOnly: false,
aliases: ["hex","rgb"],
permLevel: "User"
};
exports.help = {
name: "color",
category: "Information",
description: "Shows some information and a preview of a specified hex color.",
usage: "color <hex>"
};