ActionScript 3 AS3 RGB颜色(对象)到HSB颜色(对象)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 AS3 RGB颜色(对象)到HSB颜色(对象)相关的知识,希望对你有一定的参考价值。

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

以上是关于ActionScript 3 AS3 RGB颜色(对象)到HSB颜色(对象)的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 AS3 RGB颜色到十六进制颜色(uint)

ActionScript 3 AS3 CSS颜色(字符串)到RGB颜色(对象)

ActionScript 3 AS3十六进制颜色(uint)到RGB颜色(对象)

ActionScript 3 AS3使用按位移位将RGB值转换为颜色

ActionScript 3 AS3 - RGB到Hex的RGB类

ActionScript 3 AS3 - HSL到RGB类