~eda-qa/dhlib/main

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
/* <license>
 * This file is part of the dis-Emi-A HaXe Library. Copyright (c) edA-qa mort-ora-y
 * For full copyright and license information please refer to doc/license.txt.
 * </license> 
 */
package flashx.filter;

import flash.filters.ColorMatrixFilter;

/**
 * A series of helpers to create Color filters
 */
class Color
{
	/**
	 * This performs a pseudo-grayscale conversion, that is, based
	 * on the average intensity of the pixels -- since there is no filter
	 * to do true grayscale (based on max or min value of the pixels)
	 */
	static public function grayscale( ) : ColorMatrixFilter
	{
		var matrix = [
			1.0/3, 1.0/3, 1.0/3, 0,0, // red
        	1.0/3, 1.0/3, 1.0/3, 0,0, // green
        	1.0/3, 1.0/3, 1.0/3, 0,0, // blue
        	0, 0, 0, 1.0,0 // alpha
        	];
        return new ColorMatrixFilter( matrix );
	}
	
	static public function rgb( r  : Float, g : Float, b : Float ) : ColorMatrixFilter
	{
		var matrix = [
			r*1.0/3, r*1.0/3, r*1.0/3, 0,0, // red
        	g*1.0/3, g*1.0/3, g*1.0/3, 0,0, // green
        	b*1.0/3, b*1.0/3, b*1.0/3, 0,0, // blue
        	0, 0, 0, 1.0,0 // alpha
        	];
        return new ColorMatrixFilter( matrix );
	}
	
	/**
	 * Like rgb but only applies at a certain level (That is, only partially colorizes)
	 *
	 * @param t [in] 0 <= t <= 1 for level of tinge
	 */
	static public function rgbTinge( r  : Float, g : Float, b : Float, t : Float ) : ColorMatrixFilter
	{
		var ts = 1.0/3 * t;
		var tp = 1 - t;
		
		var matrix = [
			tp + r*ts, r*ts, r*ts, 0,0, // red
        	g*ts, tp + g*ts, g*ts, 0,0, // green
        	b*ts, b*1.0/3, tp + b*ts, 0,0, // blue
        	0, 0, 0, 1.0,0 // alpha
        	];
        return new ColorMatrixFilter( matrix );
	}
}