This commit is contained in:
punkfairie 2023-05-14 11:22:32 -07:00
parent 9456bb8505
commit 16fb9e9cb0
7 changed files with 136 additions and 3 deletions

49
lib/cjs/index.js Normal file
View file

@ -0,0 +1,49 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EliteMatrix = void 0;
class EliteMatrix {
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];
}
else {
rgb = color;
}
// 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++;
}
newColor.forEach((n) => {
Math.max(Math.min(n, 1), 0);
});
// Return the same data type as user put in.
if (Array.isArray(color)) {
return newColor;
}
else {
let hex = '#';
newColor.forEach((n) => {
n *= 255;
hex += n.toString(16);
});
return hex;
}
}
}
exports.EliteMatrix = EliteMatrix;

11
lib/cjs/types/index.d.ts vendored Normal file
View file

@ -0,0 +1,11 @@
type matrix = [number, number, number];
type rgbColor = [number, number, number];
export declare class EliteMatrix {
red: matrix;
green: matrix;
blue: matrix;
constructor(matrixRed: matrix, matrixGreen: matrix, matrixBlue: matrix);
filterColor(color: rgbColor | string): rgbColor | string;
}
export {};
//# sourceMappingURL=index.d.ts.map

View file

@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,KAAK,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AACvC,KAAK,QAAQ,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAEzC,qBAAa,WAAW;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;gBAED,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM;IAQtE,WAAW,CAAC,KAAK,EAAE,QAAQ,GAAC,MAAM,GAAG,QAAQ,GAAC,MAAM;CA6CvD"}

48
lib/esm/index.mjs Normal file
View file

@ -0,0 +1,48 @@
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];
}
else {
rgb = color;
}
// 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++;
}
newColor.forEach((n) => {
Math.max(Math.min(n, 1), 0);
});
// Return the same data type as user put in.
if (Array.isArray(color)) {
return newColor;
}
else {
let hex = '#';
newColor.forEach((n) => {
n *= 255;
hex += n.toString(16);
});
return hex;
}
}
}

11
lib/esm/types/index.d.ts vendored Normal file
View file

@ -0,0 +1,11 @@
type matrix = [number, number, number];
type rgbColor = [number, number, number];
export declare class EliteMatrix {
red: matrix;
green: matrix;
blue: matrix;
constructor(matrixRed: matrix, matrixGreen: matrix, matrixBlue: matrix);
filterColor(color: rgbColor | string): rgbColor | string;
}
export {};
//# sourceMappingURL=index.d.ts.map

View file

@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,KAAK,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AACvC,KAAK,QAAQ,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAEzC,qBAAa,WAAW;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;gBAED,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM;IAQtE,WAAW,CAAC,KAAK,EAAE,QAAQ,GAAC,MAAM,GAAG,QAAQ,GAAC,MAAM;CA6CvD"}

View file

@ -25,6 +25,9 @@ export class EliteMatrix {
rgb = [red, green, blue];
// Round to 2 decimal places.
rgb = (rgb.map((n) => this.#round(n)) as rgbColor);
} else {
rgb = color;
}
@ -41,9 +44,11 @@ export class EliteMatrix {
i++;
}
newColor.forEach((n) => {
Math.max(Math.min(n, 1), 0);
})
// 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))
// Return the same data type as user put in.
if (Array.isArray(color)) {
@ -53,10 +58,17 @@ export class EliteMatrix {
let hex: string = '#';
newColor.forEach((n) => {
n *= 255;
n = Math.round(n);
hex += n.toString(16);
});
return hex;
}
}
/* ------------------------------------------------------------------------------ #round ---- */
#round(n: number): number {
return Math.round((n + Number.EPSILON) * 100) / 100;
}
}