Return to Snippet

Revision: 40914
at February 9, 2011 22:38 by adrianparr


Initial Code
var darkGreenRgbObj:Object = new Object();
darkGreenRgbObj.r = 51;
darkGreenRgbObj.g = 102;
darkGreenRgbObj.b = 0;
var darkGreenHsbObj:Object = RGBtoHSB(darkGreenRgbObj.r, darkGreenRgbObj.g, darkGreenRgbObj.b);
trace("RGB: " + darkGreenRgbObj.r + ", " + darkGreenRgbObj.g + ", " + darkGreenRgbObj.b);
trace("HSB: " + darkGreenHsbObj.h + ", " + darkGreenHsbObj.s + ", " + darkGreenHsbObj.b);

function RGBtoHSB(r:int, g:int, b:int):Object
{
	var hsb:Object = new Object;
	var _max:Number = Math.max(r,g,b);
	var _min:Number = Math.min(r,g,b);
	
	hsb.s = (_max != 0) ? (_max - _min) / _max * 100: 0;
	hsb.b = _max / 255 * 100;

	if(hsb.s == 0){
		hsb.h = 0;
	}else{
		switch(_max)
		{
			case r:
				hsb.h = (g - b)/(_max - _min)*60 + 0;
				break;
			case g:
				hsb.h = (b - r)/(_max - _min)*60 + 120;
				break;
			case b:
				hsb.h = (r - g)/(_max - _min)*60 + 240;
				break;
		}
	}
	
	hsb.h = Math.min(360, Math.max(0, Math.round(hsb.h)))
	hsb.s = Math.min(100, Math.max(0, Math.round(hsb.s)))
	hsb.b = Math.min(100, Math.max(0, Math.round(hsb.b)))
	
	return hsb;
}

// OUTPUT
// RGB: 51, 102, 0
// HSB: 90, 100, 40

Initial URL

                                

Initial Description

                                

Initial Title
AS3 RGB Colours (object) to HSB Colours (Object)

Initial Tags
color, convert

Initial Language
ActionScript 3