chaos/scripts/utils.js

43 lines
864 B
JavaScript
Raw Normal View History

2013-12-15 21:45:16 +00:00
/*jslint browser:true, plusplus:true */
/*globals self */
(function (self) {
'use strict';
var utils = {};
// random utils
utils.getRandomArbitary = function (min, max) {
return Math.random() * (max - min) + min;
};
utils.getRandomInt = function (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
utils.getRandomColor = function () {
var color = [], i;
for (i = 0; i < 3; i++) {
color.push(parseFloat(
utils.getRandomArbitary(0, 1).toFixed(2)
) * 255);
}
return color;
};
// colors utils
function componentToHex(c) {
var hex = parseInt(c, 10).toString(16);
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;
}(self));