One strategy to achieve that result could be to control the color value using the HSL
model and definining the map of colors (weight
+colorname
= color to set the css background
property) as css classes addressing the color weight
by setting the lightness
and the color name
setting the hue
and saturation
.
https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hsl
In this demo I have the weight
category of classes defined starting with lightness-*
and the color name category of classes defined as color-*
.
In this way you can define the color map using a css asset instead of dealing with js configuration.
Each of those classes just control the css custom variables: --hue
, --saturation
, --lightness
Those variable are used by the master css rule assigned to the target element that you wish to style with the given color as background:
.custom-background-color {
background: hsl(var(--hue) var(--saturation) var(--lightness));
}
Notes: The demo expects a target object to be the subject of the styling but since we had that custom-background-color
class it could be a criteria to deduce which objects are supposed to be styled like that without having to pass a specific element fetched in advance
Then the javascript code just adds a change
event listener on both the dropdowns controlling respectively the color weight
and name
by adding the respective classes to the target element after any were first unset.
The name of the weight
and colorname
classes to add to the element are a function of the values coming from the dropdown.
const colorbox = document.getElementById('colorbox');
const colorName = document.getElementById('color_name');
const colorWeight = document.getElementById('color_weight');
colorName.addEventListener('change', onColorPicked);
colorWeight.addEventListener('change', onColorPicked);
onColorPicked();
function onColorPicked(){
clearColor(colorbox);
setColor(colorbox, colorName.value, colorWeight.value);
}
//sets the background color for target as name, weight
function setColor(target, name, weight) {
target.classList.add(`color-${name}`, `lightness-${weight}`);
}
//removes the class color-* and lightness-* if any
function clearColor(target){
[...target.classList].forEach( className => {
if (
className.startsWith('color-') ||
className.startsWith('lightness-')) {
target.classList.remove(className);
}
});
}
*{
box-sizing: border-box;
}
body{
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 15% 70%;
gap: 1em;
padding: .5em;
height: 95vh;
}
select{
cursor: pointer;
}
#colorbox {
padding: 10px;
width: 100%;
margin-top: .5em;
grid-column: span 2;
height: 100%;
}
.custom-background-color {
--hue: 100;
--saturation: 100%;
--lightness: 100%;
background: hsl(var(--hue) var(--saturation) var(--lightness));
}
/* lightness */
.lightness-dark {
--lightness: 10% !important;
}
.lightness-medium {
--lightness: 30% !important;
}
.lightness-light {
--lightness: 50% !important;
}
.lightness-pale {
--lightness: 70% !important;
}
/* colors */
.color-grey {
--hue: 0;
--saturation: 0%;
}
.color-purple {
--hue: 270;
--saturation: 100%;
}
.color-blue {
--hue: 240;
--saturation: 100%;
}
.color-green {
--hue: 120;
--saturation: 100%;
}
.color-brown {
--hue: 30;
--saturation: 50%;
}
.color-orange {
--hue: 30;
--saturation: 100%;
}
.color-yellow {
--hue: 60;
--saturation: 100%;
}
.color-red {
--hue: 0;
--saturation: 100%;
}
<select id="color_weight">
<option value="dark">Dark</option>
<option value="medium">Medium</option>
<option value="light">Light</option>
<option value="pale" selected>Pale</option>
</select>
<select id="color_name">
<option value="grey">Grey</option>
<option value="purple">Purple</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="brown">Brown</option>
<option value="orange">Orange</option>
<option value="yellow">Yellow</option>
<option value="red" selected>Red</option>
</select>
<div id="colorbox" class="custom-background-color">
</div>