splashy 5.1.47 → 6.0.0-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
File without changes
package/README.md CHANGED
@@ -22,7 +22,7 @@ $ npm install splashy --save
22
22
  ### From URL
23
23
 
24
24
  ```js
25
- (async () => {
25
+ ;(async () => {
26
26
  const splashy = require('splashy')
27
27
  const got = require('got')
28
28
 
@@ -38,7 +38,7 @@ $ npm install splashy --save
38
38
  ### From Buffer
39
39
 
40
40
  ```js
41
- (async () => {
41
+ ;(async () => {
42
42
  const splashy = require('splashy')
43
43
  const path = require('path')
44
44
  const fs = require('fs')
@@ -58,7 +58,7 @@ $ npm install splashy --save
58
58
 
59
59
  #### input
60
60
 
61
- *Required*<br>
61
+ _Required_<br>
62
62
  Type: [ImageSource](https://github.com/akfish/node-vibrant#imagesource)
63
63
 
64
64
  The raw content for detecting the color information.
@@ -71,7 +71,9 @@ The raw content for detecting the color information.
71
71
  ## License
72
72
 
73
73
  **microlink-function** © [Microlink](https://microlink.io), released under the [MIT](https://github.com/microlink/microlink-function/blob/master/LICENSE.md) License.<br>
74
+
74
75
  Authored and maintained by [Kiko Beats](https://kikobeats.com) with help from [contributors](https://github.com/microlink/microlink-function/contributors).
75
76
 
76
- > [microlink.io](https://microlink.io) · GitHub [microlinkhq](https://github.com/microlinkhq) · X [@microlinkhq](https://x.com/microlinkhq)
77
+ Special thanks to [Tim Carry](https://github.com/pixelastic) for writing the benchmark and [Lokesh Dhakar](https://github.com/lokesh) for the original code implementation.
77
78
 
79
+ > [microlink.io](https://microlink.io) · GitHub [microlinkhq](https://github.com/microlinkhq) · X [@microlinkhq](https://x.com/microlinkhq)
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "splashy",
3
3
  "description": "Given an image, extract predominant & palette colors",
4
4
  "homepage": "https://github.com/microlinkhq/splashy",
5
- "version": "5.1.47",
5
+ "version": "6.0.0-1",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "name": "Kiko Beats",
@@ -28,6 +28,7 @@
28
28
  "keywords": [
29
29
  "canvas",
30
30
  "color",
31
+ "colorthief",
31
32
  "colour",
32
33
  "dominant",
33
34
  "extract",
@@ -41,7 +42,9 @@
41
42
  "url"
42
43
  ],
43
44
  "dependencies": {
45
+ "@lokesh.dhakar/quantize": "~1.4.0",
44
46
  "debug-logfmt": "~1.2.3",
47
+ "ndarray": "~1.0.19",
45
48
  "node-addon-api": "~8.3.0",
46
49
  "node-gyp": "~11.0.0",
47
50
  "node-vibrant": "~3.2.1-alpha.1",
@@ -71,21 +74,13 @@
71
74
  "files": [
72
75
  "src"
73
76
  ],
74
- "scripts": {
75
- "clean": "rm -rf node_modules",
76
- "contributors": "(npx git-authors-cli && npx finepack && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
77
- "dev": "nodemon --exec \"npm start\" -e \"js\"",
78
- "lint": "standard-markdown README.md && standard",
79
- "postrelease": "npm run release:tags && npm run release:github && (ci-publish || npm publish --access=public)",
80
- "preinstall": "npx bin-version-check-cli magick \">=7\"",
81
- "pretest": "npm run lint",
82
- "release": "standard-version -a",
83
- "release:github": "conventional-github-releaser -p angular",
84
- "release:tags": "git push --follow-tags origin HEAD:master",
85
- "start": "node index.js",
86
- "test": "c8 ava"
87
- },
88
77
  "license": "MIT",
78
+ "ava": {
79
+ "files": [
80
+ "test/**/*.js",
81
+ "!test/util.js"
82
+ ]
83
+ },
89
84
  "commitlint": {
90
85
  "extends": [
91
86
  "@commitlint/config-conventional"
@@ -111,5 +106,19 @@
111
106
  "simple-git-hooks": {
112
107
  "commit-msg": "npx commitlint --edit",
113
108
  "pre-commit": "npx nano-staged"
109
+ },
110
+ "scripts": {
111
+ "clean": "rm -rf node_modules",
112
+ "contributors": "(npx git-authors-cli && npx finepack && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
113
+ "dev": "nodemon --exec \"npm start\" -e \"js\"",
114
+ "lint": "standard-markdown README.md && standard",
115
+ "postrelease": "npm run release:tags && npm run release:github && (ci-publish || npm publish --access=public)",
116
+ "preinstall": "npx bin-version-check-cli magick \">=7\"",
117
+ "pretest": "npm run lint",
118
+ "release": "standard-version -a",
119
+ "release:github": "conventional-github-releaser -p angular",
120
+ "release:tags": "git push --follow-tags origin HEAD:master",
121
+ "start": "node index.js",
122
+ "test": "c8 ava"
114
123
  }
115
- }
124
+ }
package/src/index.js CHANGED
@@ -1,34 +1,49 @@
1
1
  'use strict'
2
2
 
3
- const { serializeError } = require('serialize-error')
4
- const debug = require('debug-logfmt')('splashy')
5
- const createVibrant = require('./vibrant')
6
-
7
- const toPalette = swatch =>
8
- Object.keys(swatch)
9
- .reduce((acc, key) => {
10
- const value = swatch[key]
11
- if (value) {
12
- acc.push({
13
- popularity: value.getPopulation(),
14
- hex: value.getHex()
15
- })
16
- }
17
- return acc
18
- }, [])
19
- .sort((a, b) => a.popularity <= b.popularity)
20
- .map(color => color.hex)
21
-
22
- module.exports = async input => {
23
- let swatch
24
-
25
- try {
26
- const vibrant = createVibrant(input)
27
- swatch = await vibrant.getPalette()
28
- } catch (error) {
29
- debug.error(serializeError(error))
30
- swatch = {}
3
+ const quantize = require('@lokesh.dhakar/quantize')
4
+ const ndarray = require('ndarray')
5
+ const sharp = require('sharp')
6
+
7
+ async function getPixels ({ data, info }) {
8
+ return ndarray(
9
+ new Uint8Array(data.buffer, data.byteOffset, data.length),
10
+ [info.width, info.height, 4],
11
+ [4, (4 * info.width) | 0, 1],
12
+ 0
13
+ )
14
+ }
15
+
16
+ function createPixelArray (pixels, pixelCount, quality = 10) {
17
+ const pixelArray = []
18
+ for (let i = 0, offset; i < pixelCount; i += quality) {
19
+ offset = i * 4
20
+ const r = pixels[offset]
21
+ const g = pixels[offset + 1]
22
+ const b = pixels[offset + 2]
23
+ const a = pixels[offset + 3]
24
+ const isOpaqueEnough = a >= 125
25
+ const isWhite = r > 250 && g > 250 && b > 250
26
+ if (isOpaqueEnough && !isWhite) pixelArray.push([r, g, b])
31
27
  }
32
28
 
33
- return toPalette(swatch)
29
+ return pixelArray
30
+ }
31
+
32
+ const toHex = ([r, g, b]) => '#' + (b | (g << 8) | (r << 16) | (1 << 24)).toString(16).slice(1)
33
+
34
+ module.exports = async function (buffer) {
35
+ const raw = await sharp(buffer)
36
+ // resizing the image before processing leads to more consistent (and much shorter) processing times.
37
+ // .resize(200, 200, { fit: 'inside', withoutEnlargement: true })
38
+ .ensureAlpha()
39
+ .raw()
40
+ .toBuffer({ resolveWithObject: true })
41
+
42
+ const imgData = await getPixels(raw)
43
+ const pixelCount = imgData.shape[0] * imgData.shape[1]
44
+ const pixelArray = createPixelArray(imgData.data, pixelCount)
45
+ const cmap = quantize(pixelArray, 10) // internal tuning
46
+ return cmap.palette().slice(0, 6).map(toHex)
34
47
  }
48
+
49
+ module.exports.toHex = toHex
package/src/vibrant.js DELETED
@@ -1,62 +0,0 @@
1
- 'use strict'
2
-
3
- const { ImageBase } = require('@vibrant/image')
4
- const Vibrant = require('node-vibrant')
5
- const sharp = require('sharp')
6
-
7
- class SharpImage extends ImageBase {
8
- constructor () {
9
- super(...arguments)
10
- this._image = undefined
11
- }
12
-
13
- async load (image) {
14
- if (typeof image === 'string' || image instanceof Buffer) {
15
- const { data, info } = await sharp(image)
16
- // resizing the image before processing leads to more consistent (and much shorter) processing times.
17
- .resize(200, 200, { fit: 'inside', withoutEnlargement: true })
18
- .ensureAlpha()
19
- .raw()
20
- .toBuffer({ resolveWithObject: true })
21
-
22
- this._image = {
23
- width: info.width,
24
- height: info.height,
25
- data
26
- }
27
-
28
- return this
29
- } else {
30
- return Promise.reject(
31
- new Error('Cannot load image from HTMLImageElement in node environment')
32
- )
33
- }
34
- }
35
-
36
- clear () {}
37
-
38
- update () {}
39
-
40
- getWidth () {
41
- return this._image.width
42
- }
43
-
44
- getHeight () {
45
- return this._image.height
46
- }
47
-
48
- resize () {}
49
-
50
- getPixelCount () {
51
- const { width, height } = this._image
52
- return width * height
53
- }
54
-
55
- getImageData () {
56
- return this._image
57
- }
58
-
59
- remove () {}
60
- }
61
-
62
- module.exports = input => new Vibrant(input, { ImageClass: SharpImage })