Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Find Average RGB Colors
private function averageRed(src : BitmapData, accuracy : Number = 0.01, region : Rectangle = null) : int {
return averageColorProperty( src, region, accuracy, 'r' );
}
private function averageGreen(src : BitmapData, accuracy : Number = 0.01, region : Rectangle = null) : int {
return averageColorProperty( src, region, accuracy, 'g' );
}
private function averageBlue(src : BitmapData, accuracy : Number = 0.01, region : Rectangle = null) : int {
return averageColorProperty( src, region, accuracy, 'b' );
}
private function averageColorProperty(src : BitmapData, region : Rectangle = null, accuracy : Number = 0.01, colorspace : String = 'l') : Number {
if(!region) {
region = new Rectangle( 0, 0, src.width, src.height );
}
var offset : int = 1 / accuracy;
var total : int;
var count : int;
// loop thru x/y pixels by offset
for (var i : int = region.x; i < (region.x + region.width) ; i += offset) {
for (var j : int = region.y; j < (region.y + region.height) ; j += offset) {
var hex : uint = getColor( src, i, j );
var obj : Object = componentsFromHex( hex );
if(colorspace == "h" || colorspace == "s" || colorspace == "l") {
var hsl : Object = RGBtoHSL( obj.r, obj.g, obj.b );
obj = hsl;
}
if(colorspace == "v") {
var hsv : Object = RGBtoHSV( obj.r, obj.g, obj.b );
obj = hsv;
}
total += obj[colorspace];
count++;
}
}
return total / count;
}
private function RGBtoHSV(red : Number, green : Number, blue : Number) : Object {
var min : Number, max : Number, s : Number, v : Number, h : Number = 0;
max = Math.max( red, Math.max( green, blue ) );
min = Math.min( red, Math.min( green, blue ) );
if (max == 0) {
return {h:0, s:0, v:0};
}
v = max;
s = (max - min) / max;
h = RGBToHue( red, green, blue );
return {h:h, s:s, v:v};
}
private function center(a : Number, b : Number, c : Number) : Number {
if ((a > b) && (a > c)) {
if (b > c) return b;
else return c;
} else if ((b > a) && (b > c)) {
if (a > c) return a;
else return c;
} else if (a > b) {
return a;
} else {
return b;
}
}
private function RGBToHue(red : Number, green : Number, blue : Number) : uint {
var f : Number, min : Number, mid : Number, max : Number, n : Number;
max = Math.max( red, Math.max( green, blue ) );
min = Math.min( red, Math.min( green, blue ) );
// achromatic case
if (max - min == 0) {
return 0;
}
mid = center( red, green, blue );
// using this loop to avoid super-ugly nested ifs
while (true) {
if (red == max) {
if (blue == min) n = 0; else n = 5;
break;
}
if (green == max) {
if (blue == min) n = 1; else n = 2;
break;
}
if (red == min) n = 3; else n = 4;
break;
}
if ((n % 2) == 0) {
f = mid - min;
} else {
f = max - mid;
}
f = f / (max - min);
return 60 * (n + f);
}
private function RGBtoHSL(red : Number, green : Number, blue : Number) : Object {
var min : Number, max : Number, delta : Number, l : Number, s : Number, h : Number = 0;
max = Math.max( red, Math.max( green, blue ) );
min = Math.min( red, Math.min( green, blue ) );
//l = (min + max) / 2;
l = (min + max) * 0.5;
// L
if (l == 0) {
return {h:h, l:0, s:1};
}
//delta = (max - min) / 2;
delta = (max - min) * 0.5;
if (l < 0.5) {
// S
s = delta / l;
} else {
s = delta / (1 - l);
}
// H
h = RGBToHue( red, green, blue );
return {h:h, l:l, s:s};
}
private function componentsFromHex(hex : uint) : Object {
var r : Number = hex >> 16 & 0xff;
var g : Number = hex >> 8 & 0xff;
var b : Number = hex & 0xff;
return {r:r, g:g, b:b};
}
private function getColor(src : BitmapData, x : uint = 0, y : uint = 0, getAlpha : Boolean = false) : uint {
var w : Number = src.width;
var h : Number =src.height;
var bmp : BitmapData = new BitmapData( w, h );
bmp.lock( );
bmp.draw( src );
var color : uint = (!getAlpha) ? bmp.getPixel( int( x ), int( y ) ) : bmp.getPixel32( int( x ), int( y ) );
bmp.unlock( );
bmp.dispose( );
return color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment