💄 Rewrite utils in ES2015

This commit is contained in:
Mattéo Delabre 2015-12-12 00:38:15 +01:00
parent 684894cccd
commit e34aec407b
1 changed files with 44 additions and 41 deletions

View File

@ -1,43 +1,46 @@
/*jslint browser:true, plusplus:true */
/*globals self */
(function (self) {
'use strict'; 'use strict';
var utils = {}; /**
* 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;
// random utils /**
utils.getRandomArbitary = function (min, max) { * Generate a random color
return Math.random() * (max - min) + min; *
}; * @return {Array} RGB components
*/
export const randomColor = () => {
const color = [];
utils.getRandomInt = function (min, max) { for (let i = 0; i < 3; i++) {
return Math.floor(Math.random() * (max - min + 1)) + min; color.push(parseFloat(Math.random().toFixed(2)) * 255);
};
utils.getRandomColor = function () {
var color = [], i;
for (i = 0; i < 3; i++) {
color.push(parseFloat(
utils.getRandomArbitary(0, 1).toFixed(2)
) * 255);
} }
return color; return color;
}; };
// colors utils /**
function componentToHex(c) { * Convert a decimal number to its hexadecimal representation
var hex = parseInt(c, 10).toString(16); *
* @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; return hex.length === 1 ? '0' + hex : hex;
}
utils.rgbToHex = function (c) {
console.log(c);
return '#' + componentToHex(c[0]) + componentToHex(c[1]) + componentToHex(c[2]);
}; };
// exports /**
self.utils = utils; * Convert a RGB color to its hexadecimal representation
}(self)); *
* @param {Array} color RGB color
* @return {string} Hex representation
*/
export const rgbToHex =
color => '#' + hex(color[0]) + hex(color[1]) + hex(color[2]);