Hexart

Tuesday, February 2, 2010

Here's what it looks like.
What is it? I've create a site where you upload any image you want and it goes through one block of pixels at a time, evaluating each blocks median color hex code and prints that value to screen. You can adjust the font size and the font density. Go ahead give it a try!

Here's some code for ya!

I wrote this bitmap slicer class. It works really well, just pass it a bitmap and how many slices you want to slice it into. Each slice is the same aspect ratio of the original image. The parameter "pieces" is the square root of the total squares that there will be. The more square, the more memory is use.

public static function Slice(origBitmap:Bitmap, pieces:int):Array{
var BitmapArray:Array = new Array();

var recX:Number = 0;
var recY:Number = 0;
var recW:Number;
var recH:Number;
var aspctRatio:Number;
var destBitmap:Bitmap;
var totalSquares:Number;

if(origBitmap.height > origBitmap.width){
aspctRatio = origBitmap.width/origBitmap.height;
recH = Math.floor(origBitmap.height/pieces);
recW = Math.floor((origBitmap.height/pieces)*aspctRatio);
totalSquares = Math.floor(pieces * (origBitmap.width/recW));
}
else if(origBitmap.height < origBitmap.width){
aspctRatio = origBitmap.height/origBitmap.width;
recW = Math.floor(origBitmap.width/pieces);
recH = Math.floor((origBitmap.width/pieces)*aspctRatio);
totalSquares = pieces * Math.floor(origBitmap.height/recH);
}
else{
aspctRatio = 1;
recW = (origBitmap.width/pieces);
recH = recW;
totalSquares = pieces * (origBitmap.height/recH);
}


var xPos:Number;
var yPos:Number;
var colCounter:Number = 0;
var rowCounter:Number = 0;
for(var i:int = 0; i < totalSquares; i++){
destBitmap = new Bitmap(new BitmapData(recW, recH),PixelSnapping.NEVER, true);
destBitmap.bitmapData.copyPixels(origBitmap.bitmapData, new Rectangle(recX+(recW*colCounter), recY+(recH*rowCounter), recW, recH), new Point(0, 0));
BitmapArray.push(destBitmap);
destBitmap = null;
colCounter++;
if(colCounter >= pieces){
colCounter = 0;
rowCounter++;
}
}

return BitmapArray;
}


This finds the median color of each bitmap slice

//this finds the mean color for a bitmap data
public static function get_median_hex(bd:BitmapData):uint{
var pixelValue:uint;
var red:Number = 0;
var green:Number = 0;
var blue:Number = 0;
var count:Number = 0;

for (var i:int = 0; i < bd.height; i++){
for (var j:int = 0; j < bd.width; j++) {
pixelValue = bd.getPixel(j, i);

red += pixelValue >> 16 & 0xFF;
green += pixelValue >> 8 & 0xFF;
blue += pixelValue & 0xFF;
count++;

}
}
red /= count;
green /= count;
blue /= count;
return red << 16 | green << 8 | blue;
}

0 comments:

Post a Comment