(rgbToHex)

//// FIGLEAF SOFTWARE RGB to Hex
Math.rgbToHex = function(r,g,b){
        return(r<<16 | g<<8 | b);}

Purpose:
Converts three separate integers representing the rgb values of a color to a hexadecimal representation of the same color.

Explanation:
Any one color is composed of 3 color components; red, green and blue. Each color component can hold a value from 0 to 255. When working with color, a component's value can generally be written 3 different ways. For example's sake, let's assume we are working with an ORANGE color with the values listed in the example below. The green component of our orange color is at 50% of its maximum brightness. In a base ten system ORANGE's green component is written as 128 (half of 256). Very simple. In a base 16 number system 128 is written as 80 which means 8 sets of 16 which is, you guessed it, 128. The third way to write 128 is to use binary (base 2 system). In binary the number 128 is represented as 10000000 which is another way of saying 1 times 2 to the 7th power. Where this is all heading..

Example:

Format =      Red :    Green :     Blue
Base10 ORANGE =      255 :      128 :      000
Base16 ORANGE =       FF :       80 :       00
Base2  ORANGE = 11111111 : 10000000 : 00000000

Let's say we want to convert our three ORANGE color components (Red=255, Green=128, Blue=0) to one hexadecimal (base 16) value (0xFF8000). The "rgbToHex" function first converts all the RGB values to binary numbers. This happens automatically whenever the bitwise << operator is used. Since our ORANGE color has a red value of 255 the red component is converted to 11111111 binary. The green value of 128 is converted to 10000000 binary and the blue value of 0 is essentially unchanged or converted to 00000000. That gives us {r:11111111, g:10000000, b:00000000}. Now that we have the binary equivalents of our RGB values we can start manipulating them. The R<<16 part of the function shifts the red value 16 places to the left and fills in the blank spaces with zeros giving us 111111110000000000000000. The G<<8 part of the function shifts the green value 8 places to the left 1000000000000000. The blue value does not shift (there's no << after the b). The "|" operator then tallies the column of numbers, similar to adding them but not quite the same. If any column of numbers has a 1 in it the result for that column is a 1.

111111110000000000000000
        1000000000000000
                00000000
+_______________________
111111111000000000000000

The resulting binary number 111111111000000000000000 is equivalent to the hexadecimal number 0xFF8000 and the base 10 number 16744448.

And that's how you get from RGB notation to a hexadecimal color value.

Are we having fun yet? Good, I knew we were.

(hexToRGB)

// PiXELWiT REVERSE ENGINEERING
Math.hexToRGB = function(hex){
        var red = hex>>16;
        var grnBlu = hex-(red<<16);
        var grn = grnBlu>>8;
        var blu = grnBlu-(grn<<8);
        return({r:red, g:grn, b:blu});
}

Purpose:
Breaks a hexadecimal representation of a color into its three color components.

Explanation:

Math.hexToRGB = function(hex){

In keeping with our ORANGE example lets say we pass our hexToRGB function the hexadecimal color 0xFF8000.

var red = hex>>16;

This line first converts our hexadecimal 0xFF8000 to binary 111111111000000000000000 by applying the >> operator then it removes the last 16 digits leaving 11111111.

var grnBlu = hex-(red<<16);

This adds 16 zeros to the end of red then subtracts that value from hex (111111110000000000000000-111111111000000000000000) leaving grnBlu with a value of 1000000000000000.

var grn = grnBlu>>8;

The green value is extracted from grnBlu by chopping the last 8 digits off grnBlu which leaves green with a value of 10000000.

var blu = grnBlu-(grn<<8);

This adds 8 zeros to the end of grn and subtracts grn from grnBlu (1000000000000000-1000000000000000) leaving blu with a value of 0 or 00000000.

return({r:red, g:grn, b:blu});

The final line returns a generic object with three properties called r, g, and b which hold their respective numeric color values.