// separate and convert hex RGB to decimal values
// bgcolor is in form of "#FFFFFF", so first char 
// must be skipped
// Analysis

var oldBG = document.bgColor;

var red = oldBG.charAt(1).toUpperCase() + oldBG.charAt(2).toUpperCase();
var green = oldBG.charAt(3).toUpperCase() + oldBG.charAt(4).toUpperCase();
var blue = oldBG.charAt(5).toUpperCase() + oldBG.charAt(6).toUpperCase();

This section of code assigns the current background colour to the variable oldBG. Three new variables are then created, red, green and blue. For each of these, the corresponding RGB value is extracted from the oldBG string using the charAt method. For example, assuming that oldBG has the value of "#ABCDEF", the first line extracts the character at position 1 ('A' -- strings are indexed beginning at position 0), and then concatenates it with the character at position 2 ('B'). This value is then assigned to the variable red ("AB").

The process is repeated for the colours green and blue.

Close Window