Fixes for RGB handling; finish README.

This commit is contained in:
punkfairie 2023-05-14 12:47:55 -07:00
parent 660096ccc4
commit 5d2c5c37c3
2 changed files with 19 additions and 5 deletions

View file

@ -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 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 [ed-scout]: https://github.com/joncage/ed-scout/blob/master/EDScoutWebUI/HudColourAdjuster.py#L37

View file

@ -26,10 +26,11 @@ export class EliteMatrix {
rgb = [red, green, blue]; rgb = [red, green, blue];
// Round to 2 decimal places. // Round to 2 decimal places.
rgb = (rgb.map((n) => this.#round(n)) as rgbColor); rgb = (rgb.map(n => this.#round(n)) as rgbColor);
} else { } else {
rgb = color; // Convert RGB decimal to RGB percent.
rgb = (color.map(n => this.#round(n / 255)) as rgbColor);
} }
// Apply matrix filter. // Apply matrix filter.
@ -48,11 +49,11 @@ export class EliteMatrix {
newColor = newColor.map((n) => Math.max(Math.min(n, 1), 0)); newColor = newColor.map((n) => Math.max(Math.min(n, 1), 0));
// Round again. // 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. // Return the same data type as user put in.
if (Array.isArray(color)) { if (Array.isArray(color)) {
return (newColor as rgbColor); return (newColor.map(n => Math.round(n * 255)) as rgbColor);
} else { } else {
let hex: string = '#'; let hex: string = '#';