import sharp from 'sharp';
import Prevvy from 'prevvy';
import path from 'path';
import { getTmpFile } from './file.ts';

export async function getProminentColor(imageFile: string): Promise<string> {
    const { dominant } = await sharp(imageFile).stats();
    const { r, g, b } = dominant;
    return rgbToHex(r, g, b);
}

export function rgbToHex(r: number, g: number, b: number): string {
    return "#" + (1 << 24 | r << 16 | g << 8 | b).toString(16).slice(1);
}

export async function getStoryboard(imageFileOrUrl: string): Promise<string> {
    let { name } = path.parse(imageFileOrUrl);
    let outputImagePath = getTmpFile(`${name}.png`);
    let options = {
        input: imageFileOrUrl,
        output: outputImagePath,
        width: 128,
        cols: 5,
        rows: 5,
    };
    let prevvy = new Prevvy(options);
    await prevvy.generate();
    return outputImagePath;
}