I have a functionallity which allow my users to choose a theme color via range selector. The code is a mix of an answer to a previous question and my own. This is working great but it displays above the selector a text tag showing this , the value in degrees changes as the selector changes position. It's fine for me as a developer, but for the users it's a nonesense value, so I did comment that in the code. But I was thinking, why not to show the color names instead because it will be useful to users. So I started on my efforts but got stuck (once more).
This is my working code:
// Theme Color Chooser
//const currentThemec = localStorage.getItem('themeColor');
const value = document.getElementById('value');
const body = document.getElementById('usrhue');
/*if (currentThemec) {
const themeColor = currentThemec;
body.style.filter = currentThemec;
value.textContent = currentThemec;
}*/
document.getElementById('hue-rotate').oninput = function() {
const filter = 'hue-rotate(xdeg)'.replace('x', this.value);
value.textContent = filter;
body.style.filter = filter;
//localStorage.setItem( 'themeColor', body.style.filter );
}
body {
color: #ff0000;
background: #f2f2f2;
padding: 20px;
}
a {
color: #0033cc;
}
p {
}
h1 {
color: #0033cc;
}
svg {
fill: #0000cc;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body id="usrhue">
<h3>Theme Color</h1>
<pre><h3><code id="value">hue-rotate(0deg)</code></h3></pre>
<input id="hue-rotate" step="45" type="range" min="0" max="315" value="0">
<div>
<div class="tile">navbar</div>
<svg class="icon" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" x="0" y="0" width="50" height="33" viewbox="0 0 50 33"><path style="fill-opacity:1;fill-rule:evenodd;stroke:none" d="m 50,16.111089 c -0.0023,-0.52719 -0.269301,-1.175411 -0.836045,-1.837787 L 33.538651,0 l 0,4.099986 10.560199,12.011103 -10.560199,12.011102 0,4.099986 15.625304,-14.273302 C 49.730699,17.286499 49.997802,16.638278 50,16.111089 Z m -50,0 c 0.0022,-0.52719 0.2693022,-1.175411 0.8360452,-1.837787 L 16.46135,0 l 0,4.099986 -10.5601995,12.011103 10.5601995,12.011102 0,4.099986 L 0.8360452,17.948875 C 0.2693022,17.286499 0.0021979,16.638278 0,16.111089 Z"/></svg>
</div>
</body>
</html>
What I need is to replace this with this
using the colors i listed above. This colors are the nav bar colors of my app on each hue-rotate position, so, it is fixed. They will always be that colors asociated to those degrees and to those color names.
I have tried to fix my code after @ЖнецЪ answer and it is working fine. I will leave it here for future seekers.
// Theme Color Chooser
const currentThemec = localStorage.getItem('themeColor');
const value = document.getElementById('value');
const body = document.getElementById('usrhue');
if (currentThemec) {
const themeColor = currentThemec;
body.style.filter = currentThemec;
value.textContent = 'Color';
}
document.getElementById('hue-rotate').oninput = function() {
const filter = 'hue-rotate(xdeg)'.replace('x', this.value);
value.textContent = 'Color Tomato';
body.style.filter = filter;
localStorage.setItem( 'themeColor', body.style.filter );
switch (this.value) {
case '45':
return (value.textContent = 'Color Wood');
case '90':
return (value.textContent = 'Color Green House');
case '135':
return (value.textContent = 'Color Green Gross');
case '180':
return (value.textContent = 'Color Fun Green');
case '225':
return (value.textContent = 'Color Sky Blue');
case '270':
return (value.textContent = 'Color Vivid Violet');
case '315':
return (value.textContent = 'Color Rose & Guns');
default:
return (value.textContent = 'Color Tomato');
}
}