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
/*The MIT License

Copyright (c) 2009 Mark Knol

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.*/

package nl.stroep.utils
{
/**
* Util for working with Color
*
* @author Stroep.nl | Mark Knol
*/
public final class Color
{
private var _value:int = 0;

protected var _red:int = 0;
protected var _green:int = 0;
protected var _blue:int = 0;

/// generated list with all grayscale color values
private var _grayList:/*int*/Array;

public function Color ( value:int = 0xFFFFFF ):void
{
this.value = value;
}

/// get the real color value
public function get value():int
{
return (_red << 16) | (_green << 8) | _blue;
}

/// set the real color value
public function set value ( value:int ):void
{
_red = value >> 16 & 0xFF; // red
_green = value >> 8 & 0xFF; // green
_blue = value & 0xFF; // blue

_value = value;
}


/**
* Get the red value of a color
*
* @param color Enter full color from range 0x000000 to 0xFFFFFF
* @return Red colorvalue
* @tiptext
*/
public function get red ( ):int
{
return _red;
}


/**
* Get the green value of a color
*
* @param color Enter full color from range 0x000000 to 0xFFFFFF
* @return Green colorvalue
* @tiptext
*/
public function get green ( ):int
{
return _green;
}


/**
* Get the blue value of a color
*
* @param color Enter full color from range 0x000000 to 0xFFFFFF
* @return Blue colorvalue
* @tiptext
*/
public function get blue ( ):int
{
return _blue;
}


/**
* Set new red value to a color
* @param tint range between 0-255
* @tiptext
*/
public function set red ( val:int ):void
{
_red = val;
_red = limit( _red, 0, 255 );
}


/**
* Set new green value to a color
* @param tint range between 0-255
* @tiptext
*/
public function set green ( val:int ):void
{
_green = val;
_green = limit( _green, 0, 255 );
}


/**
* Set new blue value to a color
* @param tint range between 0-255
* @tiptext
*/
public function set blue ( val:int ):void
{
_blue = val;
_blue = limit( _blue, 0, 255 );
}

/**
* Get a grayscale color from a tint
*
* @param tint Enter tint from range 0 to 255
* @return Gray colorvalue
* @tiptext
*/
public static function grayscale ( val:int = 0 ):int
{
if (val < 0){ val = 0 }
if (val > 255) { val = 255 }

return (val << 16) | (val << 8) | val;
}

/**
* Get a grayscale color from a tint using precalculated internal Vector
*
* @param tint Enter tint from range 0 to 255
* @return Gray colorvalue
* @tiptext
*/
public function grayscaleFast ( val:int = 0 ):int
{
if (val < 0){ val = 0 }
if (val > 255) { val = 255 }

return grayList[val];
}

/**
* Darken or lighten color with count(-255 to 255)<br/>
* Darken = count &lt; 0<br/>
* Lighten = count &gt; 0
* @param count amount sliding color (-255 to 255)
* @return darker color
* @tiptext
*/
public function slideColor( count:int = 0 ):int
{
var retval:int = value;

var r:int = limit( (retval >> 16) + count, 0, 255)
var g:int = limit( (retval >> 8 & 0xFF) + count, 0, 255)
var b:int = limit( (retval & 0xFF) + count, 0, 255)

return (r << 16) | (g << 8) | ( b );
}


/**
* Darken color with amount (0 to 255)
* @param count amount to darken color (0 to 255)
* @return darker color
* @tiptext
*/
public function darker( count:int = 0 ):int
{
return slideColor(-count);
}


/**
* Lighten color with amount (0 to 255)
* @param count amount to lighten color (0 to 255)
* @return lighter color
* @tiptext
*/
public function lighter( count:int = 0 ):int
{
return slideColor(count);
}

/**
* Limit a value; faster alternative to Math.min / Math.max
* @param val
* @param lowerLimit
* @param upperLimit
* @return limited value
*/
private function limit( val:Number, lowerLimit:Number, upperLimit:Number ):Number
{
if (val < lowerLimit){ return lowerLimit }
if (val > upperLimit) { return upperLimit }
return val;
}

private function get grayList():Array
{
if (!_grayList)
{
_grayList = [];
for (var i:uint = 0; i < 255; ++i)
{
_grayList.push( grayscale(i) );
}
}
return _grayList;
}

private function set grayList(value:Array):void
{
_grayList = value;
}
}
}

Change log

r127 by mark.knol on Jan 25, 2011   Diff
updated Color
added ColorUtil class
Go to: 
Project members, sign in to write a code review

Older revisions

r93 by mark.knol on Nov 4, 2010   Diff
BIG reordering of the repository:
- flashflowfactory is now without
transitions, you can download your own
transition pack with favorite
tweenengine.
...
All revisions of this file

File info

Size: 6336 bytes, 242 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting