My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
// ColorMatrix Class v2.6
//
// released under MIT License (X11)
// http://www.opensource.org/licenses/mit-license.php
//
// Author: Mario Klingemann
// http://www.quasimondo.com

/*
Copyright (c) 2006-2010 Mario Klingemann

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// Changes in v1.1:
// Changed the RGB to luminance constants
// Added colorize() method

// Changes in v1.2:
// Added clone()
// Added randomize()
// Added blend()
// Added "filter" property

// Changes in v1.3:
// Added invertAlpha()
// Added thresholdAlpha()

// Changes in v1.4:
// Added luminance2Alpha()

//Changes in v1.5
// Added rotateX();
// Added rotateY();
// Added rotateZ();
// Added shearZ();

//changes in v2.0
// AS3 optimizations
// Added setMultiplicators()
// Added clearChannels()
// Added rotateHue()
// Added transformVector()
// Added applyMatrix()
// Added rotateRed()
// Added rotateGreen()
// Added rotateBlue()
// Added shearRed()
// Added shearGreen()
// Added shearBlue()

//changes in v2.1
// Added applyColorDeficiency()

//changes in v2.2
// Added applyFilter()

//changes in v2.3
// Added threshold_rgb()
// Added RGB2YUV()
// Added YUV2RGB()
// Added invertMatrix()
// Added normalize()
// Added fitRange()
// Added toString()

// fixed factor in threshold

//changes in v2.4
// Added autoDesaturate()

//changes in v2.5
// Added extrapolate()

//changes in v2.51
// fixed a bug in setChannels
// removed some redundand casts from as2

//changes in v2.6
// added tolerance factor to autoDesaturate()
// slightly improved performance of autoDesaturate()

package com.quasimondo.geom {

import __AS3__.vec.Vector;

import flash.display.BitmapData;
import flash.filters.ColorMatrixFilter;
import flash.geom.Matrix3D;
import flash.geom.Point;

public class ColorMatrix {

public static const COLOR_DEFICIENCY_TYPES:Array = [
'Protanopia',
'Protanomaly',
'Deuteranopia',
'Deuteranomaly',
'Tritanopia',
'Tritanomaly',
'Achromatopsia',
'Achromatomaly' ];

// Estimatged occurences of color deficiencies:
// Protanopia: 1.32%
// Protanomaly: 1.32%
// Deuteranopia: 1.21%
// Deuteranomaly: 5.35%
// Tritanopia: 0.031%
// Tritanomaly: 0.0002%
// Achromatopsia: 0.00002%
// Achromatomaly: 0.00002%


// RGB to Luminance conversion constants as found on
// Charles A. Poynton's colorspace-faq:
// http://www.faqs.org/faqs/graphics/colorspace-faq/

private static const LUMA_R:Number = 0.212671;
private static const LUMA_G:Number = 0.71516;
private static const LUMA_B:Number = 0.072169;


// There seem different standards for converting RGB
// values to Luminance. This is the one by Paul Haeberli:

private static const LUMA_R2:Number = 0.3086;
private static const LUMA_G2:Number = 0.6094;
private static const LUMA_B2:Number = 0.0820;



private static const ONETHIRD:Number = 1 / 3;

private static const IDENTITY:Array = [1,0,0,0,0,
0,1,0,0,0,
0,0,1,0,0,
0,0,0,1,0];


private static const RAD:Number = Math.PI / 180;

public var matrix:Array;

private var preHue:ColorMatrix;
private var postHue:ColorMatrix;
private var hueInitialized:Boolean;

/*
Function: ColorMatrix

Constructor

Parameters:

mat - if omitted matrix gets initialized with an
identity matrix. Alternatively it can be
initialized with another ColorMatrix or
an array (there is currently no check
if the array is valid. A correct array
contains 20 elements.)


*/

public function ColorMatrix ( mat:Object = null )
{

if (mat is ColorMatrix )
{
matrix = mat.matrix.concat();
} else if (mat is Array )
{
matrix = mat.concat();
} else
{
reset();
}

}

/*
Function: reset

resets the matrix to the neutral identity matrix. Applying this
matrix to an image will not make any changes to it.

Parameters:

none

Returns:

nothing
*/

public function reset():void
{
matrix = IDENTITY.concat();
}


public function clone():ColorMatrix
{
return new ColorMatrix( matrix );
}

public function invert():void
{
concat([ -1 , 0, 0, 0, 255,
0 , -1, 0, 0, 255,
0 , 0, -1, 0, 255,
0, 0, 0, 1, 0]);
}

/*
Function: adjustSaturation

changes the saturation

Parameters:

s - typical values come in the range 0.0 ... 2.0 where
0.0 means 0% Saturation
0.5 means 50% Saturation
1.0 is 100% Saturation (aka no change)
2.0 is 200% Saturation

Other values outside of this range are possible
-1.0 will invert the hue but keep the luminance


Returns:

nothing


*/

public function adjustSaturation( s:Number ):void{

var sInv:Number;
var irlum:Number;
var iglum:Number;
var iblum:Number;

sInv = (1 - s);
irlum = (sInv * LUMA_R);
iglum = (sInv * LUMA_G);
iblum = (sInv * LUMA_B);

concat([(irlum + s), iglum, iblum, 0, 0,
irlum, (iglum + s), iblum, 0, 0,
irlum, iglum, (iblum + s), 0, 0,
0, 0, 0, 1, 0]);

}


/*
Function: adjustContrast

changes the contrast

Parameters:

s - typical values come in the range -1.0 ... 1.0 where
-1.0 means no contrast (grey)
0 means no change
1.0 is high contrast



Returns:

nothing


*/

public function adjustContrast( r:Number, g:Number = NaN, b:Number = NaN ):void
{
if (isNaN(g)) g = r;
if (isNaN(b)) b = r;
r += 1;
g += 1;
b += 1;

concat([r, 0, 0, 0, (128 * (1 - r)),
0, g, 0, 0, (128 * (1 - g)),
0, 0, b, 0, (128 * (1 - b)),
0, 0, 0, 1, 0]);
}


public function adjustBrightness(r:Number, g:Number=NaN, b:Number=NaN):void
{
if (isNaN(g)) g = r;
if (isNaN(b)) b = r;
concat([1, 0, 0, 0, r,
0, 1, 0, 0, g,
0, 0, 1, 0, b,
0, 0, 0, 1, 0]);
}

public function toGreyscale(r:Number, g:Number, b:Number):void
{
concat([r, g, b, 0, 0,
r, g, b, 0, 0,
r, g, b, 0, 0,
0, 0, 0, 1, 0]);
}


public function adjustHue( degrees:Number ):void
{
degrees *= RAD;
var cos:Number = Math.cos(degrees);
var sin:Number = Math.sin(degrees);
concat([((LUMA_R + (cos * (1 - LUMA_R))) + (sin * -(LUMA_R))), ((LUMA_G + (cos * -(LUMA_G))) + (sin * -(LUMA_G))), ((LUMA_B + (cos * -(LUMA_B))) + (sin * (1 - LUMA_B))), 0, 0,
((LUMA_R + (cos * -(LUMA_R))) + (sin * 0.143)), ((LUMA_G + (cos * (1 - LUMA_G))) + (sin * 0.14)), ((LUMA_B + (cos * -(LUMA_B))) + (sin * -0.283)), 0, 0,
((LUMA_R + (cos * -(LUMA_R))) + (sin * -((1 - LUMA_R)))), ((LUMA_G + (cos * -(LUMA_G))) + (sin * LUMA_G)), ((LUMA_B + (cos * (1 - LUMA_B))) + (sin * LUMA_B)), 0, 0,
0, 0, 0, 1, 0]);
}


public function rotateHue( degrees:Number ):void
{
initHue();

concat( preHue.matrix );
rotateBlue( degrees );
concat( postHue.matrix );

}




public function luminance2Alpha():void
{
concat([0, 0, 0, 0, 255,
0, 0, 0, 0, 255,
0, 0, 0, 0, 255,
LUMA_R, LUMA_G, LUMA_B, 0, 0]);
}

public function adjustAlphaContrast(amount:Number):void
{
amount += 1;
concat([1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, amount, (128 * (1 - amount))]);
}

public function colorize(rgb:int, amount:Number=1):void
{
var r:Number;
var g:Number;
var b:Number;
var inv_amount:Number;

r = (((rgb >> 16) & 0xFF) / 0xFF);
g = (((rgb >> 8) & 0xFF) / 0xFF);
b = ((rgb & 0xFF) / 0xFF);
inv_amount = (1 - amount);

concat([(inv_amount + ((amount * r) * LUMA_R)), ((amount * r) * LUMA_G), ((amount * r) * LUMA_B), 0, 0,
((amount * g) * LUMA_R), (inv_amount + ((amount * g) * LUMA_G)), ((amount * g) * LUMA_B), 0, 0,
((amount * b) * LUMA_R), ((amount * b) * LUMA_G), (inv_amount + ((amount * b) * LUMA_B)), 0, 0,
0, 0, 0, 1, 0]);
}


public function setChannels( r:int = 1, g:int = 2, b:int = 4, a:int = 8):void
{
var rf:Number = (((r & 1) == 1) ? 1 : 0 ) + (((r & 2) == 2) ? 1 : 0) + (((r & 4) == 4) ? 1 : 0) + (((r & 8) == 8) ? 1 : 0);
if (rf > 0){
rf = (1 / rf);
};
var gf:Number = (((g & 1) == 1) ? 1 : 0 ) + (((g & 2) == 2) ? 1 : 0) + (((g & 4) == 4) ? 1 : 0) + (((g & 8) == 8) ? 1 : 0);
if (gf > 0){
gf = (1 / gf);
};
var bf:Number = (((b & 1) == 1) ? 1 : 0 ) + (((b & 2) == 2) ? 1 : 0) + (((b & 4) == 4) ? 1 : 0) + (((b & 8) == 8) ? 1 : 0);
if (bf > 0){
bf = (1 / bf);
};
var af:Number = (((a & 1) == 1) ? 1 : 0 ) + (((a & 2) == 2) ? 1 : 0) + (((a & 4) == 4) ? 1 : 0) + (((a & 8) == 8) ? 1 : 0);
if (af > 0){
af = (1 / af);
};
concat([(((r & 1) == 1)) ? rf : 0, (((r & 2) == 2)) ? rf : 0, (((r & 4) == 4)) ? rf : 0, (((r & 8) == 8)) ? rf : 0, 0, (((g & 1) == 1)) ? gf : 0, (((g & 2) == 2)) ? gf : 0, (((g & 4) == 4)) ? gf : 0, (((g & 8) == 8)) ? gf : 0, 0, (((b & 1) == 1)) ? bf : 0, (((b & 2) == 2)) ? bf : 0, (((b & 4) == 4)) ? bf : 0, (((b & 8) == 8)) ? bf : 0, 0, (((a & 1) == 1)) ? af : 0, (((a & 2) == 2)) ? af : 0, (((a & 4) == 4)) ? af : 0, (((a & 8) == 8)) ? af : 0, 0]);
}


public function blend(mat:ColorMatrix, amount:Number):void
{
var inv_amount:Number = (1 - amount);
var i:int = 0;
while (i < 20)
{
matrix[i] = ((inv_amount * matrix[i]) + (amount * mat.matrix[i]));
i++;
};
}

public function extrapolate(mat:ColorMatrix, factor:Number ):void
{
var i:int = 0;
while (i < 20)
{
matrix[i] += ( mat.matrix[i] - matrix[i]) * factor;
i++;
};
}

public function average( r:Number=ONETHIRD, g:Number=ONETHIRD, b:Number=ONETHIRD):void
{
concat([r, g, b, 0, 0,
r, g, b, 0, 0,
r, g, b, 0, 0,
0, 0, 0, 1, 0]);
}

public function threshold(threshold:Number, factor:Number=256):void
{
concat([(LUMA_R * factor), (LUMA_G * factor), (LUMA_B * factor), 0, (-(factor-1) * threshold),
(LUMA_R * factor), (LUMA_G * factor), (LUMA_B * factor), 0, (-(factor-1) * threshold),
(LUMA_R * factor), (LUMA_G * factor), (LUMA_B * factor), 0, (-(factor-1) * threshold),
0, 0, 0, 1, 0]);
}

public function threshold_rgb(threshold:Number, factor:Number=256):void
{
concat([factor, 0, 0, 0, (-(factor-1) * threshold),
0, factor, 0, 0, (-(factor-1) * threshold),
0, 0, factor, 0, (-(factor-1) * threshold),
0, 0, 0, 1, 0]);
}

public function desaturate():void
{
concat([LUMA_R, LUMA_G, LUMA_B, 0, 0,
LUMA_R, LUMA_G, LUMA_B, 0, 0,
LUMA_R, LUMA_G, LUMA_B, 0, 0,
0, 0, 0, 1, 0]);
}


public function randomize(amount:Number=1):void
{
var inv_amount:Number = (1 - amount);
var r1:Number = (inv_amount + (amount * (Math.random() - Math.random())));
var g1:Number = (amount * (Math.random() - Math.random()));
var b1:Number = (amount * (Math.random() - Math.random()));
var o1:Number = ((amount * 0xFF) * (Math.random() - Math.random()));
var r2:Number = (amount * (Math.random() - Math.random()));
var g2:Number = (inv_amount + (amount * (Math.random() - Math.random())));
var b2:Number = (amount * (Math.random() - Math.random()));
var o2:Number = ((amount * 0xFF) * (Math.random() - Math.random()));
var r3:Number = (amount * (Math.random() - Math.random()));
var g3:Number = (amount * (Math.random() - Math.random()));
var b3:Number = (inv_amount + (amount * (Math.random() - Math.random())));
var o3:Number = ((amount * 0xFF) * (Math.random() - Math.random()));

concat([r1, g1, b1, 0, o1,
r2, g2, b2, 0, o2,
r3, g3, b3, 0, o3,
0, 0, 0, 1, 0]);
}




public function setMultiplicators( red:Number = 1, green:Number = 1, blue:Number = 1, alpha:Number = 1 ):void
{
var mat:Array = new Array ( red, 0, 0, 0, 0,
0, green, 0, 0, 0,
0, 0, blue, 0, 0,
0, 0, 0, alpha, 0 );

concat(mat);
}

public function clearChannels( red:Boolean = false, green:Boolean = false, blue:Boolean = false, alpha:Boolean = false ):void
{
if ( red )
{
matrix[0] = matrix[1] = matrix[2] = matrix[3] = matrix[4] = 0;
}
if ( green )
{
matrix[5] = matrix[6] = matrix[7] = matrix[8] = matrix[9] = 0;
}
if ( blue )
{
matrix[10] = matrix[11] = matrix[12] = matrix[13] = matrix[14] = 0;
}
if ( alpha )
{
matrix[15] = matrix[16] = matrix[17] = matrix[18] = matrix[19] = 0;
}
}

public function thresholdAlpha( threshold:Number, factor:Number = 256):void
{
concat([1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, factor, (-factor * threshold)]);
}

public function averageRGB2Alpha():void
{
concat([0, 0, 0, 0, 255,
0, 0, 0, 0, 255,
0, 0, 0, 0, 255,
ONETHIRD, ONETHIRD, ONETHIRD, 0, 0]);
}

public function invertAlpha():void
{
concat([1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, -1, 255]);
}

public function rgb2Alpha( r:Number, g:Number, b:Number ):void
{
concat([0, 0, 0, 0, 255,
0, 0, 0, 0, 255,
0, 0, 0, 0, 255,
r, g, b, 0, 0]);
}

public function setAlpha(alpha:Number):void
{
concat([1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, alpha, 0]);
}

public function get filter():ColorMatrixFilter
{
return new ColorMatrixFilter( matrix );
}

public function applyFilter( bitmapData:BitmapData ):void
{
bitmapData.applyFilter( bitmapData, bitmapData.rect, new Point(), filter );
}


public function concat( mat:Array ):void
{

var temp:Array = [];
var i:int = 0;
var x:int, y:int;
for (y = 0; y < 4; y++ )
{

for (x = 0; x < 5; x++ )
{
temp[ int( i + x) ] = mat[i ] * matrix[x] +
mat[int(i+1)] * matrix[int(x + 5)] +
mat[int(i+2)] * matrix[int(x + 10)] +
mat[int(i+3)] * matrix[int(x + 15)] +
(x == 4 ? mat[int(i+4)] : 0);
}
i+=5;
}

matrix = temp;

}

public function rotateRed( degrees:Number ):void
{
rotateColor( degrees, 2, 1 );
}

public function rotateGreen( degrees:Number ):void
{
rotateColor( degrees, 0, 2 );
}

public function rotateBlue( degrees:Number ):void
{
rotateColor( degrees, 1, 0 );
}

public function normalize():void
{
for ( var i:int = 0; i < 4; i++ )
{
var sum:Number = 0;

for ( var j:int = 0; j < 4; j++ )
{
sum += matrix[i*5+j] * matrix[i*5+j];
}

sum = 1 / Math.sqrt( sum );
if ( sum != 1 )
{
for ( j = 0; j < 4; j++ )
{
matrix[i*5+j] *= sum;
}
}
}
}

public function fitRange():void
{

for ( var i:int = 0; i < 4; i++ )
{
var minFactor:Number = 0;
var maxFactor:Number = 0;

for ( var j:int = 0; j < 4; j++ )
{
if ( matrix[i*5+j] < 0 ) minFactor += matrix[i*5+j];
else maxFactor += matrix[i*5+j];
}

var range:Number = maxFactor * 255 - minFactor * 255;
var rangeCorrection:Number = 255 / range;

if ( rangeCorrection != 1 )
{
for ( j = 0; j < 4; j++ )
{
matrix[i*5+j] *= rangeCorrection;
}
}

minFactor = 0;
maxFactor = 0;

for ( j = 0; j < 4; j++ )
{
if ( matrix[i*5+j] < 0 ) minFactor += matrix[i*5+j];
else maxFactor += matrix[i*5+j];
}

var worstMin:Number = minFactor * 255;
var worstMax:Number = maxFactor * 255;

matrix[i*5+4] = - ( worstMin + ( worstMax - worstMin ) * 0.5 - 127.5 );
}
}

private function rotateColor( degrees:Number, x:int, y:int ):void
{
degrees *= RAD;
var mat:Array = IDENTITY.concat();
mat[ x + x * 5 ] = mat[ y + y * 5 ] = Math.cos( degrees );
mat[ y + x * 5 ] = Math.sin( degrees );
mat[ x + y * 5 ] = -Math.sin( degrees );
concat( mat );
}

public function shearRed( green:Number, blue:Number ):void
{
shearColor( 0, 1, green, 2, blue );
}

public function shearGreen( red:Number, blue:Number ):void
{
shearColor( 1, 0, red, 2, blue );
}

public function shearBlue( red:Number, green:Number ):void
{
shearColor( 2, 0, red, 1, green );
}

private function shearColor( x:int, y1:int, d1:Number, y2:int, d2:Number ):void
{
var mat:Array = IDENTITY.concat();
mat[ y1 + x * 5 ] = d1;
mat[ y2 + x * 5 ] = d2;
concat( mat );
}

public function applyColorDeficiency( type:String ):void
{
// the values of this method are copied from http://www.nofunc.com/Color_Matrix_Library/

switch ( type )
{
case 'Protanopia':
concat([0.567,0.433,0,0,0, 0.558,0.442,0,0,0, 0,0.242,0.758,0,0, 0,0,0,1,0]);
break;
case 'Protanomaly':
concat([0.817,0.183,0,0,0, 0.333,0.667,0,0,0, 0,0.125,0.875,0,0, 0,0,0,1,0]);
break;
case 'Deuteranopia':
concat([0.625,0.375,0,0,0, 0.7,0.3,0,0,0, 0,0.3,0.7,0,0, 0,0,0,1,0]);
break;
case 'Deuteranomaly':
concat([0.8,0.2,0,0,0, 0.258,0.742,0,0,0, 0,0.142,0.858,0,0, 0,0,0,1,0]);
break;
case 'Tritanopia':
concat([0.95,0.05,0,0,0, 0,0.433,0.567,0,0, 0,0.475,0.525,0,0, 0,0,0,1,0]);
break;
case 'Tritanomaly':
concat([0.967,0.033,0,0,0, 0,0.733,0.267,0,0, 0,0.183,0.817,0,0, 0,0,0,1,0]);
break;
case 'Achromatopsia':
concat([0.299,0.587,0.114,0,0, 0.299,0.587,0.114,0,0, 0.299,0.587,0.114,0,0, 0,0,0,1,0]);
break;
case 'Achromatomaly':
concat([0.618,0.320,0.062,0,0, 0.163,0.775,0.062,0,0, 0.163,0.320,0.516,0,0, 0,0,0,1,0]);
break;

}

}

public function RGB2YUV():void
{
concat([ 0.29900, 0.58700, 0.11400, 0, 0,
-0.16874, -0.33126, 0.50000, 0, 128,
0.50000, -0.41869, -0.08131, 0, 128,
0 , 0 , 0 , 1, 0 ]);

}

public function YUV2RGB():void
{
concat([ 1 , -0.000007154783816076815, 1.4019975662231445 , 0, -179.45477266423404,
1 , -0.3441331386566162 , -0.7141380310058594 , 0, 135.45870971679688,
1 , 1.7720025777816772 , 0.00001542569043522235, 0, -226.8183044444304,
0 , 0 , 0 , 1, 0 ]);

}


public function RGB2YIQ():void
{
concat([ 0.2990, 0.5870, 0.1140, 0, 0,
0.595716, -0.274453, -0.321263, 0, 128,
0.211456, -0.522591, -0.311135, 0, 128,
0 , 0 , 0 , 1, 0 ]);
}
/*
public function YIQ2RGB():void
{
concat([ 1, ,-0.000007154783816076815, 1.4019975662231445 , 0, -179.45477266423404,
1 , -0.3441331386566162 , -0.7141380310058594 , 0, 135.45870971679688,
1 , 1.7720025777816772 , 0.00001542569043522235, 0, -226.8183044444304,
0 , 0 , 0 , 1, 0 ]);

}
*/

public function autoDesaturate( bitmapData:BitmapData, stretchLevels:Boolean = false, outputToBlueOnly:Boolean = false, tolerance:Number = 0.01 ):void
{
var histogram:Vector.<Vector.<Number>> = bitmapData.histogram(bitmapData.rect );

var sum_r:Number = 0;
var sum_g:Number = 0;
var sum_b:Number = 0;
var min:Number;
var max:Number;
var minFound:Boolean = false;

var histR:Vector.<Number> = histogram[0];
var histG:Vector.<Number> = histogram[1];
var histB:Vector.<Number> = histogram[2];

for ( var i:int = 0; i < 256; i++ )
{
sum_r += histR[i] * i;
sum_g += histG[i] * i;
sum_b += histB[i] * i;
}

var total:Number = sum_r + sum_g + sum_b;
if ( total == 0 )
{
total = 3;
sum_r = sum_g = sum_b = 3;
}

sum_r /= total;
sum_g /= total;
sum_b /= total;

var offset:Number = 0;

if ( stretchLevels )
{
var minPixels:Number = bitmapData.rect.width * bitmapData.rect.height * tolerance;
var sr:Number = 0;
var sg:Number = 0;
var sb:Number = 0;
for ( i = 0; i < 256; i++ )
{
sr += histR[i];
sg += histG[i];
sb += histB[i];
if ( sr > minPixels || sg > minPixels || sb > minPixels )
{
min = i;
break;
}
}
sr = 0;
sg = 0;
sb = 0;
for ( i = 256; --i > -1; )
{
sr += histR[i];
sg += histG[i];
sb += histB[i];
if ( sr > minPixels || sg > minPixels || sb > minPixels )
{
max = i;
break;
}
}

if ( max - min < 255)
{
var f:Number = 256 / ((max - min) + 1);
sum_r *= f;
sum_g *= f;
sum_b *= f;
offset = -min;
}
}


f = 1 / Math.sqrt(sum_r * sum_r + sum_g * sum_g + sum_b * sum_b);
sum_r *= f;
sum_g *= f;
sum_b *= f;

if ( !outputToBlueOnly )
concat([sum_r,sum_g,sum_b,0,offset,
sum_r,sum_g,sum_b,0,offset,
sum_r,sum_g,sum_b,0,offset,
0,0,0,1,0]);
else
concat([0,0,0,0,0,
0,0,0,0,0,
sum_r,sum_g,sum_b,0,offset,
0,0,0,1,0]);

}


public function invertMatrix():Boolean
{
var coeffs:Matrix3D = new Matrix3D( Vector.<Number>( [matrix[0],matrix[1],matrix[2],matrix[3],
matrix[5],matrix[6],matrix[7],matrix[8],
matrix[10],matrix[11],matrix[12],matrix[13],
matrix[15],matrix[16],matrix[17],matrix[18]]
) );

var check:Boolean = coeffs.invert();
if (!check) return false;

matrix[0] = coeffs.rawData[0];
matrix[1] = coeffs.rawData[1];
matrix[2] = coeffs.rawData[2];
matrix[3] = coeffs.rawData[3];
var tmp1:Number = -( coeffs.rawData[0] * matrix[4] + coeffs.rawData[1] * matrix[9] + coeffs.rawData[2] * matrix[14] + coeffs.rawData[3] * matrix[15] );

matrix[5] = coeffs.rawData[4];
matrix[6] = coeffs.rawData[5];
matrix[7] = coeffs.rawData[6];
matrix[8] = coeffs.rawData[7];
var tmp2:Number = -( coeffs.rawData[4] * matrix[4] + coeffs.rawData[5] * matrix[9] + coeffs.rawData[6] * matrix[14] + coeffs.rawData[7] * matrix[15] );

matrix[10] = coeffs.rawData[8];
matrix[11] = coeffs.rawData[9];
matrix[12] = coeffs.rawData[10];
matrix[13] = coeffs.rawData[11];
var tmp3:Number = -( coeffs.rawData[8] * matrix[4] + coeffs.rawData[9] * matrix[9] + coeffs.rawData[10] * matrix[14] + coeffs.rawData[11] * matrix[15] );

matrix[15] = coeffs.rawData[12];
matrix[16] = coeffs.rawData[13];
matrix[17] = coeffs.rawData[14];
matrix[18] = coeffs.rawData[15];
var tmp4:Number = -( coeffs.rawData[12] * matrix[4] + coeffs.rawData[13] * matrix[9] + coeffs.rawData[14] * matrix[14] + coeffs.rawData[15] * matrix[15] );

matrix[4] = tmp1;
matrix[9] = tmp2;
matrix[14] = tmp3;
matrix[19] = tmp4;

return true;
}

public function applyMatrix( rgba:uint ):uint
{
var a:Number = ( rgba >>> 24 ) & 0xff;
var r:Number = ( rgba >>> 16 ) & 0xff;
var g:Number = ( rgba >>> 8 ) & 0xff;
var b:Number = rgba & 0xff;

var r2:int = 0.5 + r * matrix[0] + g * matrix[1] + b * matrix[2] + a * matrix[3] + matrix[4];
var g2:int = 0.5 + r * matrix[5] + g * matrix[6] + b * matrix[7] + a * matrix[8] + matrix[9];
var b2:int = 0.5 + r * matrix[10] + g * matrix[11] + b * matrix[12] + a * matrix[13] + matrix[14];
var a2:int = 0.5 + r * matrix[15] + g * matrix[16] + b * matrix[17] + a * matrix[18] + matrix[19];

if ( a2 < 0 ) a2 = 0;
if ( a2 > 255 ) a2 = 255;
if ( r2 < 0 ) r2 = 0;
if ( r2 > 255 ) r2 = 255;
if ( g2 < 0 ) g2 = 0;
if ( g2 > 255 ) g2 = 255;
if ( b2 < 0 ) b2 = 0;
if ( b2 > 255 ) b2 = 255;

return a2<<24 | r2<<16 | g2<<8 | b2;
}


public function transformVector( values:Array ):void
{
if ( values.length != 4) return;

var r:Number = values[0] * matrix[0] + values[1] * matrix[1] + values[2] * matrix[2] + values[3] * matrix[3] + matrix[4];
var g:Number = values[0] * matrix[5] + values[1] * matrix[6] + values[2] * matrix[7] + values[3] * matrix[8] + matrix[9];
var b:Number = values[0] * matrix[10] + values[1] * matrix[11] + values[2] * matrix[12] + values[3] * matrix[13] + matrix[14];
var a:Number = values[0] * matrix[15] + values[1] * matrix[16] + values[2] * matrix[17] + values[3] * matrix[18] + matrix[19];

values[0] = r;
values[1] = g;
values[2] = b;
values[3] = a;
}


private function initHue():void
{

//var greenRotation:Number = 35.0;
var greenRotation:Number = 39.182655;

if (!hueInitialized)
{
hueInitialized = true;
preHue = new ColorMatrix();
preHue.rotateRed( 45 );
preHue.rotateGreen(- greenRotation );

var lum:Array = [ LUMA_R2, LUMA_G2, LUMA_B2, 1.0 ];

preHue.transformVector(lum);

var red:Number = lum[0] / lum[2];
var green:Number = lum[1] / lum[2];

preHue.shearBlue(red, green);

postHue = new ColorMatrix();
postHue.shearBlue( -red, -green);
postHue.rotateGreen(greenRotation);
postHue.rotateRed(- 45.0 );
}

}


public function toString():String
{
return matrix.toString();
}
}
}

Change log

r59 by quasimondo on Jun 1, 2011   Diff
Optimized ColorMatrix.autoDesaturate a bit
Added tolerance factor for level
stretching to olorMatrix.autoDesaturate
Go to: 
Project members, sign in to write a code review

Older revisions

r6 by quasimondo on Feb 16, 2010   Diff
Added Credits for Paul Haeberli
r5 by quasimondo on Feb 16, 2010   Diff
Added InteractiveSprite
Updated ColorMatrix to 2.41 - added
several neutral default values to
methods
r2 by quasimondo on Feb 16, 2010   Diff
Added ColorMatrix, CoordinateShuffler,
CameraBitmap and ThresholdBitmap
All revisions of this file

File info

Size: 30371 bytes, 998 lines
Powered by Google Project Hosting