misc.color: add colornames name support

This commit is contained in:
Cynthia Foxwell 2024-07-31 21:33:20 -06:00
parent 3e84bdb299
commit e54f953fa1
2 changed files with 3157781 additions and 2 deletions

3157758
data/colornames.csv Normal file

File diff suppressed because it is too large Load diff

View file

@ -13,6 +13,7 @@ const GoogleImages = require("google-images");
const {tinycolor, random: randomColor} = require("@ctrl/tinycolor"); const {tinycolor, random: randomColor} = require("@ctrl/tinycolor");
const sharp = require("sharp"); const sharp = require("sharp");
const net = require("node:net"); const net = require("node:net");
const fs = require("node:fs");
const imagesClient = new GoogleImages(hf.apikeys.gimg, hf.apikeys.google); const imagesClient = new GoogleImages(hf.apikeys.gimg, hf.apikeys.google);
@ -628,6 +629,16 @@ searchInteraction.callback = async function (interaction) {
}; };
hf.registerCommand(searchInteraction); hf.registerCommand(searchInteraction);
const colornamesRaw = fs.readFileSync("../../data/colornames.csv").split("\n");
colornamesRaw.shift();
const colornamesMapped = colornamesRaw.map((line) => {
const [hex, name] = line.split(",");
return [hex, name];
});
const colornames = Object.fromEntries(colornamesMapped);
const colornamesFlipped = Object.fromEntries(colornamesMapped.reverse());
const color = new Command("color"); const color = new Command("color");
color.category = CATEGORY; color.category = CATEGORY;
color.helpText = "Show information on a color or get a random color"; color.helpText = "Show information on a color or get a random color";
@ -635,13 +646,18 @@ color.callback = async function (msg, line, args, {truerandom}) {
let color = tinycolor(line), let color = tinycolor(line),
random = false; random = false;
if (!line || line == "" || args.length == 0) { if (!line || line == "") {
color = truerandom ? tinycolor(Math.floor(Math.random() * 0xffffff)) : randomColor(); color = truerandom ? tinycolor(Math.floor(Math.random() * 0xffffff)) : randomColor();
random = true; random = true;
} }
if (!color.isValid) { if (!color.isValid) {
return "Color not valid."; const colornamesHex = colornamesFlipped[line];
if (colornamesHex) {
color = tinycolor(colornamesHex);
} else {
return "Color not valid.";
}
} }
const image = await sharp({ const image = await sharp({
@ -692,6 +708,11 @@ color.callback = async function (msg, line, args, {truerandom}) {
}); });
} }
const colornamesName = colornames[color.toHex()];
if (colornamesName) {
fields.push({name: "\u200b", value: `**[colornames](https://colornames.org/) Name**\n${colornamesName}`});
}
return { return {
embeds: [ embeds: [
{ {