diff --git a/lib/cjs/index.js b/lib/cjs/index.js index a614bb5..71fe171 100644 --- a/lib/cjs/index.js +++ b/lib/cjs/index.js @@ -1,8 +1,15 @@ "use strict"; +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; Object.defineProperty(exports, "__esModule", { value: true }); exports.EliteMatrix = void 0; class EliteMatrix { constructor(matrixRed, matrixGreen, matrixBlue) { + _EliteMatrix_instances.add(this); this.red = matrixRed; this.green = matrixGreen; this.blue = matrixBlue; @@ -16,9 +23,12 @@ class EliteMatrix { const green = parseInt(color.slice(3, 5), 16) / 255; const blue = parseInt(color.slice(5, 7), 16) / 255; rgb = [red, green, blue]; + // Round to 2 decimal places. + rgb = rgb.map(n => __classPrivateFieldGet(this, _EliteMatrix_instances, "m", _EliteMatrix_round).call(this, n)); } else { - rgb = color; + // Convert RGB decimal to RGB percent. + rgb = color.map(n => __classPrivateFieldGet(this, _EliteMatrix_instances, "m", _EliteMatrix_round).call(this, n / 255)); } // Apply matrix filter. let newColor = []; @@ -29,17 +39,19 @@ class EliteMatrix { this.blue[i] * rgb[2]); 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) => __classPrivateFieldGet(this, _EliteMatrix_instances, "m", _EliteMatrix_round).call(this, n)); // Return the same data type as user put in. if (Array.isArray(color)) { - return newColor; + return newColor.map(n => Math.round(n * 255)); } else { let hex = '#'; newColor.forEach((n) => { n *= 255; + n = Math.round(n); hex += n.toString(16); }); return hex; @@ -47,3 +59,6 @@ class EliteMatrix { } } exports.EliteMatrix = EliteMatrix; +_EliteMatrix_instances = new WeakSet(), _EliteMatrix_round = function _EliteMatrix_round(n) { + return Math.round((n + Number.EPSILON) * 100) / 100; +}; diff --git a/lib/cjs/types/index.d.ts b/lib/cjs/types/index.d.ts index a5bf395..17b601a 100644 --- a/lib/cjs/types/index.d.ts +++ b/lib/cjs/types/index.d.ts @@ -1,6 +1,7 @@ type matrix = [number, number, number]; type rgbColor = [number, number, number]; export declare class EliteMatrix { + #private; red: matrix; green: matrix; blue: matrix; diff --git a/lib/cjs/types/index.d.ts.map b/lib/cjs/types/index.d.ts.map index b86fa4d..8b564cc 100644 --- a/lib/cjs/types/index.d.ts.map +++ b/lib/cjs/types/index.d.ts.map @@ -1 +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 +{"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;CA0DvD"} \ No newline at end of file diff --git a/lib/esm/index.mjs b/lib/esm/index.mjs index 7603dce..c28765f 100644 --- a/lib/esm/index.mjs +++ b/lib/esm/index.mjs @@ -16,9 +16,12 @@ export class EliteMatrix { const green = parseInt(color.slice(3, 5), 16) / 255; const blue = parseInt(color.slice(5, 7), 16) / 255; rgb = [red, green, blue]; + // Round to 2 decimal places. + rgb = rgb.map(n => this.#round(n)); } else { - rgb = color; + // Convert RGB decimal to RGB percent. + rgb = color.map(n => this.#round(n / 255)); } // Apply matrix filter. let newColor = []; @@ -29,20 +32,26 @@ export class EliteMatrix { this.blue[i] * rgb[2]); 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)) { - return newColor; + return newColor.map(n => Math.round(n * 255)); } else { let hex = '#'; newColor.forEach((n) => { n *= 255; + n = Math.round(n); hex += n.toString(16); }); return hex; } } + /* ------------------------------------------------------------------------------ #round ---- */ + #round(n) { + return Math.round((n + Number.EPSILON) * 100) / 100; + } } diff --git a/lib/esm/types/index.d.ts b/lib/esm/types/index.d.ts index a5bf395..17b601a 100644 --- a/lib/esm/types/index.d.ts +++ b/lib/esm/types/index.d.ts @@ -1,6 +1,7 @@ type matrix = [number, number, number]; type rgbColor = [number, number, number]; export declare class EliteMatrix { + #private; red: matrix; green: matrix; blue: matrix; diff --git a/lib/esm/types/index.d.ts.map b/lib/esm/types/index.d.ts.map index b86fa4d..8b564cc 100644 --- a/lib/esm/types/index.d.ts.map +++ b/lib/esm/types/index.d.ts.map @@ -1 +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 +{"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;CA0DvD"} \ No newline at end of file diff --git a/package.json b/package.json index 43e4055..09b1ce5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "elite-matrix", - "version": "1.0.0", + "version": "1.0.1", "description": "A small library for working with the Elite Dangerous color matrix.", "types": "./lib/cjs/types/index.d.ts", "main": "./lib/cjs/index.js",