From 5d2c5c37c3c3c6dc37adfac191dbf0d834619b5c Mon Sep 17 00:00:00 2001 From: punkfairie Date: Sun, 14 May 2023 12:47:55 -0700 Subject: [PATCH] Fixes for RGB handling; finish README. --- README.md | 15 ++++++++++++++- src/index.ts | 9 +++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9713801..c9d8e33 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,19 @@ const matrix = new EliteMatrix(matrixRed, matrixGreen, matrixBlue); ``` From there you can use the `EliteMatrix` object to apply color filters. `filterColor` accepts either -a string hex color, or an RGB array. It will return in a matching format. +a string hex color (with or without the '#'), or an RGB array. It will return in a matching format, +and will include the '#' for a hex color. +```javascript +matrix.filterColor('#F5A804'); +// -> '#ffbae3' + +matrix.filterColor('F5A804'); +// -> '#ffbae3' + +matrix.filterColor([96, 66, 2]); +// -> [255, 186, 227] +// Which, by the way, can be passed directly to CSS rgb() via template literals: +document.getElementById('elem').style.color = `rgb(${[255, 186, 227]})`; +``` [ed-scout]: https://github.com/joncage/ed-scout/blob/master/EDScoutWebUI/HudColourAdjuster.py#L37 \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 1fd3d2b..4746d36 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,10 +26,11 @@ export class EliteMatrix { rgb = [red, green, blue]; // Round to 2 decimal places. - rgb = (rgb.map((n) => this.#round(n)) as rgbColor); + rgb = (rgb.map(n => this.#round(n)) as rgbColor); } else { - rgb = color; + // Convert RGB decimal to RGB percent. + rgb = (color.map(n => this.#round(n / 255)) as rgbColor); } // Apply matrix filter. @@ -48,11 +49,11 @@ export class EliteMatrix { newColor = newColor.map((n) => Math.max(Math.min(n, 1), 0)); // Round again. - newColor = newColor.map((n) => this.#round(n)) + newColor = newColor.map((n) => this.#round(n)); // Return the same data type as user put in. if (Array.isArray(color)) { - return (newColor as rgbColor); + return (newColor.map(n => Math.round(n * 255)) as rgbColor); } else { let hex: string = '#';