Last active
July 10, 2023 00:58
-
-
Save dooderstem/ef68bb71be3f09820df0d6106822fb54 to your computer and use it in GitHub Desktop.
rgbcolor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require("node-fetch"); | |
const { createCanvas } = require("canvas"); | |
const RGBColor = require("rgbcolor"); | |
const DataImageAttachment = require("dataimageattachment"); | |
const fs = require("fs"); | |
// https://github.com/meodai/color-names | |
let api = `https://api.color.pizza/v1/?values=`; | |
let Canvas = (rgbcolor) => { | |
let canvas = createCanvas(50, 50); | |
ctx = canvas.getContext("2d"); | |
ctx.fillStyle = rgbcolor; | |
ctx.fillRect(0, 0, canvas.width, canvas.height); | |
return canvas; | |
}; | |
// generate random HEX value | |
let randomColor = ""; | |
let characters = "0123456789abcdef"; | |
for (let i = 0; i < 6; i++) { | |
randomColor += characters[Math.floor(Math.random() * 16)]; | |
} | |
let randomColorRGB = new RGBColor(randomColor); | |
let colorValues = () => { | |
let RGBcolor = randomColorRGB.toRGB(); | |
let HEXcolor = randomColorRGB.toHex(); | |
return { RGBcolor, HEXcolor }; | |
}; | |
colorValues = colorValues(); | |
console.log( | |
"RGB generated: " + | |
colorValues.RGBcolor + | |
"\r\nHEX generated: " + | |
colorValues.HEXcolor | |
); | |
const url = api + randomColor; | |
console.log(url); | |
Canvas = Canvas(colorValues.RGBcolor); | |
const createPNGwriteStream = () => { | |
// create write stream | |
const out = fs.createWriteStream("color.png"); | |
// create a png stream | |
const stream = Canvas.createPNGStream(); | |
stream.pipe(out); | |
// when the file is created | |
out.on("finish", () => console.log("The PNG file was created.")); | |
}; | |
createPNGwriteStream(); | |
// discord embed | |
let colorEmbed; | |
// data:image/png;base64 | |
let dataURL = Canvas.toDataURL(); | |
console.log(dataURL); | |
// fetch the api | |
let response = fetch(url) | |
.then((resp) => resp.json()) | |
.then((data) => { | |
const { colors } = data; | |
console.log(colors[0].name); | |
// embed object | |
colorEmbed = { | |
title: colors[0].name, | |
fields: [ | |
{ | |
name: "HEX", | |
value: colorValues.HEXcolor, | |
}, | |
{ | |
name: "RGB", | |
value: colorValues.RGBcolor, | |
}, | |
], | |
thumbnail: { | |
url: "attachment://randomColor.png", | |
}, | |
}; | |
}) | |
.finally( | |
(module.exports = { | |
name: "Random Hex Color", | |
description: "Generates a random hex code for you and provides the color", | |
execute(msg, args) { | |
msg.channel.send({ | |
embeds: [colorEmbed], | |
files: [new DataImageAttachment(dataURL, "randomColor.png")], | |
}); | |
}, | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment