Skip to content

Commit

Permalink
fixed ugly white on transparent png
Browse files Browse the repository at this point in the history
  • Loading branch information
raluvy95 committed May 31, 2023
1 parent 9b0a6c2 commit 96feb8c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 62 deletions.
109 changes: 52 additions & 57 deletions src/commands/tools/catppify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,70 @@ import { DateTime } from "luxon";
import ms from "ms";

async function generator(msg: Message, args: string[]) {
if (process.env.CATPPIFY) {
let ext = !args[0] ? '' : args[0].split('.').at(-1)
let link: string = ''
let palette: string
let noise: string
let ext = !args[0] ? '' : args[0].split('.').at(-1)
let link: string = ''
let palette: string
let noise: string


if (args[0]?.startsWith("http://")) {
client.createMessage(msg.channel.id, "HTTP is not supported. Use HTTPS instead!")
if (args[0]?.startsWith("http://")) {
client.createMessage(msg.channel.id, "HTTP is not supported. Use HTTPS instead!")
return
}

if (!args[0]?.match('^https:\/\/') && !ext?.match(/(jp(e?)g|png|gif)/gi)) {
if (msg.attachments.length < 1) {
client.createMessage(msg.channel.id, "Invalid link")
return
}
link = msg.attachments[0].url
ext = link.split('.').at(-1)
palette = args[0]
noise = args[1]
} else {
link = args[0]
palette = args[1]
noise = args[2]
}

if (!args[0]?.match('^https:\/\/') && !ext?.match(/(jp(e?)g|png|gif)/gi)) {
if (msg.attachments.length < 1) {
client.createMessage(msg.channel.id, "Invalid link")
return
}
link = msg.attachments[0].url
ext = link.split('.').at(-1)
palette = args[0]
noise = args[1]
} else {
link = args[0]
palette = args[1]
noise = args[2]
}
if (ext?.toLowerCase() == "webp") {
client.createMessage(msg.channel.id, "Webp is not supported at this moment")
return
}

if (ext?.toLowerCase() == "webp") {
client.createMessage(msg.channel.id, "Webp is not supported at this moment")
if (!palette) {
palette = 'mocha'
} else {
const avaliable_palette = ['frappe', 'latte', 'macchiato', 'mocha', 'oled']
if (!avaliable_palette.includes(palette)) {
client.createMessage(msg.channel.id, `Invalid palette. Use one of the following avaliable palettes: ${avaliable_palette.join(", ")}`)
return
}

if (!palette) {
palette = 'mocha'
} else {
const avaliable_palette = ['frappe', 'latte', 'macchiato', 'mocha', 'oled']
if (!avaliable_palette.includes(palette)) {
client.createMessage(msg.channel.id, `Invalid palette. Use one of the following avaliable palettes: ${avaliable_palette.join(", ")}`)
return
}
}
if (!noise) {
noise = '4'
} else {
if (isNaN(Number(noise))) {
client.createMessage(msg.channel.id, `That's not a number lol`)
return
}
if (!noise) {
noise = '4'
} else {
if (isNaN(Number(noise))) {
client.createMessage(msg.channel.id, `That's not a number lol`)
return
}
const avaliable_noise = ['0', '1', '2', '3', '4']
if (!avaliable_noise.includes(noise)) {
client.createMessage(msg.channel.id, `Invalid palette. Use one of the following avaliable noises: ${avaliable_noise.join(", ")}`)
return
}
const avaliable_noise = ['0', '1', '2', '3', '4']
if (!avaliable_noise.includes(noise)) {
client.createMessage(msg.channel.id, `Invalid palette. Use one of the following avaliable noises: ${avaliable_noise.join(", ")}`)
return
}
const init_time = DateTime.now()
client.sendChannelTyping(msg.channel.id)
}
const init_time = DateTime.now()
client.sendChannelTyping(msg.channel.id)

const generated = await catppify(link, palette as Palette, noise as Noise)
const final_time = DateTime.now()
const diff = final_time.diff(init_time, ["hours", "minutes", "seconds", "milliseconds"])
const generated = await catppify(link, palette as Palette, noise as Noise)
const final_time = DateTime.now()
const diff = final_time.diff(init_time, ["hours", "minutes", "seconds", "milliseconds"])

client.createMessage(msg.channel.id, `Here you go (Took ${ms(diff.toMillis())})`, {
file: generated,
name: "generated.png"
})
} else {
client.createMessage(msg.channel.id, "Catppify is disabled.")
return
}
client.createMessage(msg.channel.id, `Here you go (Took ${ms(diff.toMillis())})`, {
file: generated,
name: "generated.png"
})
}

class Catppify extends MovCommand {
Expand Down
7 changes: 2 additions & 5 deletions src/utils/catppify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export async function catppify(url: string, palette: Palette = "mocha", noise: N
throw e
}

// TODO: optimize this
for (let x = 0; x < _img.width; x++) {
for (let y = 0; y < _img.height; y++) {
let chunk = ctx.getImageData(x, y, 1, 1).data
Expand All @@ -43,13 +42,11 @@ export async function catppify(url: string, palette: Palette = "mocha", noise: N
const c_pixel = ctxC.getImageData(c_x, c_y, 1, 1).data

const out_pixel = [...c_pixel, a]

ctx.fillStyle = `rgba(${out_pixel.join(", ")})`
const fillStyle = `rgba(${out_pixel[0]}, ${out_pixel[1]}, ${out_pixel[2]}, ${(a / 255).toFixed(5)})`
ctx.fillStyle = fillStyle
ctx.fillRect(x, y, 1, 1)
}
}

return out_img.toBuffer()


}

0 comments on commit 96feb8c

Please sign in to comment.