In the dotime()
function we need to add a couple of things. First we need to check the radio buttons with the id "colorWeighting", and save which radio button that is checked in a variable "colorQuantization". This is done in the exactly same way as we did when we selected different color shades in step 7b.
Then we need to call a new function colorName()
, and we need to include two things. First we need to include the RGB values as an array [rgbHour,rgbMinute,rgbSecond]
, and secondly we need to include the type of distance measuring, that is variable colorQuantization
. Then we use the replace method to remove spaces in the color name because the web browser do not understand color names with spaces in. Then we use jQuery to update the color name and the background color of the rectangle on the screen.
The last thing we need to do is to write the colorName()
function. In this function we declare two variables comparisonValue
and namePosition
. These variables will be used to compare the smallest measured distance, and which entry number in the arrays that corresponded to the measured distance. There are also two long arrays, one with all the 140 color names (like: var colorNames = ["White","Snow", ...
), and one with 140 arrays with the RGB values (like: var rgbValues = [[255,255,255],[255,250,250], ...
). I'm not going to present these long arrays in this text, but they can easily be copied from the JavaScript file.
To compare all RGB values in the rgbValues
array with the current RGB value used as the background color, we need to write a for loop similar to the one in step 7b. The script uses an if statement to check the value of variable colorQuantization
and then use either Manhattan, Euclidean, or Weighted Euclidean distance measure to compare the values. Even if these calculate the distance different, there is not a big difference between these comparisons. The reason for this is probably because of the rather small data set of 140 colors. The formulas for these comparisons looks like this:
In the above code strip we used the Math
object again to perform mathematical tasks. In this code we use abs
to get absolute numbers (only positive numbers). We also used sqrt
to get the square root of a number, as well as pow
which gives the value of a number to be the power of 2 (in this case).
With if (!comparisonValue)
the JavaScript checks wether the variable is empty or not. If the variable is empty it is the first turn in the for loop and the first measurement value should be stored in the variable. Then if the measurement value is less than the variable comparisonValue
, the measurement value should be stored in comparisonValue
. The turn i
in the for loop is then also stored in the variable namePosition
.
Nearly done. The JavaScript just needs to return the color name on position namePosition
to the dotime()
function which in turn will update the information in the HTML element.
The color clock is now finished.
There might still be opportunities for improvements. Feel free to continue and evolve this color clock.