Сan even shorter:
string='';//random string for painting //color classifier - 6 types of non-breaking space: for(let i=120;i--;) string+=`${'\u180E\u200B\u200C\u200D\u2060\uFEFF'[6*Math.random()^0]}${'+-0'[3*Math.random()^0]}`; css = [string];//preparing an array for spread //we are returning the replaced string in css[0], using as the second argument .replace() a arrow function (which writes to a global variables): css[0] = css[0].replace(/(\u180E[\+\-0])|(\u200B[\+\-0])|(\u200C[\+\-0])|(\u200D[\+\-0])|(\u2060[\+\-0])|(\uFEFF[\+\-0])/g, (match, $1, $2, $3, $4, $5, $6) => { if ($1) {css.push('color: blue;')} if ($2) {css.push('color: red;')} if ($3) {css.push('color: green;')} if ($4) {css.push('color: yellow;')} if ($5) {css.push('color: magenta;')} if ($6) {css.push('color: cyan;')} return`%c${match}`});//so that neighboring '%c' do not overlap console.log(...css);
Here we do not use flags and explicit loops. We use only one function. You can write instructions of multiple choice for global variable css[] (like a 'switch...case') on one line:
css.push(`color: ${'blue red green yellow magenta cyan'.split(' ')[Math.log2(+('0b'+[$6, $5, $4, $3, $2, $1].map(e=>+!!e).join('')))]};`);
– in the first step array of regex capturing groups transformed at bit field, further we find log2(field), which is the position in the array of colors at RPN. So length of the function is 5 lines, not counting the first 2 lines containing 'random()' for test (which you can also write on one line using recursion:
css = [(s=>(f = (i, recursion =_=>(s+=`${'\u180E\u200B\u200C\u200D\u2060\uFEFF'[6*Math.random()^0]}${'+-0'[3*Math.random()^0]}`, f(--i)))=>!i? s :recursion())(120))('')];
).
Thus, we can use in theory max up to 6! = 720 colors, i.e. you need &h1003/6! = 23302 groups for a standard palette with a depth of 24 bits.
Open a console by browser on this page and run:
(a => (a.type = 'text/javascript', a.id = 'demo_console', a.innerHTML = `const css = [(s=>(f = (i, recursion =_=>(s+=\`$\{'\u180E\u200B\u200C\u200D\u2060\uFEFF'[6*Math.random()^0]}$\{'+-0'[3*Math.random()^0]}\`, f(--i)))=>!i? s :recursion())(120))('')]; css[0] = css[0].replace(/(\u180E[\+\-0])|(\u200B[\+\-0])|(\u200C[\+\-0])|(\u200D[\+\-0])|(\u2060[\+\-0])|(\uFEFF[\+\-0])/g, (match, $1, $2, $3, $4, $5, $6)=>\{ css.push(\`color: $\{'blue red green yellow magenta cyan'.split(' ')[Math.log2(+('0b'+[$6, $5, $4, $3, $2, $1].map(e=>+!!e).join('')))]};\`); return\`%c$\{match}\`}); console.log(...css);` ))(document.getElementsByTagName('head')[0].appendChild(document.createElement('script')))