Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/late-books-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tidaltheory/lens': minor
---

Extract dominant colour from image
14 changes: 12 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import process from 'node:process'

import { globby } from 'globby'
import { JSONFile, Low } from 'lowdb'
import ora from 'ora'
import ora, { oraPromise } from 'ora'
import sade from 'sade'
import sharp from 'sharp'
import { PackageJson } from 'type-fest'

import { loadConfig } from './lib/context.js'
import { getDominantPalette } from './lib/dominant.js'
import { generateFingerprint } from './lib/fingerprint.js'
import { matchThumbnail, writeThumbnail } from './lib/thumbnail.js'
import type { ImageRecord, ImageThumbnails } from './types.js'
Expand Down Expand Up @@ -73,6 +74,10 @@ prog.command('add <src>')
let sharpImage = sharp(source)
let { width, height } = await sharpImage.metadata()
let fingerprint = await generateFingerprint(sharpImage)
let dominantPalette = await oraPromise(
getDominantPalette(sharpImage),
{ successText: 'Extracted dominant colours' }
)

let { dir, name: imageName, ext } = parse(source)
let filename = useFilenameDirectory
Expand Down Expand Up @@ -148,6 +153,7 @@ prog.command('add <src>')
let entry: ImageRecord = {
path: filename,
dimensions: { width, height },
colors: dominantPalette,
thumbnails: entryThumbnails,
}

Expand All @@ -171,7 +177,11 @@ prog.command('add <src>')
}
}

spinner.succeed(`Processed ${processed} images ...DONE!`)
spinner.succeed(
`Processed ${processed} ${
processed === 1 ? 'image' : 'images'
} ...DONE!`
)
})

prog.command('jpg <src>')
Expand Down
6 changes: 6 additions & 0 deletions src/lib/dominant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Sharp } from 'sharp'

export async function getDominantPalette(image: Sharp) {
let { dominant } = await image.stats()
return [`rgb(${dominant.r}, ${dominant.g}, ${dominant.b})`]
}