elite-matrix/lib/cjs/index.js

65 lines
2.8 KiB
JavaScript
Raw Normal View History

2023-05-14 11:22:32 -07:00
"use strict";
2023-05-14 12:52:32 -07:00
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _EliteMatrix_instances, _EliteMatrix_round;
2023-05-14 11:22:32 -07:00
Object.defineProperty(exports, "__esModule", { value: true });
exports.EliteMatrix = void 0;
class EliteMatrix {
constructor(matrixRed, matrixGreen, matrixBlue) {
2023-05-14 12:52:32 -07:00
_EliteMatrix_instances.add(this);
2023-05-14 11:22:32 -07:00
this.red = matrixRed;
this.green = matrixGreen;
this.blue = matrixBlue;
}
/* ------------------------------------------------------------------------- filterColor ---- */
filterColor(color) {
let rgb;
// Convert hex to RGB.
if (typeof color === 'string') {
const red = parseInt(color.slice(1, 3), 16) / 255;
const green = parseInt(color.slice(3, 5), 16) / 255;
const blue = parseInt(color.slice(5, 7), 16) / 255;
rgb = [red, green, blue];
2023-05-14 12:52:32 -07:00
// Round to 2 decimal places.
rgb = rgb.map(n => __classPrivateFieldGet(this, _EliteMatrix_instances, "m", _EliteMatrix_round).call(this, n));
2023-05-14 11:22:32 -07:00
}
else {
2023-05-14 12:52:32 -07:00
// Convert RGB decimal to RGB percent.
rgb = color.map(n => __classPrivateFieldGet(this, _EliteMatrix_instances, "m", _EliteMatrix_round).call(this, n / 255));
2023-05-14 11:22:32 -07:00
}
// Apply matrix filter.
let newColor = [];
let i = 0;
while (i < 3) {
newColor.push(this.red[i] * rgb[0] +
this.green[i] * rgb[1] +
this.blue[i] * rgb[2]);
i++;
}
2023-05-14 12:52:32 -07:00
// Make sure we don't have anything above 1 or below 0.
newColor = newColor.map((n) => Math.max(Math.min(n, 1), 0));
// Round again.
newColor = newColor.map((n) => __classPrivateFieldGet(this, _EliteMatrix_instances, "m", _EliteMatrix_round).call(this, n));
2023-05-14 11:22:32 -07:00
// Return the same data type as user put in.
if (Array.isArray(color)) {
2023-05-14 12:52:32 -07:00
return newColor.map(n => Math.round(n * 255));
2023-05-14 11:22:32 -07:00
}
else {
let hex = '#';
newColor.forEach((n) => {
n *= 255;
2023-05-14 12:52:32 -07:00
n = Math.round(n);
2023-05-14 11:22:32 -07:00
hex += n.toString(16);
});
return hex;
}
}
}
exports.EliteMatrix = EliteMatrix;
2023-05-14 12:52:32 -07:00
_EliteMatrix_instances = new WeakSet(), _EliteMatrix_round = function _EliteMatrix_round(n) {
return Math.round((n + Number.EPSILON) * 100) / 100;
};