Skip to content

Commit

Permalink
perf(benchmark): add missing dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Dec 14, 2024
1 parent 63e8646 commit f6bb0bf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
43 changes: 24 additions & 19 deletions benchmark/index.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { readFile, writeFile } from 'fs/promises'
import { fileURLToPath } from 'node:url'
import colorthief from 'colorthief'
import Vibrant from 'node-vibrant'
import tinycolor from 'tinycolor2'
import { chain } from 'lodash-es'
import { readdirSync } from 'fs'
import mql from '@microlink/mql'
import path from 'node:path'

import splashy from '../src/index.js'

const __dirname = import.meta.dirname
const { toHex } = splashy

const __dirname = path.dirname(fileURLToPath(import.meta.url))

const images = readdirSync(path.resolve(__dirname, 'fixtures')).map(filename =>
path.resolve(__dirname, 'fixtures', filename)
Expand All @@ -36,10 +38,7 @@ async function getPaletteWithNodeVibrant (filepath) {
.map()
.sortBy('_population')
.reverse()
.map(value => {
const [r, g, b] = value._rgb
return tinycolor({ r, g, b }).toHex()
})
.map(value => toHex(value._rgb))
.value()
}

Expand All @@ -54,14 +53,7 @@ async function getPaletteWithSplashy (filepath) {
async function getPaletteWithColorthief (filepath) {
const buffer = await readFile(filepath)
const colorthiefPalette = await colorthief.getPalette(buffer)
const result = chain(colorthiefPalette)
.map(value => {
const [r, g, b] = value
return tinycolor({ r, g, b }).toHex()
})
.value()

return result
return colorthiefPalette.map(toHex)
}

const output = []
Expand All @@ -77,21 +69,34 @@ for (const [index, imagePath] of images.entries()) {

const nodeVibrantPalette = await getPaletteWithNodeVibrant(filepath)
const vibrantScreenshotUrl = await screenshotUrl(nodeVibrantPalette)
await writeFile(path.resolve(__dirname, 'output', `node-vibrant-${displayIndex}.png`), await download(vibrantScreenshotUrl))
await writeFile(
path.resolve(__dirname, 'output', `node-vibrant-${displayIndex}.png`),
await download(vibrantScreenshotUrl)
)
output.push('### Palette by `node-vibrant`')
output.push(`[![node-vibrant](./output/node-vibrant-${displayIndex}.png)](${vibrantScreenshotUrl})`)
output.push(
`[![node-vibrant](./output/node-vibrant-${displayIndex}.png)](${vibrantScreenshotUrl})`
)

const splashyPalette = await getPaletteWithSplashy(filepath)
const splashyLinkScreenshotUrl = await screenshotUrl(splashyPalette)
await writeFile(path.resolve(__dirname, 'output', `splashy-${displayIndex}.png`), await download(splashyLinkScreenshotUrl))
await writeFile(
path.resolve(__dirname, 'output', `splashy-${displayIndex}.png`),
await download(splashyLinkScreenshotUrl)
)
output.push('### Palette by `splashy`')
output.push(`[![splashy](./output/splashy-${displayIndex}.png)](${splashyLinkScreenshotUrl})`)

const colorthiefPalette = await getPaletteWithColorthief(filepath)
const colorthiefScreenshotUrl = await screenshotUrl(colorthiefPalette)
await writeFile(path.resolve(__dirname, 'output', `colorthief-${displayIndex}.png`), await download(colorthiefScreenshotUrl))
await writeFile(
path.resolve(__dirname, 'output', `colorthief-${displayIndex}.png`),
await download(colorthiefScreenshotUrl)
)
output.push('### Palette by `colorthief`')
output.push(`[![colorthief](./output/colorthief-${displayIndex}.png)](${colorthiefScreenshotUrl})`)
output.push(
`[![colorthief](./output/colorthief-${displayIndex}.png)](${colorthiefScreenshotUrl})`
)
}

const readmeContent = `
Expand Down
9 changes: 9 additions & 0 deletions benchmark/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@splashy/benchmark",
"private": true,
"devDependencies": {
"@microlink/mql": "~0.13.12",
"colorthief": "~2.6.0",
"lodash-es": "~4.17.21"
}
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ module.exports = async function (buffer) {
const cmap = quantize(pixelArray, 10) // internal tuning
return cmap.palette().slice(0, 6).map(toHex)
}

module.exports.toHex = toHex

0 comments on commit f6bb0bf

Please sign in to comment.