From 6583d3d560ab8ad1950171976fe448107a20d43f Mon Sep 17 00:00:00 2001 From: juliandescottes Date: Wed, 24 Sep 2014 21:53:41 +0200 Subject: [PATCH] moved rgbToHex correct implementation to pskl core utils --- src/js/utils/FrameUtils.js | 23 +---------------------- src/js/utils/core.js | 23 ++++++++++++++++++----- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/src/js/utils/FrameUtils.js b/src/js/utils/FrameUtils.js index 273a08fb..ae71a1c9 100644 --- a/src/js/utils/FrameUtils.js +++ b/src/js/utils/FrameUtils.js @@ -68,34 +68,13 @@ if (a < 125) { grid[x][y] = Constants.TRANSPARENT_COLOR; } else { - grid[x][y] = pskl.utils.FrameUtils.rgbToHex_(r,g,b); + grid[x][y] = pskl.utils.rgbToHex(r,g,b); } } } return pskl.model.Frame.fromPixelGrid(grid); }, - /** - * Convert a rgb(Number, Number, Number) color to hexadecimal representation - * @param {Number} r red value, between 0 and 255 - * @param {Number} g green value, between 0 and 255 - * @param {Number} b blue value, between 0 and 255 - * @return {String} hex representation of the color '#ABCDEF' - */ - rgbToHex_ : function (r, g, b) { - return "#" + this.componentToHex_(r) + this.componentToHex_(g) + this.componentToHex_(b); - }, - - /** - * Convert a color component (as a Number between 0 and 255) to its string hexa representation - * @param {Number} c component value, between 0 and 255 - * @return {String} eg. '0A' - */ - componentToHex_ : function (c) { - var hex = c.toString(16); - return hex.length == 1 ? "0" + hex : hex; - }, - /** * Alpha compositing using porter duff algorithm : * http://en.wikipedia.org/wiki/Alpha_compositing diff --git a/src/js/utils/core.js b/src/js/utils/core.js index 59cb7e45..2fd92c14 100644 --- a/src/js/utils/core.js +++ b/src/js/utils/core.js @@ -46,12 +46,25 @@ if (!Function.prototype.bind) { var ns = $.namespace("pskl.utils"); - ns.rgbToHex = function(r, g, b) { - if (r > 255 || g > 255 || b > 255) { - throw "Invalid color component"; - } + /** + * Convert a rgb(Number, Number, Number) color to hexadecimal representation + * @param {Number} r red value, between 0 and 255 + * @param {Number} g green value, between 0 and 255 + * @param {Number} b blue value, between 0 and 255 + * @return {String} hex representation of the color '#ABCDEF' + */ + ns.rgbToHex = function (r, g, b) { + return "#" + pskl.utils.componentToHex(r) + pskl.utils.componentToHex(g) + pskl.utils.componentToHex(b); + }; - return ((r << 16) | (g << 8) | b).toString(16); + /** + * Convert a color component (as a Number between 0 and 255) to its string hexa representation + * @param {Number} c component value, between 0 and 255 + * @return {String} eg. '0A' + */ + ns.componentToHex = function (c) { + var hex = c.toString(16); + return hex.length == 1 ? "0" + hex : hex; }; ns.normalize = function (value, def) {