
//==============================================================================
// Javascript Library (c) Virtooz 2006-2008
//
// LIBRARY: uUtils.js
//
// DESCRIPTION
// Some handy javascript functions
//
// FUNCTIONS
// byteToHex(n)                                     - Convert byte integer to hex
// hexToByte(hex)                                   - Convert hex to byte integer
// colorHexToRgb(colorHex)                          - Convert hex color to RGB composite
// colorRgbToHex(R, G, B)                           - Convert RGB values to hex color
// shiftChannel(srcChannel, dstChannel, dChannel)   - Shift channel from src to dest using delta
// shiftColor(colorHexSrc, colorHexDst, dR, dG, dB) - Shift color from source to dest using delta's
//==============================================================================

//------------------------------------------------------------------------------
// byteToHex
//
// Convert integer in the range [0, 255] to 2-digit hex
//------------------------------------------------------------------------------

function byteToHex(n) {
	var result = n.toString(16);
  result = result.toUpperCase();
	if (result.length==1) result = '0' + result;
	return result;
}

//------------------------------------------------------------------------------
// hexToByte
//
// Convert 2-digit hex to integer in the range [0, 255]
//------------------------------------------------------------------------------

function hexToByte(hex) {
	return parseInt(hex, 16); 
}

//------------------------------------------------------------------------------
// colorHexToRgb(colorHex)
//
// Convert hex color to RGB channels
//------------------------------------------------------------------------------

function colorHexToRgb(colorHex) {
  var s = new String(colorHex);
  var r = hexToByte(s.substring(0, 2));
  var g = hexToByte(s.substring(2, 4));
  var b = hexToByte(s.substring(4, 6));
  return {
    R: r,
    G: g,
    B: b
  };
}

//------------------------------------------------------------------------------
// colorRgbToHex
//
// Convert RGB values to hex color
//------------------------------------------------------------------------------

function colorRgbToHex(R, G, B) {
  return byteToHex(R) + byteToHex(G) + byteToHex(B);
}

//------------------------------------------------------------------------------
// shiftChannel
//
// Shift a byte-size color channel from source to destination using a delta.
// Clip at 0 and 255 and destination color.
//------------------------------------------------------------------------------

function shiftChannel(srcChannel, dstChannel, dChannel) {
  var newChannel = srcChannel + dChannel;
  if (dChannel >= 0) {
    if (newChannel > dstChannel) newChannel = dstChannel;
    if (newChannel > 255) newChannel = 255;
  }
  else {
    if (newChannel < dstChannel) newChannel = dstChannel;
    if (newChannel < 0) newChannel = 0;
  }
  return newChannel;
}

//------------------------------------------------------------------------------
// shiftColor
//
// Shift hex color from source to destination using channel delta's
//------------------------------------------------------------------------------

function shiftColor(colorHexSrc, colorHexDst, dR, dG, dB) {
  var srcColor = colorHexToRgb(colorHexSrc);
  var dstColor = colorHexToRgb(colorHexDst);
  var R = shiftChannel(srcColor.R, dstColor.R, dR);
  var G = shiftChannel(srcColor.G, dstColor.G, dG);
  var B = shiftChannel(srcColor.B, dstColor.B, dB);
  
  return colorRgbToHex(R, G, B);
}


