From 98de3cbb2aa769e18b5e2da8753fb5f3c3dc7b50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 2 Mar 2011 08:52:39 +0100 Subject: [PATCH] Simplify getAverageColor() loop. --- src/item/Raster.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/item/Raster.js b/src/item/Raster.js index 947c6b7c..e8b36ad0 100644 --- a/src/item/Raster.js +++ b/src/item/Raster.js @@ -204,13 +204,12 @@ Raster = Item.extend({ function getAverageColor(pixels) { var channels = [0, 0, 0]; var total = 0; - for (var i = 0, l = pixels.length / 4; i < l; i++) { - var offset = i * 4; - var alpha = pixels[offset + 3] / 255; + for (var i = 0, l = pixels.length; i < l; i += 4) { + var alpha = pixels[i + 3] / 255; total += alpha; - channels[0] += pixels[offset] * alpha; - channels[1] += pixels[offset + 1] * alpha; - channels[2] += pixels[offset + 2] * alpha; + channels[0] += pixels[i] * alpha; + channels[1] += pixels[i + 1] * alpha; + channels[2] += pixels[i + 2] * alpha; } for (var i = 0; i < 3; i++) channels[i] /= total * 255;