hcb-geo-pattern
Version:
Create beautiful generative geometric background images from a string.
41 lines • 1.79 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseColorGenerator = void 0;
const color_1 = __importDefault(require("color"));
const processing_1 = require("../processing");
class BaseColorGenerator {
constructor(color, seed, grayScale) {
this.color = color;
this.seed = seed;
this.grayScale = grayScale;
}
generate() {
return this.transform(this.color, this.seed);
}
transform(color, seed) {
const hueOffset = (0, processing_1.map)(seed.read(14, 3), 0, 4095, 0, 359);
const satOffset = seed.read(17, 1);
let newColor = (0, color_1.default)(color).hsl();
newColor = newColor.hue(newColor.hue() - hueOffset);
newColor = satOffset % 2 === 0 ?
newColor.saturationl(newColor.saturationl() + satOffset) :
newColor.saturationl(newColor.saturationl() - satOffset);
if (this.grayScale !== 0 && this.grayScale !== null && this.grayScale !== undefined) {
const rgbObj = newColor.rgb().object();
const avg = (rgbObj.r * 0.30 + rgbObj.g * 0.59 + rgbObj.b * 0.11);
const final_r = rgbObj.r + (avg - rgbObj.r) * this.grayScale;
const final_g = rgbObj.g + (avg - rgbObj.g) * this.grayScale;
const final_b = rgbObj.b + (avg - rgbObj.b) * this.grayScale;
const grayColor = color_1.default.rgb(final_r, final_g, final_b);
return grayColor.rgb();
}
else {
return newColor.rgb();
}
}
}
exports.BaseColorGenerator = BaseColorGenerator;
//# sourceMappingURL=base-color-generator.js.map