-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathUtils.as
More file actions
executable file
·401 lines (350 loc) · 15.2 KB
/
Utils.as
File metadata and controls
executable file
·401 lines (350 loc) · 15.2 KB
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
package
{
import flash.display.BitmapData;
import flash.display.Shape;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import org.flixel.FlxPoint;
import org.flixel.FlxSprite;
import org.flixel.FlxParticle;
import org.flixel.FlxGroup;
import org.flixel.FlxG;
public class Utils{
public static const DEG_TO_RAD:Number = Math.PI/180;
public static var ROMAN_VALUES:Array = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
public static var ROMAN_LETTERS:Array = ['M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I']
/**
* Shrinks the hitbox (x,y and offset) of given FlxSprite to the pixels that are actually
* covered by a greater portion than min{X,Y}Cover of the pixels.
*/
static public function shrinkHitbox(sprite:FlxSprite, minXCover:Number=0, minYCover:Number=0.5):void{
var pix:BitmapData = sprite.pixels;
var x:int, y:int, sum:int = 0;
var minx:int = pix.width;
var maxx:int = 0;
var miny:int = pix.height;
var maxy:int = 0;
for (x = 0; x < pix.width; x++){
sum = 0
for (y = 0; y < pix.height; y++){
if (pix.getPixel32(x,y) > 0x00FFFFFF) sum ++;
}
if (sum >= minXCover*pix.height) {
minx = Math.min(minx,x);
maxx = Math.max(maxx,x);
}
}
for (y = 0; y < pix.height; y++){
sum = 0
for (x = 0; x < pix.width; x++){
if (pix.getPixel32(x,y) > 0x00FFFFFF) sum ++;
}
if (sum >= minYCover*pix.width) {
miny = Math.min(miny,y);
maxy = Math.max(maxy,y);
}
}
sprite.width = maxx-minx;
sprite.height = maxy-miny;
sprite.offset.x = minx
sprite.offset.y = miny;
sprite.x += minx/2;
sprite.y += miny/2;
}
static public function findRectanglesWithColor(bitmap:BitmapData, color:uint):Vector.<Rectangle> {
var cur:Rectangle;
var rects:Vector.<Rectangle> = new Vector.<Rectangle>();
var i:int,j:int,k:int;
for(i = 0; i < bitmap.height; i++){
for(j = 0; j < bitmap.width; j++){
for(k = 0; k < rects.length; k++){
cur = rects[k];
// If we are in a rect, skip to the right side.
if (cur.contains(j,i)){
j = cur.right;
// If we are outside of the image, next line.
if (j >= bitmap.width){
j = 0;
i++;
}
}
}
if (bitmap.getPixel32(j,i) == color){
rects.push(cur = new Rectangle(j,i,1,1));
// Traverse to right
while( bitmap.getPixel32(j,i) == color){
j++;
}
cur.width = j - cur.x;
// Traverse down
while( bitmap.getPixel32(j-1,i) == color){
i++;
}
cur.height = i - cur.y;
// Reset I and J
j = cur.right;
i = cur.y;
}
}
}
return rects;
}
/**
* Replace one color with another in bitmap.
*/
static public function replaceColor(bitmap:BitmapData,fromColor:uint, toColor:uint):void{
for (var i:int = 0; i < bitmap.height; i++){
for( var j:int = 0; j < bitmap.width; j++){
if (bitmap.getPixel32(j,i) == fromColor){
bitmap.setPixel32(j, i, toColor);
}
}
}
}
/**
* Fills a bitmap with a gradient with given colors
*/
static public function gradientOverlay(bitmap:BitmapData, colors:Array, rotation:Number=90, chunks:int = 1):void{
var matrix:Matrix = new Matrix();
matrix.createGradientBox(bitmap.width/chunks, bitmap.height/chunks, rotation*DEG_TO_RAD);
var s:Shape = new Shape();
var ratios:Array = new Array(colors.length);
var alphas:Array = new Array(colors.length);
for (var i:int = 0; i < colors.length; i ++){
alphas[i] = (colors[i] >>> 24)/255;
ratios[i] = (i * (1/(colors.length-1)))*255;
}
s.graphics.beginGradientFill("linear", colors, alphas, ratios, matrix, "pad", "rgb");
s.graphics.drawRect(0, 0, bitmap.width/chunks, bitmap.height/chunks);
if (chunks == 1) {
bitmap.draw(s);
} else {
var transform:Matrix = new Matrix();
var tempBitmap:BitmapData = new BitmapData(bitmap.width/chunks,bitmap.height/chunks,true,0x000000);
tempBitmap.draw(s);
transform.scale(bitmap.width/tempBitmap.width,bitmap.height/tempBitmap.height);
bitmap.draw(tempBitmap,transform);
}
}
/**
* Convert a HSV (hue, saturation, lightness) color space value to an RGB color
*
* @param h Hue degree, between 0 and 359
* @param s Saturation, between 0.0 (grey) and 1.0
* @param v Value, between 0.0 (black) and 1.0
*
* @return 32-bit RGB colour value (0xAARRGGBB)
*/
public static function HSVtoRGB(h:Number, s:Number, v:Number):uint
{
var result:uint;
if (s == 0.0)
{
result = getColor32(255, v * 255, v * 255, v * 255);
}
else
{
h = h / 60.0;
var f:Number = h - int(h);
var p:Number = v * (1.0 - s);
var q:Number = v * (1.0 - s * f);
var t:Number = v * (1.0 - s * (1.0 - f));
switch (int(h))
{
case 0:
result = getColor32(255, v * 255, t * 255, p * 255);
break;
case 1:
result = getColor32(255, q * 255, v * 255, p * 255);
break;
case 2:
result = getColor32(255, p * 255, v * 255, t * 255);
break;
case 3:
result = getColor32(255, p * 255, q * 255, v * 255);
break;
case 4:
result = getColor32(255, t * 255, p * 255, v * 255);
break;
case 5:
result = getColor32(255, v * 255, p * 255, q * 255);
break;
default:
FlxG.log("FlxColor Error: HSVtoRGB : Unknown color");
}
}
return result;
}
public static function RGBtoHSV(color:uint):Object
{
var rgb:Object = getRGB(color);
var red:Number = rgb.red / 255;
var green:Number = rgb.green / 255;
var blue:Number = rgb.blue / 255;
var min:Number = Math.min(red, green, blue);
var max:Number = Math.max(red, green, blue);
var delta:Number = max - min;
var lightness:Number = (max + min) / 2;
var hue:Number;
var saturation:Number;
// Grey color, no chroma
if (delta == 0)
{
hue = 0;
saturation = 0;
}
else
{
if (lightness < 0.5)
{
saturation = delta / (max + min);
}
else
{
saturation = delta / (2 - max - min);
}
var delta_r:Number = (((max - red) / 6) + (delta / 2)) / delta;
var delta_g:Number = (((max - green) / 6) + (delta / 2)) / delta;
var delta_b:Number = (((max - blue) / 6) + (delta / 2)) / delta;
if (red == max)
{
hue = delta_b - delta_g;
}
else if (green == max)
{
hue = (1 / 3) + delta_r - delta_b;
}
else if (blue == max)
{
hue = (2 / 3) + delta_g - delta_r;
}
if (hue < 0)
{
hue += 1;
}
if (hue > 1)
{
hue -= 1;
}
}
// Keep the value with 0 to 359
hue *= 360;
hue = Math.round(hue);
// Testing
//saturation *= 100;
//lightness *= 100;
return { hue: hue, saturation: saturation, lightness: lightness, value: lightness };
}
public static function interpolateColor(color1:uint, color2:uint, f:Number):uint
{
var a1:uint = color1 >>> 24;
var r1:uint = color1 >> 16 & 0xFF;
var g1:uint = color1 >> 8 & 0xFF;
var b1:uint = color1 & 0xFF;
var a2:uint = color2 >>> 24;
var r2:uint = color2 >> 16 & 0xFF;
var g2:uint = color2 >> 8 & 0xFF;
var b2:uint = color2 & 0xFF;
var fi:Number = (1-f);
a1 = (fi * a1) + (f * a2);
r1 = (fi * r1) + (f * r2);
g1 = (fi * g1) + (f * g2);
b1 = (fi * b1) + (f * b2);
return a1 << 24 | r1 << 16 | g1 << 8 | b1;
}
public static function interpolateColorAndAlpha(color1:uint, color2:uint, steps:uint, currentStep:uint):uint
{
var src1:Object = getRGB(color1);
var src2:Object = getRGB(color2);
var a:uint = (((src2.alpha - src1.alpha) * currentStep) / steps) + src1.alpha;
var r:uint = (((src2.red - src1.red) * currentStep) / steps) + src1.red;
var g:uint = (((src2.green - src1.green) * currentStep) / steps) + src1.green;
var b:uint = (((src2.blue - src1.blue) * currentStep) / steps) + src1.blue;
return getColor32(a, r, g, b);
}
/**
* Return the component parts of a color as an Object with the properties alpha, red, green, blue
*
* <p>Alpha will only be set if it exist in the given color (0xAARRGGBB)</p>
*
* @param color in RGB (0xRRGGBB) or ARGB format (0xAARRGGBB)
*
* @return Object with properties: alpha, red, green, blue
*/
public static function getRGB(color:uint):Object
{
var alpha:uint = color >>> 24;
var red:uint = color >> 16 & 0xFF;
var green:uint = color >> 8 & 0xFF;
var blue:uint = color & 0xFF;
return { alpha: alpha, red: red, green: green, blue: blue };
}
/**
* Given an alpha and 3 color values this will return an integer representation of it
*
* @param alpha The Alpha value (between 0 and 255)
* @param red The Red channel value (between 0 and 255)
* @param green The Green channel value (between 0 and 255)
* @param blue The Blue channel value (between 0 and 255)
*
* @return A native color value integer (format: 0xAARRGGBB)
*/
public static function getColor32(alpha:uint, red:uint, green:uint, blue:uint):uint
{
return alpha << 24 | red << 16 | green << 8 | blue;
}
public static function explode(object:FlxSprite, group:FlxGroup, portion:Number = 1, gibsize:int=4, rounded:Boolean=true):Vector.<FlxParticle>{
var gibs:Vector.<FlxParticle> = new Vector.<FlxParticle>()
var gib:FlxParticle;
for (var x:int = 0; x < object.framePixels.width; x += gibsize){
for (var y:int = 0; y < object.framePixels.height; y += gibsize){
if ((object.framePixels.getPixel32(x+gibsize/2,y+gibsize/2) >>> 24) > 0){
if (FlxG.random() < portion){
gib = group.recycle(FlxParticle) as FlxParticle;
if (gib.frameWidth != gibsize || gib.frameHeight != gibsize){
gib.makeGraphic(gibsize,gibsize,0,true);
}
gib.revive();
// _flashPoint.x = X;
// _flashPoint.y = Y;
// _flashRect2.width = bitmapData.width;
// _flashRect2.height = bitmapData.height;
// gib.framePixels.copyPixels(object._framePixels,rect,point,null,null,true);
gib.stamp(object, -x, -y);
if (rounded){
gib.framePixels.setPixel32(0,0,0);
gib.framePixels.setPixel32(0,gibsize-1,0);
gib.framePixels.setPixel32(gibsize-1,0,0);
gib.framePixels.setPixel32(gibsize-1,gibsize-1,0);
}
gibs.push(gib);
gib.elasticity = 0.5;
gib.lifespan = 7;
gib.x = object.x - object.offset.x + x;
gib.y = object.y - object.offset.y + y;
gib.acceleration.y = 900;
gib.velocity.x = FlxG.random()*80 - 40;
gib.velocity.y = -130-FlxG.random()*30;
}
}
}
}
return gibs;
}
/**
* Returns a roman numeral string
*/
public static function toRoman(n:int):String{
var s:String = ''
for (var i:int = 0; i < ROMAN_LETTERS.length; i ++){
var c:int = Math.floor(n / ROMAN_VALUES[i])
n -= c * ROMAN_VALUES[i];
while (c > 0){
s += ROMAN_LETTERS[i];
c --;
}
}
return s;
}
}
}