From 9456bb8505fe5fbe3b7fd184959ef970d9c5a5b9 Mon Sep 17 00:00:00 2001 From: punkfairie Date: Sun, 14 May 2023 10:33:59 -0700 Subject: [PATCH] Write code. --- configs/tsconfig.base.json | 14 +++++++++ configs/tsconfig.cjs.json | 11 +++++++ configs/tsconfig.esm.json | 11 +++++++ package-lock.json | 29 ++++++++++++++++++ package.json | 33 ++++++++++++++++++++ src/index.ts | 62 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 160 insertions(+) create mode 100644 configs/tsconfig.base.json create mode 100644 configs/tsconfig.cjs.json create mode 100644 configs/tsconfig.esm.json create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/index.ts diff --git a/configs/tsconfig.base.json b/configs/tsconfig.base.json new file mode 100644 index 0000000..7817ae7 --- /dev/null +++ b/configs/tsconfig.base.json @@ -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"] +} \ No newline at end of file diff --git a/configs/tsconfig.cjs.json b/configs/tsconfig.cjs.json new file mode 100644 index 0000000..d880254 --- /dev/null +++ b/configs/tsconfig.cjs.json @@ -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", + } +} \ No newline at end of file diff --git a/configs/tsconfig.esm.json b/configs/tsconfig.esm.json new file mode 100644 index 0000000..b8d2dbb --- /dev/null +++ b/configs/tsconfig.esm.json @@ -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" + } +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..88905ce --- /dev/null +++ b/package-lock.json @@ -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" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..43e4055 --- /dev/null +++ b/package.json @@ -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" + } + } + } +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..4cedfd6 --- /dev/null +++ b/src/index.ts @@ -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; + } + } +} \ No newline at end of file