Skip to content

Commit

Permalink
[#1936] Migrate random-color-gen.js to typescript (#2043)
Browse files Browse the repository at this point in the history
Currently, there is still some JavaScript code which remains unmigrated.
This allows for type unsafe code to be written, potentially resulting in
unintended behavior.

Let's migrate random-color-generator.js JavaScript code to TypeScript
code to facilitate future changes to the code.
  • Loading branch information
jq1836 committed Oct 4, 2023
1 parent d4e2272 commit a8c3f00
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
11 changes: 11 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@fortawesome/free-solid-svg-icons": "^6.0.0",
"@fortawesome/vue-fontawesome": "^3.0.3",
"@types/minimatch": "^5.1.2",
"@types/seedrandom": "^3.0.5",
"core-js": "^3.6.5",
"highlight.js": "^10.5.0",
"jszip": "^3.5.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function getRandomHex() {
return `#${Math.round(randomGenerator() * maxHexColorValue + 1).toString(16).padStart(6, '0')}`;
}

function rgb2lab(rgb) {
function rgb2lab(rgb: number[]) {
let r = rgb[0] / 255;
let g = rgb[1] / 255;
let b = rgb[2] / 255;
Expand All @@ -29,7 +29,7 @@ function rgb2lab(rgb) {

// this delta E (perceptual color distance) implementation taken from @antimatter15 from
// github: https://github.com/antimatter15/rgb-lab
function deltaE(rgbA, rgbB) {
function deltaE(rgbA: number[], rgbB: number[]) {
const labA = rgb2lab(rgbA);
const labB = rgb2lab(rgbB);
const deltaL = labA[0] - labB[0];
Expand All @@ -49,7 +49,7 @@ function deltaE(rgbA, rgbB) {
return distance < 0 ? 0 : Math.sqrt(distance);
}

function hasSimilarExistingColors(existingColors, newHex) {
function hasSimilarExistingColors(existingColors: string[], newHex: string) {
const deltaEThreshold = 11;
// the lower limit of delta E to be similar, more info at http://zschuessler.github.io/DeltaE/learn/
return existingColors.some((existingHex) => {
Expand All @@ -59,7 +59,7 @@ function hasSimilarExistingColors(existingColors, newHex) {
});
}

function getNonRepeatingColor(existingColors) {
function getNonRepeatingColor(existingColors: string[]) {
let generatedHex = getRandomHex();
while (hasSimilarExistingColors(existingColors, generatedHex)) {
generatedHex = getRandomHex();
Expand Down

0 comments on commit a8c3f00

Please sign in to comment.