Return to Snippet

Revision: 40915
at February 9, 2011 22:42 by adrianparr


Initial Code
var darkGreenHsbObj:Object = new Object();
darkGreenHsbObj.h = 90;
darkGreenHsbObj.s = 100;
darkGreenHsbObj.b = 40;
var darkGreenRgbObj:Object = HSBtoRGB(darkGreenHsbObj.h, darkGreenHsbObj.s, darkGreenHsbObj.b);
trace("HSB: " + darkGreenHsbObj.h + ", " + darkGreenHsbObj.s + ", " + darkGreenHsbObj.b);
trace("RGB: " + darkGreenRgbObj.r + ", " + darkGreenRgbObj.g + ", " + darkGreenRgbObj.b);

function HSBtoRGB(h:int, s:int, b:int):Object
{
	var rgb:Object = new Object();
	var max:Number = (b * 0.01) * 255;
	var min:Number = max * (1 - (s * 0.01));
	
	if (h == 360) {
		h = 0;
	}
	
	if (s == 0) {
		rgb.r = rgb.g = rgb.b = b*(255*0.01) ;
	} else {
		var _h:Number = Math.floor(h / 60);
		
		switch(_h){
			case 0:
				rgb.r = max	;
				rgb.g = min+h * (max-min)/ 60;
				rgb.b = min;
				break;
			case 1:
				rgb.r = max-(h-60) * (max-min)/60;
				rgb.g = max;
				rgb.b = min;
				break;
			case 2:
				rgb.r = min ;
				rgb.g = max;
				rgb.b = min+(h-120) * (max-min)/60;
				break;
			case 3:
				rgb.r = min;
				rgb.g = max-(h-180) * (max-min)/60;
				rgb.b =max;
				break;
			case 4:
				rgb.r = min+(h-240) * (max-min)/60;
				rgb.g = min;
				rgb.b = max;
				break;
			case 5:
				rgb.r = max;
				rgb.g = min;
				rgb.b = max-(h-300) * (max-min)/60;
				break;
			case 6:
				rgb.r = max;
				rgb.g = min+h  * (max-min)/ 60;
				rgb.b = min;
				break;
		}

		rgb.r = Math.min(255, Math.max(0, Math.round(rgb.r)))
		rgb.g = Math.min(255, Math.max(0, Math.round(rgb.g)))
		rgb.b = Math.min(255, Math.max(0, Math.round(rgb.b)))
	}
	return rgb;
}

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

Initial URL

                                

Initial Description

                                

Initial Title
AS3 HSB Colour (Object) to RGB Colour (Object)

Initial Tags
color, convert

Initial Language
ActionScript 3