Source: wk_pixi/config/concretizePixiJS.js

/**
 * @author    Wolfgang Kowarschick <kowa@hs-augsburg.de>
 * @copyright 2017-2018
 * @license   CC-BY-NC-SA-4.0
 */

/**
 * @module wk_pixi/config/concretizePixiJS
 */

import { concretize, addToEnvironment } from '/wk/config/concretize';
import { stringToHex }                  from '/wk_pixi/util/stringToHex';

addToEnvironment('@stringToHex', p_value => stringToHex(p_value) );

/**
 * @function
 * @static
 *
 * @description
 * Computes a concrete value, if a template value is passed.
 * Otherwise the original value is returned.
 *
 * Templates are:
 * <ul>
 *   <li>
 *     An array with at least two elements
 *     whose first element is <code>"@some"</code>.
 *     For such an array some of its other elements
 *     is returned randomly.
 *   </li>
 *   <li>
 *     An object that contains two special attributes
 *     named <code>"@min"</code> and <code>"@max"</code>.
 *     The values of those attributes have to be numbers.
 *     Returned is a random value within the interval
 *     defined by <code>"@min"</code> and <code>"@max"</code>.
 *
 *     There are further attributes that affect the computation
 *     of the random value:
 *
 *     <ul>
 *       <li><code>"@integer": true/false</code>:
 *         If true, an integer value is computed within the interval
 *         [@min, @max]. If false, a float value is computed
 *         within the interval [@min, @max[.
 *       </li>
 *       <li><code>"@positive": value</code>:
 *         The value has to be a number greater or equal 0 and less or equal 1.
 *         The randomly computed number is with a probability of
 *         <code>value</code> a member of [@min, @max] or [@min, @max[ resp.
 *         and with a probability of <code>1-value</code>
 *         a member of [-@max, -@min] or [-@max, -@min[.
 *       </li>
 *       <li><code>"@relative": "name" (a string value)</code>:
 *         The computed value is multiplied with the environment value
 *         <code>p_environment[name]</code>, if that value exists.
 *       </li>
 *     </ul>
 *   </li>
 *   <li><code>{"@stringToHeX": "#AFFE00"}</code>:
 *      A three or six byte color key is converted into the corresponding
 *      integer code of that color. This is needed by PixiJS, as that library
 *      can only deal with integer color codes instead of string color codes.
 *      Hex values, such as 0xAFFE00, which are accepted by PixiJS,
 *      on the other hand, cannot be stored in JSON-Files ....
 *   </li>
 *   <li><code>"@@NAME"</code>:
 *      Two @-symbols at the beginning of a key name (including
 *      <code>"@@some"</code>) are replace by one. So, those keys are
 *      not concretized by the first invocation of this function
 *      but by the second. If the key name starts with more then
 *      two @-symbols, <code>concretize</code> must applied correspondingly often to
 *      concretize the value associated.
 *   </li>
 * </ul>
 *
 * @param {*} p_config
 *   An object that contains the attributes stated below
 *   (at least <code>config</code>) or an arbitrary value <code>v</code>
 *   which is used as an abbreviation for <code>{ config: v}</code>.
 * @param {Object} p_config.config
 *   An object which may contain a @-values that have to be replaced
 *   by concrete values.
 * @param {Object} p_config.environment
 *   An object containing base values which can be referred by @-values.
 *   If such a value is a function, it is called to compute the base
 *   value. The current element in the array and its position are passed
 *   to that function.
 * @param {Array.<String>} p_config.specials
 *   If not null, only those specials are expanded that are member of the
 *   array <code>p_specials</code>.
 * @param {?number} p_config.level
 *   The number of levels that shall be concretized recursively.
 * @param {?object} p_config.info = null
 *   (used internally only; see <code>p_environment</code>)
 * @return {*}
 *   The computed value if one has been computed,
 *   <code>p_config</code> otherwise.
 */
function concretizePixiJS(p_config)
{ return concretize(p_config); }

export {concretizePixiJS};
export default concretizePixiJS;