Skip to content

Instantly share code, notes, and snippets.

@syntaxhacker
Last active September 19, 2022 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syntaxhacker/9f4a9236bd528570cf55548f351f3036 to your computer and use it in GitHub Desktop.
Save syntaxhacker/9f4a9236bd528570cf55548f351f3036 to your computer and use it in GitHub Desktop.
Cooolorrrsss
// generate list of random colors except blue rgb values until green is reached
let colors = [];
const numOfColors = 10;
const unDesirableColor = (color) => {
// if color is near blue or white return true
const rgb = color.split(',').map((val) => {
return parseInt(val);
}
);
const r = rgb[ 0 ];
const g = rgb[ 1 ];
const b = rgb[ 2 ];
// if color is near blue or white return true
if (r > 200 && g < 50 && b > 200) {
return true;
}
if (r > 200 && g > 200 && b > 200) {
return true;
}
// check if low constrast
if (Math.abs(r - g) < 20 && Math.abs(r - b) < 20 && Math.abs(g - b) < 20) {
return true;
}
return false;
};
const isSimilarColor = (colorsList, newColor) => {
// if newColor is similar to any color in colorsList return true
// use filter
const similarColors = colorsList.filter((color) => {
const rgb = color.replace('rgb(', '').replace(')', '').split(',');
const r = parseInt(rgb[ 0 ]);
const g = parseInt(rgb[ 1 ]);
const b = parseInt(rgb[ 2 ]);
const newRgb = newColor.replace('rgb(', '').replace(')', '').split(',');
const newR = parseInt(newRgb[ 0 ]);
const newG = parseInt(newRgb[ 1 ]);
const newB = parseInt(newRgb[ 2 ]);
if (Math.abs(r - newR) < 20 && Math.abs(g - newG) < 20 && Math.abs(b - newB) < 20) {
return true;
}
return false;
});
if (similarColors.length > 0) {
return true;
}
return false;
};
while (colors.length < numOfColors) {
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
const color = `rgb(${r}, ${g}, ${b})`;
if (unDesirableColor(color) || isSimilarColor(colors, color)) {
continue;
}
// // removeSimilarColors(colors, color);
// console.log("yeah");
colors.push(color);
};
// console.log(colors);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment