You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+44-12Lines changed: 44 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,9 +8,19 @@ Dont-crop is a [small](#performance), dependency free javascript library to fit
8
8
9
9
It can be used to pad images instead of cropping them, for a very compact [blur up](https://engineering.fb.com/2015/08/06/android/the-technology-behind-preview-photos/) and what ever else you can come up with.
10
10
11
-

11
+
## Examples
12
+
13
+
### fitGradient()
14
+

12
15
Photo by [Abed Ismail](https://unsplash.com/photos/fZXZ1-hbFrY)
13
16
17
+
### getPalette()
18
+

19
+
20
+
### More Examples
21
+
22
+
View the [demo page](https://29a.ch/sandbox/2021/dont-crop/) to see more examples and experiment with your own images.
23
+
14
24
## Installation
15
25
```
16
26
npm install -S dont-crop
@@ -68,29 +78,48 @@ It has been tested in:
68
78
The code is reasonably compact and built with tree shaking in mind.
69
79
So your bundles will only include the features you actually use.
70
80
71
-
When using `fitGradient` only and bundling your code using webpack 5 dont-crop will add about **1.2 kb** to your bundle size.
72
-
`getPalette` will cost you a bit more than **1.4 kb**.
73
-
You can use both for about **2.1 kb**.
81
+
When using `fitGradient` only and bundling your code using webpack 5 dont-crop will add about **1.2 kb** (0.7 gzipped) to your bundle size.
82
+
`getPalette` will cost you a bit more than **3.2 kb** (1.7 gzipped).
83
+
You can use both for about **4 kb** (2 gzipped).
84
+
85
+
```
86
+
3925 dist/both.js
87
+
1911 dist/both.js.gz
88
+
1264 dist/fitGradient.js
89
+
710 dist/fitGradient.js.gz
90
+
3261 dist/getPalette.js
91
+
1656 dist/getPalette.js.gz
92
+
```
74
93
75
94
Runtime performance is also fast enough not to worry about.
76
95
77
96
```
78
97
# on a AMD Ryzen 9 5950X
79
-
fitGradientToImageData x 43,559 ops/sec ±0.40% (97 runs sampled)
80
-
getPaletteFromImageData x 5,420 ops/sec ±0.21% (92 runs sampled)
98
+
fitGradientToImageData x 19,813 ops/sec ±0.96% (97 runs sampled)
99
+
getPaletteFromImageData(fast=false) x 156 ops/sec ±0.66% (83 runs sampled)
100
+
getPaletteFromImageData(fast=true) x 645 ops/sec ±0.17% (97 runs sampled)
81
101
```
82
102
83
103
The versions of the functions operating on images rather than the already downscaled image data are slower.
84
-
Their performance depends on the exact browser and device in question as well but it should generally be in the ballpark of few milliseconds for reasonably sized images.
104
+
Their performance depends on the exact browser and device in question as well but it should generally be in the ballpark of a few milliseconds for reasonably sized images.
85
105
86
106
## Test Coverage
87
107
88
108
The code is well covered in tests. The examples are used as end to end tests in both node and a browser (chrome via puppeteer).
89
109
110
+
90
111
## Algorithms
91
112
92
113
Glad you asked. `fitGradient()` is using simple [linear regression](https://en.wikipedia.org/wiki/Linear_regression).
93
-
`getPallete()` is based on [k-means](https://en.wikipedia.org/wiki/K-means_clustering).
114
+
115
+
`getPalete()` is based on [k-means](https://en.wikipedia.org/wiki/K-means_clustering).
116
+
The initial clusters are chosen using a histogram.
117
+
Similar clusters in the result are merged in a post processing step.
118
+
This is necessary because k-means tends to return equally sized clusters
119
+
whereas getPalette is supposed to return distinct clusters.
120
+
The merging is tuned to preserve different hues and colors rather than returning the most prominent shades of color (which might all share a similar hue).
121
+
The processing happens in the CIE Lab color space using CIE76 ΔE*.
122
+
94
123
95
124
## Alternatives
96
125
@@ -107,6 +136,11 @@ Provides similar functionality to getPalette.
107
136
It weighs in at about 6.4k (version 2.3.2).
108
137
It's been widely used since 2019 so it is definitely more battle proofen.
109
138
From a quick looks it seems to be using median-cut which will likely yield a bit better results than the simplistic k-means used here.
0 commit comments