chaos/scripts/utils.js

47 lines
1.0 KiB
JavaScript
Raw Normal View History

2015-12-11 23:38:15 +00:00
'use strict';
2013-12-15 21:45:16 +00:00
2015-12-11 23:38:15 +00:00
/**
* Get a random whole number
*
* @param {number} min Minimal value for the number
* @param {number} max Maximal value for the number (excluded)
* @return {number} Random number
*/
export const randomNumber = (min, max) =>
Math.floor(Math.random() * (max - min)) + min;
/**
* Generate a random color
*
* @return {Array} RGB components
*/
export const randomColor = () => {
const color = [];
for (let i = 0; i < 3; i++) {
2015-12-16 22:05:41 +00:00
color.push(Math.round((Math.random().toFixed(2)) * 255));
2013-12-15 21:45:16 +00:00
}
2015-12-11 23:38:15 +00:00
return color;
};
/**
* Convert a decimal number to its hexadecimal representation
*
* @param {number} input Number to be converted
* @return {string} Number representation
*/
const hex = input => {
let hex = parseInt(input, 10).toString(16);
return hex.length === 1 ? '0' + hex : hex;
};
/**
* Convert a RGB color to its hexadecimal representation
*
* @param {Array} color RGB color
* @return {string} Hex representation
*/
export const rgbToHex =
color => '#' + hex(color[0]) + hex(color[1]) + hex(color[2]);