sharp-vibrant
Version:
Extract prominent colors from an image in a node environment using sharp.
83 lines • 3.39 kB
JavaScript
"use strict";
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
privateMap.set(receiver, value);
return value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
};
var _rgb, _population, _hsl, _yiq, _hex, _titleTextColor, _bodyTextColor;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Swatch = void 0;
const util_1 = require("./util");
class Swatch {
constructor(rgb, population) {
_rgb.set(this, void 0);
_population.set(this, void 0);
_hsl.set(this, void 0);
_yiq.set(this, void 0);
_hex.set(this, void 0);
_titleTextColor.set(this, void 0);
_bodyTextColor.set(this, void 0);
__classPrivateFieldSet(this, _rgb, rgb);
__classPrivateFieldSet(this, _population, population);
}
static applyFilter(colors, f) {
if (typeof f !== 'function') {
return colors;
}
return [].filter.call(colors, ({ r, g, b }) => f(r, g, b, 255));
}
get r() { return __classPrivateFieldGet(this, _rgb)[0]; }
get g() { return __classPrivateFieldGet(this, _rgb)[1]; }
get b() { return __classPrivateFieldGet(this, _rgb)[2]; }
get rgb() { return __classPrivateFieldGet(this, _rgb); }
get hsl() {
if (!__classPrivateFieldGet(this, _hsl)) {
__classPrivateFieldSet(this, _hsl, util_1.rgbToHsl(...__classPrivateFieldGet(this, _rgb)));
}
return __classPrivateFieldGet(this, _hsl);
}
get hex() {
if (!__classPrivateFieldGet(this, _hex)) {
const [r, g, b] = __classPrivateFieldGet(this, _rgb);
__classPrivateFieldSet(this, _hex, util_1.rgbToHex(r, g, b));
}
return __classPrivateFieldGet(this, _hex);
}
get population() { return __classPrivateFieldGet(this, _population); }
toJSON() {
return {
rgb: this.rgb,
population: this.population,
};
}
getYiq() {
if (!__classPrivateFieldGet(this, _yiq)) {
const rgb = __classPrivateFieldGet(this, _rgb);
__classPrivateFieldSet(this, _yiq, (rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1000);
}
return __classPrivateFieldGet(this, _yiq);
}
get titleTextColor() {
if (!__classPrivateFieldGet(this, _titleTextColor)) {
__classPrivateFieldSet(this, _titleTextColor, this.getYiq() < 200 ? '#fff' : '#000');
}
return __classPrivateFieldGet(this, _titleTextColor);
}
get bodyTextColor() {
if (!__classPrivateFieldGet(this, _bodyTextColor)) {
__classPrivateFieldSet(this, _bodyTextColor, this.getYiq() < 150 ? '#fff' : '#000');
}
return __classPrivateFieldGet(this, _bodyTextColor);
}
}
exports.Swatch = Swatch;
_rgb = new WeakMap(), _population = new WeakMap(), _hsl = new WeakMap(), _yiq = new WeakMap(), _hex = new WeakMap(), _titleTextColor = new WeakMap(), _bodyTextColor = new WeakMap();
//# sourceMappingURL=color.js.map