I am working on a job board with an always changing quantity of job postings. However each job posting should have a tiered shade of one base color: #219BBE
.
Here is a sketch of what I am trying to achieve:
What I need is a javascript function which changes the shade of a color depending on the number of job_box
es.
So far I found a javascript snippet for generating the shades of #219BBE
.
function calculateShades(colorValue) {
"#219BBE" = colorValue;
// break the hexadecimal color value into R, G, B one-byte components
// and parse into decimal values.
// calculate a decrement value for R, G, and B based on 10% of their
// original values.
var red = parseInt(colorValue.substr(0, 2), 16);
var redDecrement = Math.round(red*0.1);
var green = parseInt(colorValue.substr(2, 2), 16);
var greenDecrement = Math.round(green*0.1);
var blue = parseInt(colorValue.substr(4, 2), 16);
var blueDecrement = Math.round(blue*0.1);
var shadeValues = [];
var redString = null;
var greenString = null;
var blueString = null;
for (var i = 0; i < 10; i++) {
redString = red.toString(16); // convert red to hexadecimal string
redString = pad(redString, 2); // pad the string if needed
greenString = green.toString(16); // convert green to hexadecimal string
greenString = pad(greenString, 2); // pad the string if needed
blueString = blue.toString(16); // convert blue to hexadecimal string
blueString = pad(blueString, 2); // pad the string if needed
shadeValues[i] = redString + greenString + blueString;
// reduce the shade towards black
red = red - redDecrement;
if (red <= 0) {
red = 0;
}
green = green - greenDecrement;
if (green <= 0) {
green = 0;
}
blue = blue - blueDecrement;
if (blue <= 0) {
blue = 0;
}
}
shadeValues[10] = "000000";
return shadeValues;
}
I simplified the output for this problem: HTML
<!-- Instead of 4 boxes we could also have n boxes -->
<div class="job_box"></div>
<div class="job_box"></div>
<div class="job_box"></div>
<div class="job_box"></div>
CSS
.job_box {
width: 100%;
height: 50px;
/* The dynamic background-color */
background-color: #219BBE;
}
To count the quantity of job_box
es I would use jQuery:
var numBoxes = $('.job_box').length
And here is the point where I am stuck. I know that I need a loop but that's it. Can you please push me in the right direction?