From b77c72220e3ce62a47396fd49d25690c488ee435 Mon Sep 17 00:00:00 2001 From: Nathan Curtis Date: Mon, 23 Apr 2007 22:04:03 +0000 Subject: [PATCH] Rather than break out a general purpose matrix algebra class that may not be needed anywhere else, just compress those huge unneccessary functions into a couple of sensible loops for this specific case. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@204 ed5b42cb-e716-0410-a449-f6a68f950b19 --- src/as/com/threerings/flash/FilterUtil.as | 121 ++++++++-------------- 1 file changed, 45 insertions(+), 76 deletions(-) diff --git a/src/as/com/threerings/flash/FilterUtil.as b/src/as/com/threerings/flash/FilterUtil.as index f093ea7b..008f4793 100644 --- a/src/as/com/threerings/flash/FilterUtil.as +++ b/src/as/com/threerings/flash/FilterUtil.as @@ -160,6 +160,15 @@ public class FilterUtil } } + /** + * Create a filter that, if applied to a DisplayObject, will shift the hue of that object + * by the given value. + */ + public static function createHueShift (hue :int) :ColorMatrixFilter + { + return shiftHueBy(null, hue); + } + /** * Shift the color matrix filter by the given amount. This is adapted from the code found at * http://www.kirupa.com/forum/showthread.php?t=230706 @@ -167,27 +176,46 @@ public class FilterUtil public static function shiftHueBy (original :ColorMatrixFilter, hueShift :int) :ColorMatrixFilter { - var M1 :Array = [ 0.213, 0.715, 0.072, - 0.213, 0.715, 0.072, - 0.213, 0.715, 0.072 ]; - var M2 :Array = [ 0.787, -0.715, -0.072, - -0.212, 0.285, -0.072, - -0.213, -0.715, 0.928 ]; - var M3 :Array = [-0.213, -0.715, 0.928, - 0.143, 0.140, -0.283, - -0.787, 0.715, 0.072 ]; - var M4 :Array = add(M1, add(multiply(Math.cos(hueShift * Math.PI / 180), M2), - multiply(Math.sin(hueShift * Math.PI / 180), M3))); + var cosMatrix :Array = [ 0.787, -0.715, -0.072, + -0.212, 0.285, -0.072, + -0.213, -0.715, 0.928 ]; + var sinMatrix :Array = [-0.213, -0.715, 0.928, + 0.143, 0.140, -0.283, + -0.787, 0.715, 0.072 ]; + var multiplier :Array = []; + var cos :Number = Math.cos(hueShift * Math.PI / 180); + var sin :Number = Math.sin(hueShift * Math.PI / 180); + for (var ii :int = 0; ii < 9; ii++) { + multiplier.push([ 0.213, 0.715, 0.072 ][ii%3] + cosMatrix[ii] * cos + + sinMatrix[ii] * sin); + } - var originalMatrix :Array = original.matrix; - if (originalMatrix == null) { + var originalMatrix :Array; + if (original == null || original.matrix == null) { + // thats the identity matrix for this filter + originalMatrix = [ 1, 0, 0, 0, 0, + 0, 1, 0, 0, 0, + 0, 0, 1, 0, 0, + 0, 0, 0, 1, 0 ]; + } else { originalMatrix = original.matrix; } - return new ColorMatrixFilter(concat(originalMatrix, [ M4[0], M4[1], M4[2], 0, 0, - M4[3], M4[4], M4[5], 0, 0, - M4[6], M4[7], M4[8], 0, 0, - 0, 0, 0, 1, 0 ])); + // this loop compresses a massive, wacky concatination function that was used in the code + // from the site listed above + var matrix :Array = []; + for (ii = 0; ii < 20; ii++) { + if ((ii % 5) > 2) { + matrix.push(originalMatrix[ii]); + } else { + var base :int = Math.floor(ii / 5) * 5; + matrix.push((originalMatrix[base] * multiplier[ii%5]) + + (originalMatrix[base+1] * multiplier[(ii%5)+3]) + + (originalMatrix[base+2] * multiplier[(ii%5)+6])); + } + } + + return new ColorMatrixFilter(matrix); } protected static function checkArgs (disp :DisplayObject, filter :BitmapFilter) :void @@ -196,64 +224,5 @@ public class FilterUtil throw new ArgumentError("args may not be null"); } } - - protected static function identity () :Array - { - return [ 1, 0, 0, 0, 0, - 0, 1, 0, 0, 0, - 0, 0, 1, 0, 0, - 0, 0, 0, 1, 0 ]; - } - - protected static function add (A :Array, B :Array) :Array - { - var C :Array = []; - for(var ii :int = 0; ii < A.length; ii++) - { - C.push( A[ii] + B[ii] ); - } - return C; - } - - protected static function multiply(x :Number, B :Array) :Array - { - var A :Array = []; - for each (var n :Number in B) - { - A.push(x * n); - } - return A; - } - - protected static function concat(A :Array, B :Array) :Array - { - var nM :Array = []; - - nM[0] = (A[0] * B[0]) + (A[1] * B[5]) + (A[2] * B[10]) + (A[3] * B[15]); - nM[1] = (A[0] * B[1]) + (A[1] * B[6]) + (A[2] * B[11]) + (A[3] * B[16]); - nM[2] = (A[0] * B[2]) + (A[1] * B[7]) + (A[2] * B[12]) + (A[3] * B[17]); - nM[3] = (A[0] * B[3]) + (A[1] * B[8]) + (A[2] * B[13]) + (A[3] * B[18]); - nM[4] = (A[0] * B[4]) + (A[1] * B[9]) + (A[2] * B[14]) + (A[3] * B[19]) + A[4]; - - nM[5] = (A[5] * B[0]) + (A[6] * B[5]) + (A[7] * B[10]) + (A[8] * B[15]); - nM[6] = (A[5] * B[1]) + (A[6] * B[6]) + (A[7] * B[11]) + (A[8] * B[16]); - nM[7] = (A[5] * B[2]) + (A[6] * B[7]) + (A[7] * B[12]) + (A[8] * B[17]); - nM[8] = (A[5] * B[3]) + (A[6] * B[8]) + (A[7] * B[13]) + (A[8] * B[18]); - nM[9] = (A[5] * B[4]) + (A[6] * B[9]) + (A[7] * B[14]) + (A[8] * B[19]) + A[9]; - - nM[10] = (A[10] * B[0]) + (A[11] * B[5]) + (A[12] * B[10]) + (A[13] * B[15]); - nM[11] = (A[10] * B[1]) + (A[11] * B[6]) + (A[12] * B[11]) + (A[13] * B[16]); - nM[12] = (A[10] * B[2]) + (A[11] * B[7]) + (A[12] * B[12]) + (A[13] * B[17]); - nM[13] = (A[10] * B[3]) + (A[11] * B[8]) + (A[12] * B[13]) + (A[13] * B[18]); - nM[14] = (A[10] * B[4]) + (A[11] * B[9]) + (A[12] * B[14]) + (A[13] * B[19]) + A[14]; - - nM[15] = (A[15] * B[0]) + (A[16] * B[5]) + (A[17] * B[10]) + (A[18] * B[15]); - nM[16] = (A[15] * B[1]) + (A[16] * B[6]) + (A[17] * B[11]) + (A[18] * B[16]); - nM[17] = (A[15] * B[2]) + (A[16] * B[7]) + (A[17] * B[12]) + (A[18] * B[17]); - nM[18] = (A[15] * B[3]) + (A[16] * B[8]) + (A[17] * B[13]) + (A[18] * B[18]); - nM[19] = (A[15] * B[4]) + (A[16] * B[9]) + (A[17] * B[14]) + (A[18] * B[19]) + A[19]; - - return nM; - } } }