2023-05-14 11:22:32 -07:00
|
|
|
export class EliteMatrix {
|
|
|
|
red;
|
|
|
|
green;
|
|
|
|
blue;
|
|
|
|
constructor(matrixRed, matrixGreen, matrixBlue) {
|
|
|
|
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 => this.#round(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 => this.#round(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) => this.#round(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;
|
|
|
|
}
|
|
|
|
}
|
2023-05-14 12:52:32 -07:00
|
|
|
/* ------------------------------------------------------------------------------ #round ---- */
|
|
|
|
#round(n) {
|
|
|
|
return Math.round((n + Number.EPSILON) * 100) / 100;
|
|
|
|
}
|
2023-05-14 11:22:32 -07:00
|
|
|
}
|