const canvas = require('canvas') const cnv = canvas.createCanvas(426.6, 240) const ctx = cnv.getContext('2d') const colorsys = require('colorsys') async function convertImage (src, callback) { const img = await canvas.loadImage(src).catch(callback) if (!img) return ctx.drawImage(img, 0, 0, cnv.width, cnv.height) const rgba = ctx.getImageData(0, 0, cnv.width, cnv.height).data const lines = [] let line = [] for (let i = 0; i < rgba.length; i += 4) { const r = rgba[i] const g = rgba[i + 1] const b = rgba[i + 2] // const a = rgba[i + 3]; const hex = colorsys.rgbToHex(r, g, b) if (line.length >= 1 && line[line.length - 1].color === hex) { line[line.length - 1].text += '.' } else { line.push({ text: '.', color: hex }) } if (((i / 4) % cnv.width) === 0) { lines.push(JSON.stringify(line)) line = [] } } callback(undefined, lines) } module.exports = convertImage