mirror of
https://github.com/syuilo/ai.git
synced 2025-03-25 21:12:56 +00:00
単色の画像を生成してリプライで返すように
This commit is contained in:
parent
e5e9d0076e
commit
d23ffe197c
2 changed files with 41 additions and 5 deletions
|
@ -1,6 +1,7 @@
|
||||||
import autobind from 'autobind-decorator';
|
import autobind from 'autobind-decorator';
|
||||||
import Module from '@/module';
|
import Module from '@/module';
|
||||||
import Message from '@/message';
|
import Message from '@/message';
|
||||||
|
import { generateColorSample } from './render';
|
||||||
|
|
||||||
export default class extends Module {
|
export default class extends Module {
|
||||||
public readonly name = 'color';
|
public readonly name = 'color';
|
||||||
|
@ -21,13 +22,31 @@ export default class extends Module {
|
||||||
const b = Math.floor(Math.random() * 256);
|
const b = Math.floor(Math.random() * 256);
|
||||||
// rgbをhexに変換する
|
// rgbをhexに変換する
|
||||||
const hex = `${r.toString(16)}${g.toString(16)}${b.toString(16)}`;
|
const hex = `${r.toString(16)}${g.toString(16)}${b.toString(16)}`;
|
||||||
const message = `RGB: ${r}, ${g}, ${b} (#${hex})とかどう? [参考](https://www.colorhexa.com/${hex})`
|
const message = `RGB: ${r}, ${g}, ${b} (# ${hex})とかどう?`
|
||||||
msg.reply(message, {
|
|
||||||
immediate: true
|
setTimeout(async () => {
|
||||||
});
|
const file = await this.getColorSampleFile(r,g,b);
|
||||||
return true;
|
this.log('Replying...');
|
||||||
|
msg.reply(message, { file });
|
||||||
|
}, 500);
|
||||||
|
return {
|
||||||
|
reaction: 'like'
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@autobind
|
||||||
|
private async getColorSampleFile(r,g,b): Promise<any> {
|
||||||
|
const colorSample = generateColorSample(r,g,b);
|
||||||
|
|
||||||
|
this.log('Image uploading...');
|
||||||
|
const file = await this.ai.upload(colorSample, {
|
||||||
|
filename: 'color.png',
|
||||||
|
contentType: 'image/png'
|
||||||
|
});
|
||||||
|
|
||||||
|
return file;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
17
src/modules/color/render.ts
Normal file
17
src/modules/color/render.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
import { createCanvas } from 'canvas';
|
||||||
|
|
||||||
|
const imageSize = 512; //px
|
||||||
|
|
||||||
|
export function generateColorSample(r: string, g: string, b: string) {
|
||||||
|
const canvas = createCanvas(imageSize, imageSize);
|
||||||
|
const ctx = canvas.getContext('2d');
|
||||||
|
ctx.antialias = 'none';
|
||||||
|
|
||||||
|
// 引数で渡されたrgb値を基準に、色を塗りつぶす
|
||||||
|
ctx.fillStyle = `rgb(${r},${g},${b})`;
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.fillRect(0, 0, imageSize, imageSize);
|
||||||
|
|
||||||
|
// canvas.toBuffer()をreturn
|
||||||
|
return canvas.toBuffer();
|
||||||
|
}
|
Loading…
Reference in a new issue