This commit is contained in:
shim
2023-04-17 11:06:08 +09:00
parent d0b393aa97
commit 76264e09ad
4686 changed files with 552713 additions and 0 deletions

View File

@@ -0,0 +1,101 @@
const getPixels = require('get-pixels');
const quantize = require('quantize');
function createPixelArray(imgData, pixelCount, quality) {
const pixels = imgData;
const pixelArray = [];
for (let i = 0, offset, r, g, b, a; i < pixelCount; i = i + quality) {
offset = i * 4;
r = pixels[offset + 0];
g = pixels[offset + 1];
b = pixels[offset + 2];
a = pixels[offset + 3];
// If pixel is mostly opaque and not white
if (typeof a === 'undefined' || a >= 125) {
if (!(r > 250 && g > 250 && b > 250)) {
pixelArray.push([r, g, b]);
}
}
}
return pixelArray;
}
function validateOptions(options) {
let { colorCount, quality } = options;
if (typeof colorCount === 'undefined' || !Number.isInteger(colorCount)) {
colorCount = 10;
} else if (colorCount === 1 ) {
throw new Error('colorCount should be between 2 and 20. To get one color, call getColor() instead of getPalette()');
} else {
colorCount = Math.max(colorCount, 2);
colorCount = Math.min(colorCount, 20);
}
if (typeof quality === 'undefined' || Number.isInteger(quality)) {
quality = 10;
} else if (quality < 1) {
quality = 10;
}
return {
colorCount,
quality
}
}
function loadImg(img) {
return new Promise((resolve, reject) => {
getPixels(img, function(err, data) {
if(err) {
reject(err)
} else {
resolve(data);
}
})
});
}
function getColor(img, quality) {
return new Promise((resolve, reject) => {
getPalette(img, 5, quality)
.then(palette => {
resolve(palette[0]);
})
.catch(err => {
reject(err);
})
});
}
function getPalette(img, colorCount = 10, quality = 10) {
const options = validateOptions({
colorCount,
quality
});
return new Promise((resolve, reject) => {
loadImg(img)
.then(imgData => {
const pixelCount = imgData.shape[0] * imgData.shape[1];
const pixelArray = createPixelArray(imgData.data, pixelCount, options.quality);
const cmap = quantize(pixelArray, options.colorCount);
const palette = cmap? cmap.palette() : null;
resolve(palette);
})
.catch(err => {
reject(err);
})
});
}
module.exports = {
getColor,
getPalette
};

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2015 Lokesh Dhakar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,37 @@
# Color Thief
A script for grabbing the color palette from an image. Uses Javascript and the canvas tag to make it happen.
[See a Demo](http://lokeshdhakar.com/projects/color-thief) | [Read more on my blog](http://lokeshdhakar.com/color-thief)
## How to use
### Import
- `/dist/color-thief.js`: CommonJS module for use in Node.
- `/dist/color-thief.mjs`: ES6 module for use in Browser. For modern browsers as well as Webpack and Rollup.
- `/dist/color-thief.umd.js`: UMD module for use in Browser. For simple script tag loading that exposes a global variable or for RequireJS AMD support. _color-thief.min.js_ is a duplicate of this file, kept around to maintain backwards compatibility.
### Get the dominant color from an image
```js
const colorThief = new ColorThief();
colorThief.getColor(sourceImage);
```
### Build a color palette from an image
In this example, we build an 8 color palette.
```js
const colorThief = new ColorThief();
colorThief.getPalette(sourceImage, 8);
```
### API
| Method | Return | Description |
| --- | --- | --- |
| `getColor(image [, quality])` | `[Number, Number, Number]` | WIP |
| `getPalette(image [, colorCount, quality]` | `[[Number, Number, Number], ...]` | WIP |