package com.mindfock.blog {

Color Mixer Flex App Updated

Posted: April 15th, 2009 | Author: John del Rosario | Filed under: AS 3.0, Art and Design, Experiments, Flash | Tags: , , , , | No Comments »

colormixer

I’ve updated my Color Mixer application. Right now it is as good as done. It’s not the best looking, but all the functionality is working fine. If there are any suggestions on how to improve it, I’d be glad to hear them.

I am still working on the AIR version. It’s taking a while figuring out how to skin components properly (the gradient sliders clearly show this).  And it seems that taking a screenshot of the desktop is not possible on AIR, which is a shame because I was really looking forward for the feature to take any color on screen. Unless I find a workaround for this, I’m going to have to leave that feature out. :(

I hope someone finds this useful, as I know I do. Here it is.

On different note, this post was posted from Windows Live Writer. I’m liking it very much. I hope everything displays fine.


RGB to Hexadecimal to HSV conversion in AS3

Posted: April 13th, 2009 | Author: John del Rosario | Filed under: AS 3.0, Programming and Development | Tags: , | 4 Comments »

In my previous post, I needed to do a lot of conversion between different representations of color. I think a post on how to convert color would be useful.

private function RGBToHex(r:uint, g:uint, b:uint):uint{
    var hex:uint = (r << 16 | g << 8 | b);
    return hex;
}

private function HexToRGB(hex:uint):Array{
    var rgb:Array = [];

    var r:uint = hex >> 16 & 0xFF;
    var g:uint = hex >> 8 & 0xFF;
    var b:uint = hex & 0xFF;

    rgb.push(r, g, b);
    return rgb;
}

private function RGBtoHSV(r:uint, g:uint, b:uint):Array{
    var max:uint = Math.max(r, g, b);
    var min:uint = Math.min(r, g, b);

    var hue:Number = 0;
    var saturation:Number = 0;
    var value:Number = 0;

    var hsv:Array = [];

    //get Hue
    if(max == min){
        hue = 0;
    }else if(max == r){
        hue = (60 * (g-b) / (max-min) + 360) % 360;
    }else if(max == g){
        hue = (60 * (b-r) / (max-min) + 120);
    }else if(max == b){
        hue = (60 * (r-g) / (max-min) + 240);
    }

    //get Value
    value = max;

    //get Saturation
    if(max == 0){
        saturation = 0;
    }else{
        saturation = (max - min) / max;
    }

    hsv = [Math.round(hue), Math.round(saturation * 100), Math.round(value / 255 * 100)];
    return hsv;

}

private function HSVtoRGB(h:Number, s:Number, v:Number):Array{
    var r:Number = 0;
    var g:Number = 0;
    var b:Number = 0;
    var rgb:Array = [];

    var tempS:Number = s / 100;
    var tempV:Number = v / 100;

    var hi:int = Math.floor(h/60) % 6;
    var f:Number = h/60 - Math.floor(h/60);
    var p:Number = (tempV * (1 - tempS));
    var q:Number = (tempV * (1 - f * tempS));
    var t:Number = (tempV * (1 - (1 - f) * tempS));

    switch(hi){
        case 0: r = tempV; g = t; b = p; break;
        case 1: r = q; g = tempV; b = p; break;
        case 2: r = p; g = tempV; b = t; break;
        case 3: r = p; g = q; b = tempV; break;
        case 4: r = t; g = p; b = tempV; break;
        case 5: r = tempV; g = p; b = q; break;
    }

    rgb = [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
    return rgb;
}

The code above requires and returns:

  • RGB values between 0 to 255
  • Hexadecimal in RRGGBB format
  • Hue values between 0 to 360
  • Saturation and Value between 0 to 100

Notice that the “middleman” is RGB.

And, just in case you only have the Hexadecimal string to work with, here is the code to convert that string into a usable decimal value.

private function HexToDeci(hex:String):uint{
	var deci:uint = 0;
	var power:uint = hex.length - 1;
	for(var i:uint = 0; i < hex.length; i++){
		var char:String = hex.charAt(i);
		if(parseInt(char) >= 0 &&  parseInt(char) <= 9){
			deci += parseInt(char) * Math.pow(16, power);
		}else{
			switch(char){
				case "A": case "a":	deci += 10 * Math.pow(16, power); break;
				case "B": case "b":	deci += 11 * Math.pow(16, power); break;
				case "C": case "c":	deci += 12 * Math.pow(16, power); break;
				case "D": case "d":	deci += 13 * Math.pow(16, power); break;
				case "E": case "e":	deci += 14 * Math.pow(16, power); break;
				case "F": case "f":	deci += 15 * Math.pow(16, power); break;
			}
		}

		power --;
	}
	return deci;
}

EDIT: I’ve just gathered that there is a much much simpler way to convert a Hexadecimal string into a usable number. I feel kind of stupid right now. :P

private function HexToDeci(hex:String):uint{
        if (hex.substr(0, 2) != "0x") {
                hex = "0x" + hex;
        }
        return new uint(hex);
}

Basic (twitchy) Motion Tracking

Posted: April 4th, 2009 | Author: John del Rosario | Filed under: AS 3.0, Experiments | Tags: | No Comments »

I’ve put on hold reading “Making Things Move”, as the remaining chapters were too much for my brain to handle. I decided to try out some cool webcam experiments. Augmented Reality is obviously out of my reach right now (but definitely in my sights), I decided to try out motion tracking.

It is very twitchy and erratic, and it’s not very accurate neither, but it works.

Most of the concepts are explained in detail here and there.

My next step would be to track something of a specific color (a marker). Shouldn’t be too hard – I hope.

CLICK TO OPEN MOVIE. Press SPACE to toggle between display modes.


Metaballs update

Posted: January 3rd, 2009 | Author: John del Rosario | Filed under: AS 3.0, Experiments, Programming and Development | Tags: , | 1 Comment »

Still delving into metaballs. I implemented the suggestions in this page for optimizing, and the results are good. I could even animate it – just barely though, at ~15 FPS on my browser. :)  Again, most of this is just copy & paste from the source provided in the tutorial.

Metaballs – move your mouse around.  Click to change the target ball.

Still looking for a more elegant solution for Flash. This is done on a very small stage size, with only 4 balls. I want to do a full screen version, with at least 10 balls moving around. :P


Simple Metaballs

Posted: January 2nd, 2009 | Author: John del Rosario | Filed under: AS 3.0, Experiments, Programming and Development | Tags: | 1 Comment »

I stumbled upon Metaballs yesterday, and decided it would be cool to try it out. I’ve seen some very nice examples done in Flash, but no luck finding their sources.

After Googling around, I found this tutorial on Metaballs and computer graphics.

I ported the code into AS3, but the result is not very impressive. It is very slow and processor heavy. If you click on the stage, the last Metaball added will be moved, and as you can see, it takes a bit of time to redraw them. I’m not even thinking about animating this. :P

Metaballs

I’m still looking for a more elegant solution for Flash, hopefully one that uses the Vector-based drawing API of Flash, and not BitmapData.

UPDATE: I have an updated version here. Also, I linked the image above to the updated one. For comparison, the old one is here.


Nice source-codes from 25-lines AS contest

Posted: December 13th, 2008 | Author: John del Rosario | Filed under: AS 3.0, Random, Web | Tags: , | No Comments »

I missed it by a few days, but the 25-Line ActionScript contest has just ended, and 12 finalists and their works have been put up. All of them are very impressive and cool. Check it out and vote for your favorite!
I especially liked these:
http://www.25lines.com/finalists/0812/073.swf
http://www.25lines.com/finalists/0812/043.swf
http://www.25lines.com/finalists/0812/037.swf
http://www.25lines.com/finalists/0812/034.swf


ActionSnippet – AS3 tips and tricks

Posted: November 6th, 2008 | Author: John del Rosario | Filed under: AS 3.0 | Tags: , | No Comments »

I’ve bookmarked this site. You should too! There are some very useful and helpful tips in there. Like the latest one, as of this writing, about simplifying multiple method calls to a graphic (or any) object.

From this:

mc.graphics.lineStyle(0);
mc.graphics.moveTo(10, 10);
mc.graphics.lineTo(20,10);
//...

To this:

with (mc.graphics) {
    lineStyle(0);
    moveTo(10, 10);
    lineTo(20,10);
    //...
}

Much more readable and easier to type. :)

Check it out here and bookmark it.