diff --git a/lib/cjs/index.js b/lib/cjs/index.js new file mode 100644 index 0000000..a614bb5 --- /dev/null +++ b/lib/cjs/index.js @@ -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; diff --git a/lib/cjs/types/index.d.ts b/lib/cjs/types/index.d.ts new file mode 100644 index 0000000..a5bf395 --- /dev/null +++ b/lib/cjs/types/index.d.ts @@ -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 \ No newline at end of file diff --git a/lib/cjs/types/index.d.ts.map b/lib/cjs/types/index.d.ts.map new file mode 100644 index 0000000..b86fa4d --- /dev/null +++ b/lib/cjs/types/index.d.ts.map @@ -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"} \ No newline at end of file diff --git a/lib/esm/index.mjs b/lib/esm/index.mjs new file mode 100644 index 0000000..7603dce --- /dev/null +++ b/lib/esm/index.mjs @@ -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; + } + } +} diff --git a/lib/esm/types/index.d.ts b/lib/esm/types/index.d.ts new file mode 100644 index 0000000..a5bf395 --- /dev/null +++ b/lib/esm/types/index.d.ts @@ -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 \ No newline at end of file diff --git a/lib/esm/types/index.d.ts.map b/lib/esm/types/index.d.ts.map new file mode 100644 index 0000000..b86fa4d --- /dev/null +++ b/lib/esm/types/index.d.ts.map @@ -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"} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 4cedfd6..1fd3d2b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; + } } \ No newline at end of file