As I work on adding tags to my website for blog posts, I have a specific vision in mind. Each tag should be assigned a unique background color selected from a predefined array of theme colors.
My goal is to assign the same background color to tags with identical string values. For example, a tag labeled "educational" should share the same background color as another tag with the label "educational", but may differ from a tag with the label "technology".
Given the following theme:
const COLORS = ['pink', 'purple', 'indigo', 'blue', 'orange', 'red', 'yellow', 'black', 'whitesmoke']
I am seeking guidance on how to develop a function that can hash the string (rather than using just string length and Math.random) to randomly select a color from the COLORS
array by calling a simple function like stringToColor(tagName)
.
While I understand that there might be instances where tags end up having the same color, I am aiming for randomness as much as possible.