splashy 6.0.0-0 → 6.0.0-2

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.
Files changed (2) hide show
  1. package/package.json +8 -1
  2. package/src/index.js +15 -13
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": "6.0.0-0",
5
+ "version": "6.0.0-2",
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",
@@ -74,6 +75,12 @@
74
75
  "src"
75
76
  ],
76
77
  "license": "MIT",
78
+ "ava": {
79
+ "files": [
80
+ "test/**/*.js",
81
+ "!test/util.js"
82
+ ]
83
+ },
77
84
  "commitlint": {
78
85
  "extends": [
79
86
  "@commitlint/config-conventional"
package/src/index.js CHANGED
@@ -1,15 +1,10 @@
1
1
  'use strict'
2
2
 
3
- const quantize = require('@lokesh.dhakar/quantize')
3
+ const quantize = require('@lokesh.dhakar/quantize').default || require('@lokesh.dhakar/quantize')
4
4
  const ndarray = require('ndarray')
5
5
  const sharp = require('sharp')
6
6
 
7
- async function getPixels (buffer) {
8
- const { data, info } = await sharp(buffer)
9
- .ensureAlpha()
10
- .raw()
11
- .toBuffer({ resolveWithObject: true })
12
-
7
+ async function getPixels ({ data, info }) {
13
8
  return ndarray(
14
9
  new Uint8Array(data.buffer, data.byteOffset, data.length),
15
10
  [info.width, info.height, 4],
@@ -20,17 +15,15 @@ async function getPixels (buffer) {
20
15
 
21
16
  function createPixelArray (pixels, pixelCount, quality = 10) {
22
17
  const pixelArray = []
23
-
24
18
  for (let i = 0, offset; i < pixelCount; i += quality) {
25
19
  offset = i * 4
26
20
  const r = pixels[offset]
27
21
  const g = pixels[offset + 1]
28
22
  const b = pixels[offset + 2]
29
23
  const a = pixels[offset + 3]
30
-
31
- if ((a === undefined || a >= 125) && !(r > 250 && g > 250 && b > 250)) {
32
- pixelArray.push([r, g, b])
33
- }
24
+ const isOpaqueEnough = a >= 125
25
+ const isWhite = r > 250 && g > 250 && b > 250
26
+ if (isOpaqueEnough && !isWhite) pixelArray.push([r, g, b])
34
27
  }
35
28
 
36
29
  return pixelArray
@@ -39,9 +32,18 @@ function createPixelArray (pixels, pixelCount, quality = 10) {
39
32
  const toHex = ([r, g, b]) => '#' + (b | (g << 8) | (r << 16) | (1 << 24)).toString(16).slice(1)
40
33
 
41
34
  module.exports = async function (buffer) {
42
- const imgData = await getPixels(await sharp(buffer).toBuffer())
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
43
  const pixelCount = imgData.shape[0] * imgData.shape[1]
44
44
  const pixelArray = createPixelArray(imgData.data, pixelCount)
45
45
  const cmap = quantize(pixelArray, 10) // internal tuning
46
46
  return cmap.palette().slice(0, 6).map(toHex)
47
47
  }
48
+
49
+ module.exports.toHex = toHex