/**
* @author Wolfgang Kowarschick <kowa@hs-augsburg.de>
* @copyright 2017 - 2018
* @license CC-BY-NC-SA-4.0
*/
/**
* @module wk_pixi/loader/stringToHex
*/
/**
* @function stringToHex
* @static
*
* * As JSON doesn't support the hex notation of numbers,
* colors are usually stored as strings within JSON files.
* On the other hand, in pixi.js all colors are represented by numbers.
* So, color strings must be converted into numbers before they
* can be used by pixi.js.
*
* Should be improved by something like http://chir.ag/projects/ntc/ntc.js
* in order to be able to deal with color names, too.
*
* @param {string|number} p_value
* a color string (like <code>"#aaff02"</code>)
* or an integer value representing a color
* @return {number}
* the color value as an integer value
*/
function stringToHex(p_value)
{ if (!(typeof p_value === 'string' || (p_value instanceof String)))
{ return p_value; }
if (!/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/.test(p_value))
{ return 0; }
let r, g, b;
if (p_value.length === 4)
{ r = p_value[1]+p_value[1];
g = p_value[2]+p_value[2];
b = p_value[3]+p_value[3];
}
else
{ r = p_value.substr(1,2);
g = p_value.substr(3,2);
b = p_value.substr(5,2);
}
return ((parseInt(r,16) << 16) + (parseInt(g,16) << 8) + parseInt(b,16)) || 0;
}
export {stringToHex};
export default stringToHex;