Skip to content

Commit e149331

Browse files
janusjbamptongemini-code-assist[bot]
authored
refactor: function randomColor returns array (#691)
* refactor: function randomColor and function getContrastingPair Instead of returning string which will be subjected to number extract via regex we just return array of three numbers. We use string interpolation inside function getContrastingPair when string is needed. Change from while loop to for loop in function getContrastingPair. This function has a potential to enter an infinite loop if it consistently fails to find a pair of colors with sufficient contrast. This could freeze the user's browser. I'd suggest refactoring it to use a for loop with a maximum number of attempts and a fallback to a default high-contrast pair. This makes the code more robust and also simplifies the logic by removing the need for the valid flag. (gemini cost assist) * Update frontend/assets/scripts/script.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --------- Co-authored-by: John Bampton <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 0ac9469 commit e149331

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

frontend/assets/scripts/script.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ function getCurrentYear() {
6363
}
6464

6565
/**
66-
* Generates a random RGB color string.
67-
* Example output: "rgb(123, 45, 200)".
68-
* @returns {string} Randomly generated RGB color.
66+
* Generates random RGB color components as an array of numbers.
67+
* Example output: [123, 45, 200].
68+
* @returns {[number, number, number]} Randomly generated RGB color.
6969
*/
7070
function randomColor() {
7171
const red = Math.floor(Math.random() * 256);
7272
const green = Math.floor(Math.random() * 256);
7373
const blue = Math.floor(Math.random() * 256);
74-
return `rgb(${red}, ${green}, ${blue})`;
74+
return [red, green, blue];
7575
}
7676

7777
/**
@@ -110,27 +110,25 @@ function resetThemeOverrides() {
110110
* @returns {[string, string]} A pair of contrasting RGB colors.
111111
*/
112112
function getContrastingPair() {
113-
let color1 = randomColor();
114-
let color2 = randomColor();
115-
let contrast = 0;
116-
let valid = false;
117-
118-
while (!valid) {
119-
const [r1, g1, b1] = color1.match(/\d+/g).map(Number);
120-
const [r2, g2, b2] = color2.match(/\d+/g).map(Number);
113+
const MAX_ATTEMPTS = 100;
114+
for (let i = 0; i < MAX_ATTEMPTS; i++) {
115+
const [r1, g1, b1] = randomColor();
116+
const [r2, g2, b2] = randomColor();
117+
121118
const lum1 = luminance(r1, g1, b1);
122119
const lum2 = luminance(r2, g2, b2);
123120

124-
contrast = (Math.max(lum1, lum2) + 0.05) / (Math.min(lum1, lum2) + 0.05);
121+
const contrast = (Math.max(lum1, lum2) + 0.05) / (Math.min(lum1, lum2) + 0.05);
122+
125123
if (contrast > 4.5) {
126-
valid = true;
127-
} else {
128-
color1 = randomColor();
129-
color2 = randomColor();
124+
const color1 = `rgb(${r1}, ${g1}, ${b1})`;
125+
const color2 = `rgb(${r2}, ${g2}, ${b2})`;
126+
return [color1, color2];
130127
}
131128
}
132129

133-
return [color1, color2];
130+
// Fallback to a default high-contrast pair if no suitable pair is found.
131+
return ['rgb(255, 255, 255)', 'rgb(0, 0, 0)'];
134132
}
135133

136134
/**

0 commit comments

Comments
 (0)