GpsGate Server JavaScript API

Algorithm  1.0.0

GpsGate Server JavaScript API > Algorithm > color.js (source view)
Search:
 
Filters
/**
 *
 * @module Util
 *
 */

/**
 * namespace <br />
 * Color handling utilities.<br />
 * for more Color handling functionality you should check <a href="http://mochikit.com/doc/html/MochiKit/Color.html">MochiKit.Color</a>, <a href="....">dojo.Color</a> and <a href="...">dojo.dojox.Color</a>
 * @class Franson.Color
 * @static
 */
Franson.Color = Franson.Color || {}; // hmm, or reserve Franson.Color for an actual Color class instead?

/**
 * @method parseHexColor
 * @param {string} strHex. allowed formats: [ '#RRGGBB', 'RRGGBB', '#RGB', 'RGB' ]
 * @return {(r, g, b)} or null if failed
 */
Franson.Color.parseHexColor = function(strHex) // parseHexString?
{
	if (strHex.charAt(0) == '#')
		strHex = strHex.substr(1);

	// todo: or should we detect NaNs and return null?
	if (strHex.length == 6)
	{
		var r = parseInt(strHex.substr(0, 2), 16);
		var g = parseInt(strHex.substr(2, 2), 16);
		var b = parseInt(strHex.substr(4, 2), 16);
		return { 'r': r, 'g': g, 'b': b };
	}
	else if (strHex.length == 3)
	{
		var r = strHex.substr(0, 1);
		r = parseInt(r + r, 16);
		var g = strHex.substr(1, 1);
		g = parseInt(g + g, 16);
		var b = strHex.substr(2, 1);
		b = parseInt(b + b, 16);
		return { 'r': r, 'g': g, 'b': b };
	}
	return null;
};

Copyright © 2009 Franson Technology AB, Sweden. All rights reserved.