Write code.
This commit is contained in:
parent
89312ee284
commit
9456bb8505
6 changed files with 160 additions and 0 deletions
14
configs/tsconfig.base.json
Normal file
14
configs/tsconfig.base.json
Normal file
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"skipLibCheck": true,
|
||||
"checkJs": true,
|
||||
"allowJs": true,
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
},
|
||||
"files": ["../src/index.ts"]
|
||||
}
|
11
configs/tsconfig.cjs.json
Normal file
11
configs/tsconfig.cjs.json
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"extends": "./tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"lib": ["ES6", "DOM"],
|
||||
"target": "ES6",
|
||||
"module": "CommonJS",
|
||||
"moduleResolution": "node",
|
||||
"outDir": "../lib/cjs",
|
||||
"declarationDir": "../lib/cjs/types",
|
||||
}
|
||||
}
|
11
configs/tsconfig.esm.json
Normal file
11
configs/tsconfig.esm.json
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"extends": "./tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"lib": ["ES2022", "DOM"],
|
||||
"target": "ES2022",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "nodenext",
|
||||
"outDir": "../lib/esm",
|
||||
"declarationDir": "../lib/esm/types"
|
||||
}
|
||||
}
|
29
package-lock.json
generated
Normal file
29
package-lock.json
generated
Normal file
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"name": "elite-matrix",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "elite-matrix",
|
||||
"version": "1.0.0",
|
||||
"license": "GPL-3.0-only",
|
||||
"devDependencies": {
|
||||
"typescript": "^5.0.4"
|
||||
}
|
||||
},
|
||||
"node_modules/typescript": {
|
||||
"version": "5.0.4",
|
||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.0.4.tgz",
|
||||
"integrity": "sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw==",
|
||||
"dev": true,
|
||||
"bin": {
|
||||
"tsc": "bin/tsc",
|
||||
"tsserver": "bin/tsserver"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12.20"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
33
package.json
Normal file
33
package.json
Normal file
|
@ -0,0 +1,33 @@
|
|||
{
|
||||
"name": "elite-matrix",
|
||||
"version": "1.0.0",
|
||||
"description": "A small library for working with the Elite Dangerous color matrix.",
|
||||
"types": "./lib/cjs/types/index.d.ts",
|
||||
"main": "./lib/cjs/index.js",
|
||||
"scripts": {
|
||||
"clean": "rm -rf ./lib",
|
||||
"build": "npm run clean && npm run build:esm && npm run build:cjs",
|
||||
"build:esm": "tsc -p ./configs/tsconfig.esm.json && mv lib/esm/index.js lib/esm/index.mjs",
|
||||
"build:cjs": "tsc -p ./configs/tsconfig.cjs.json",
|
||||
"prepack": "npm run build"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "CMDR Punkfairie",
|
||||
"license": "GPL-3.0-only",
|
||||
"devDependencies": {
|
||||
"typescript": "^5.0.4"
|
||||
},
|
||||
"files": ["lib/**/*"],
|
||||
"exports": {
|
||||
".": {
|
||||
"import": {
|
||||
"types": "./lib/esm/types/index.d.ts",
|
||||
"default": "./lib/esm/index.mjs"
|
||||
},
|
||||
"require": {
|
||||
"types": "./lib/cjs/types/index.d.ts",
|
||||
"default": "./lib/cjs/index.js"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
62
src/index.ts
Normal file
62
src/index.ts
Normal file
|
@ -0,0 +1,62 @@
|
|||
type matrix = [number, number, number];
|
||||
type rgbColor = [number, number, number];
|
||||
|
||||
export class EliteMatrix {
|
||||
red: matrix;
|
||||
green: matrix;
|
||||
blue: matrix;
|
||||
|
||||
constructor(matrixRed: matrix, matrixGreen: matrix, matrixBlue: matrix) {
|
||||
this.red = matrixRed;
|
||||
this.green = matrixGreen;
|
||||
this.blue = matrixBlue;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- filterColor ---- */
|
||||
|
||||
filterColor(color: rgbColor|string): rgbColor|string {
|
||||
let rgb: rgbColor;
|
||||
|
||||
// Convert hex to RGB.
|
||||
if (typeof color === 'string') {
|
||||
const red: number = parseInt(color.slice(1, 3), 16) / 255;
|
||||
const green: number = parseInt(color.slice(3, 5), 16) / 255;
|
||||
const blue: number = parseInt(color.slice(5, 7), 16) / 255;
|
||||
|
||||
rgb = [red, green, blue];
|
||||
|
||||
} else {
|
||||
rgb = color;
|
||||
}
|
||||
|
||||
// Apply matrix filter.
|
||||
let newColor: number[] = [];
|
||||
let i: number = 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 as rgbColor);
|
||||
|
||||
} else {
|
||||
let hex: string = '#';
|
||||
newColor.forEach((n) => {
|
||||
n *= 255;
|
||||
hex += n.toString(16);
|
||||
});
|
||||
|
||||
return hex;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue