mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-24 22:34:46 +02:00
Automatically determine whether to silhouette in challenger
This commit is contained in:
@@ -2,7 +2,7 @@ const Command = require('../../structures/Command');
|
|||||||
const { createCanvas, loadImage } = require('canvas');
|
const { createCanvas, loadImage } = require('canvas');
|
||||||
const request = require('node-superfetch');
|
const request = require('node-superfetch');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { silhouette, centerImagePart } = require('../../util/Canvas');
|
const { silhouette, hasAlpha, centerImagePart } = require('../../util/Canvas');
|
||||||
|
|
||||||
module.exports = class ChallengerCommand extends Command {
|
module.exports = class ChallengerCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
@@ -36,7 +36,7 @@ module.exports = class ChallengerCommand extends Command {
|
|||||||
key: 'silhouetted',
|
key: 'silhouetted',
|
||||||
prompt: 'Should the image be silhouetted?',
|
prompt: 'Should the image be silhouetted?',
|
||||||
type: 'boolean',
|
type: 'boolean',
|
||||||
default: false
|
default: true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'image',
|
key: 'image',
|
||||||
@@ -65,6 +65,7 @@ module.exports = class ChallengerCommand extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
silhouetteImage(image) {
|
silhouetteImage(image) {
|
||||||
|
if (!hasAlpha(image)) return image;
|
||||||
const canvas = createCanvas(image.width, image.height);
|
const canvas = createCanvas(image.width, image.height);
|
||||||
const ctx = canvas.getContext('2d');
|
const ctx = canvas.getContext('2d');
|
||||||
ctx.drawImage(image, 0, 0);
|
ctx.drawImage(image, 0, 0);
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "xiao",
|
"name": "xiao",
|
||||||
"version": "112.13.2",
|
"version": "112.13.3",
|
||||||
"description": "Your personal server companion.",
|
"description": "Your personal server companion.",
|
||||||
"main": "Xiao.js",
|
"main": "Xiao.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
const { createCanvas } = require('canvas');
|
||||||
|
|
||||||
module.exports = class CanvasUtil {
|
module.exports = class CanvasUtil {
|
||||||
static greyscale(ctx, x, y, width, height) {
|
static greyscale(ctx, x, y, width, height) {
|
||||||
const data = ctx.getImageData(x, y, width, height);
|
const data = ctx.getImageData(x, y, width, height);
|
||||||
@@ -77,6 +79,20 @@ module.exports = class CanvasUtil {
|
|||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static hasAlpha(image) {
|
||||||
|
const canvas = createCanvas(image.width, image.height);
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
const data = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
|
||||||
|
let hasAlphaPixels = false;
|
||||||
|
for (let i = 3, n = data.length; i < n; i += 4) {
|
||||||
|
if (data[i] < 255) {
|
||||||
|
hasAlphaPixels = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hasAlphaPixels;
|
||||||
|
}
|
||||||
|
|
||||||
static drawImageWithTint(ctx, image, color, x, y, width, height) {
|
static drawImageWithTint(ctx, image, color, x, y, width, height) {
|
||||||
const { fillStyle, globalAlpha } = ctx;
|
const { fillStyle, globalAlpha } = ctx;
|
||||||
ctx.fillStyle = color;
|
ctx.fillStyle = color;
|
||||||
|
|||||||
Reference in New Issue
Block a user