Start off the bookmarklet by initializing four variables.
The variable x
is set to the valid hexadecimal values.
This line prompts the user for the requested RGB values, separated by commas, as shown in Figure 17.18
Figure 17.18. The first part of the script prompts the user for the RGB values.
If the user entered anything, continue with the code. Otherwise, the value of c
will be null, and the bookmarklet ends, as in step 10, below.
Split the entry in c
, separated by commas, and put the result into the s
Loop around the following lines once for each of the three red, green, and blue color values.
Turn the current element of s
into a number, and save it as n
This line converts n
into 2 hexadecimal digits and adds the result to h
The result (preceded by a pound sign, ready to be copied into an HTML page) is displayed via a prompt command, as shown in Figure 17.19
. It's done this way instead of with an alert, so that we can copy the code and paste it later.
Figure 17.19. Another prompt box provides the calculated hex value.
If nothing was entered in step 3, step 4 causes the code to jump to this step, where nothing happens.