AS3两种颜色之间的平均颜色
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AS3两种颜色之间的平均颜色相关的知识,希望对你有一定的参考价值。
Full credit for this code goes to Scratchbrain (http://www.scratchbrain.net/blog/ver2/)
package net.scratchbrain.color { import net.scratchbrain.color.AverageColor; public class AverageColor { public static function averageHex(_hex1:uint,_hex2:uint):uint { var rgb1:Object = ConvertColor.HexToRGB(_hex1); var rgb2:Object = ConvertColor.HexToRGB(_hex2); var hsb1:Object = ConvertColor.RGBToHSB(rgb1.r, rgb1.g, rgb1.b); var hsb2:Object = ConvertColor.RGBToHSB(rgb2.r, rgb2.g, rgb2.b); return averageHsb(hsb1.h, hsb1.s, hsb1.b, hsb2.h, hsb2.s, hsb2.b); } public static function averageRgb(_r1:int,_g1:int,_b1:int,_r2:int,_g2:int,_b2:int):uint { var hsb1:Object = ConvertColor.RGBToHSB(_r1, _g1, _b1); var hsb2:Object = ConvertColor.RGBToHSB(_r2, _g2, _b2); return averageHsb(hsb1.h, hsb1.s, hsb1.b, hsb2.h, hsb2.s, hsb2.b); } public static function averageHsb(_h1:int, _s1:int, _b1:int, _h2:int, _s2:int, _b2:int):uint { var _h:int; var _s:int; var _b:int; if(_h1 <= 18 && _s1 != 0 && _h2 >= 180){ _h1 = 360; } if(_h2 <= 18 && _s2 != 0 && _h1 >= 180){ _h2 = 360; } if(_s1 != 0 && _s2 != 0){ _h = (_h1 + _h2)/2; _s = (_s1 + _s2)/2; _b = (_b1 + _b2)/2; }else if(_s1 == 0 && _s2 == 0){ _h = 0; _s = 0; _b = (_b1 + _b2)/2; }else{ _h = (_h1 != 0) ? _h1 : _h2; _s = (_s1 + _s2)/2; _b = (_b1 + _b2)/2; } var rgb:Object = ConvertColor.HSBToRGB(_h, _s, _b); return ConvertColor.RGBToHex(rgb.r, rgb.g, rgb.b); } } } package net.scratchbrain.color { public class ConvertColor { public static function RGBToHex(r:int,g:int,b:int):uint { var hex:uint = r<<16 | g<<8 | b; return hex; } public static function HexToRGB(value:uint):Object{ var rgb:Object = new Object(); rgb.r = (value >> 16) & 0xFF rgb.g = (value >> 8) & 0xFF rgb.b = value & 0xFF return rgb; } public static 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; } public static 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; } } }
以上是关于AS3两种颜色之间的平均颜色的主要内容,如果未能解决你的问题,请参考以下文章