“FlashPlatform Algorithm RGB2HSL”的版本间的差异
来自Blueidea
(未显示同一用户的1个中间版本) | |||
第2行: | 第2行: | ||
==RGBColor== | ==RGBColor== | ||
+ | RGB色彩模型。 | ||
<syntaxhighlight lang="actionscript"> | <syntaxhighlight lang="actionscript"> | ||
package starpulse.geom { | package starpulse.geom { | ||
第175行: | 第176行: | ||
public function toString32():String { | public function toString32():String { | ||
return "[0x" + value32.toString(16).toUpperCase() + "]"; | return "[0x" + value32.toString(16).toUpperCase() + "]"; | ||
+ | } | ||
+ | } // <- end class -> | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | ==HSLColor== | ||
+ | HSL色彩模型。 | ||
+ | <syntaxhighlight lang="actionscript"> | ||
+ | package starpulse.geom { | ||
+ | /** | ||
+ | * RGB色彩。 | ||
+ | */ | ||
+ | public class HSLColor { | ||
+ | //========================================================================== | ||
+ | // Constructor | ||
+ | //========================================================================== | ||
+ | /** | ||
+ | * HSL色彩。 | ||
+ | * @param h Number - hue value [0-360] | ||
+ | * @param s Number - saturation value [0-100] | ||
+ | * @param v Number - brightness value [0-100] | ||
+ | */ | ||
+ | public function HSLColor(h:uint = 360, s:uint = 100, l:uint = 100) { | ||
+ | $h = h; | ||
+ | $s = s; | ||
+ | $l = l; | ||
+ | } | ||
+ | //========================================================================== | ||
+ | // Properties | ||
+ | //========================================================================== | ||
+ | private var $h:uint; | ||
+ | private var $s:uint; | ||
+ | private var $l:uint; | ||
+ | //========================================================================== | ||
+ | // Public accessors | ||
+ | //========================================================================== | ||
+ | /** | ||
+ | * @private | ||
+ | */ | ||
+ | public function set hue(value:uint):void { | ||
+ | $h = value; | ||
+ | } | ||
+ | /** | ||
+ | * 色相。 | ||
+ | */ | ||
+ | public function get hue():uint { | ||
+ | return $h; | ||
+ | } | ||
+ | /** | ||
+ | * @private | ||
+ | */ | ||
+ | public function set saturation(value:uint):void { | ||
+ | $s = value; | ||
+ | } | ||
+ | /** | ||
+ | * 飽和度。 | ||
+ | */ | ||
+ | public function get saturation():uint { | ||
+ | return $s; | ||
+ | } | ||
+ | /** | ||
+ | * @private | ||
+ | */ | ||
+ | public function set lightness(value:uint):void { | ||
+ | $l = value; | ||
+ | } | ||
+ | /** | ||
+ | * 亮度。 | ||
+ | */ | ||
+ | public function get lightness():uint { | ||
+ | return $l; | ||
+ | } | ||
+ | //========================================================================== | ||
+ | // Public methods | ||
+ | //========================================================================== | ||
+ | public function toString():String { | ||
+ | return "[H:" + hue + " S:" + saturation + " L:" + lightness + "]"; | ||
+ | } | ||
+ | } // <- end class -> | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==ColorFormatTransform== | ||
+ | 色彩模型轉換器。 | ||
+ | <syntaxhighlight lang="actionscript"> | ||
+ | package starpulse.geom { | ||
+ | /** | ||
+ | * 顏色格式轉換器。 | ||
+ | */ | ||
+ | public class ColorFormatTransform { | ||
+ | private static const FF:uint = 255; | ||
+ | /** | ||
+ | * RGB色彩到HSL色彩轉換。 | ||
+ | * @param rgb RGB色彩對象。 | ||
+ | * @return HSL色彩對象。 | ||
+ | */ | ||
+ | public static function rgb2hsl(rgb:RGBColor, overwrite:HSLColor = null):HSLColor { | ||
+ | const R:Number = rgb.red / 255; | ||
+ | const G:Number = rgb.green / 255; | ||
+ | const B:Number = rgb.blue / 255; | ||
+ | const MIN:Number = Math.min(R, G, B); | ||
+ | const MAX:Number = Math.max(R, G, B); | ||
+ | const DELTA_MAX:Number = MAX - MIN; | ||
+ | const L:Number = MAX; | ||
+ | var hue:Number; | ||
+ | if(DELTA_MAX == 0) { | ||
+ | if(overwrite) { | ||
+ | overwrite.hue = 0; | ||
+ | overwrite.saturation = 0; | ||
+ | overwrite.lightness = L * 100; | ||
+ | return overwrite; | ||
+ | } else { | ||
+ | return new HSLColor(0, 0, L * 100); | ||
+ | } | ||
+ | } | ||
+ | const S:Number = DELTA_MAX / MAX; | ||
+ | const DELTA_R:Number = (((MAX - R ) / 6) + (DELTA_MAX / 2)) / DELTA_MAX | ||
+ | const DELTA_G:Number = (((MAX - G ) / 6) + (DELTA_MAX / 2)) / DELTA_MAX | ||
+ | const DELTA_B:Number = (((MAX - B ) / 6) + (DELTA_MAX / 2)) / DELTA_MAX | ||
+ | if(R == MAX) { | ||
+ | hue = DELTA_B - DELTA_G; | ||
+ | } else if(G == MAX) { | ||
+ | hue = ( 1 / 3 ) + DELTA_R - DELTA_B; | ||
+ | } else if(B == MAX) { | ||
+ | hue = ( 2 / 3 ) + DELTA_G - DELTA_R; | ||
+ | } | ||
+ | hue = hue < 0 ? hue + 1 : hue > 1 ? hue - 1 : hue; | ||
+ | if(overwrite) { | ||
+ | overwrite.hue = hue * 360; | ||
+ | overwrite.saturation = S * 100; | ||
+ | overwrite.lightness = L * 100; | ||
+ | return overwrite; | ||
+ | } else { | ||
+ | return new HSLColor(hue * 360, S * 100, L * 100); | ||
+ | } | ||
+ | } | ||
+ | /** | ||
+ | * HSL色彩到RGB色彩轉換。 | ||
+ | * @param hsl HSL色彩對象。 | ||
+ | * @return RGB色彩對象。 | ||
+ | */ | ||
+ | public static function hsl2rgb(hsl:HSLColor, overwrite:RGBColor = null):RGBColor { | ||
+ | const H:Number = hsl.hue / 360; | ||
+ | const S:Number = hsl.saturation / 100; | ||
+ | const L:Number = hsl.lightness / 100; | ||
+ | var r:uint; | ||
+ | var g:uint; | ||
+ | var b:uint; | ||
+ | if(S == 0) { | ||
+ | if(overwrite) { | ||
+ | overwrite.red = L * FF; | ||
+ | overwrite.green = L * FF; | ||
+ | overwrite.blue = L * FF; | ||
+ | return overwrite; | ||
+ | } else { | ||
+ | return new RGBColor(L * FF, L * FF, L * FF); | ||
+ | } | ||
+ | } else { | ||
+ | const VH:Number = H == 1 ? 0 : H * 6; | ||
+ | const VI:uint = int(VH); | ||
+ | const A:Number = L * (1 - S); | ||
+ | const B:Number = L * (1 - S * (VH - VI)); | ||
+ | const C:Number = L * (1 - S * (1 - (VH - VI))); | ||
+ | if(VI == 0) { | ||
+ | r = L * FF; g = C * FF; b = A * FF; | ||
+ | } else if (VI == 1) { | ||
+ | r = B * FF; g = L * FF; b = A * FF; | ||
+ | } else if (VI == 2) { | ||
+ | r = A * FF; g = L * FF; b = C * FF; | ||
+ | } else if (VI == 3) { | ||
+ | r = A * FF; g = B * FF; b = L * FF; | ||
+ | } else if (VI == 4) { | ||
+ | r = C * FF; g = A * FF; b = L * FF; | ||
+ | } else { | ||
+ | r = L * FF; g = A * FF; b = B * FF; | ||
+ | } | ||
+ | if(overwrite) { | ||
+ | overwrite.red = r; | ||
+ | overwrite.green = g; | ||
+ | overwrite.blue = b; | ||
+ | return overwrite; | ||
+ | } else { | ||
+ | return new RGBColor(r, g, b); | ||
+ | } | ||
+ | } | ||
} | } | ||
} // <- end class -> | } // <- end class -> | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
2011-04-14T03:58:55的最后版本
RGBColor
RGB色彩模型。
package starpulse.geom { /** * RGB色彩。 */ public class RGBColor { //========================================================================== // Constructor //========================================================================== /** * RGB色彩。 * @param r Number - red value [0-255] * @param g Number - green value [0-255] * @param b Number - blue value [0-255] */ public function RGBColor(r:uint = 255, g:uint = 255, b:uint = 255, a:uint = 255) { $a = a; $r = r; $g = g; $b = b; } //========================================================================== // Properties //========================================================================== private var $a:uint; private var $r:uint; private var $g:uint; private var $b:uint; //========================================================================== // Public accessors //========================================================================== /** * @private */ public function set alpha(value:uint):void { $a = value; } /** * 透明通道。 */ public function get alpha():uint { return $a; } /** * @private */ public function set red(value:uint):void { $r = value; } /** * 紅色通道。 */ public function get red():uint { return $r; } /** * @private */ public function set green(value:uint):void { $g = value; } /** * 綠色通道。 */ public function get green():uint { return $g; } /** * @private */ public function set blue(value:uint):void { $b = value; } /** * 蓝色通道。 */ public function get blue():uint { return $b; } /** * @private */ public function set value(v:uint):void { $a = 255; $r = (v >>> 16 << 16) >>> 16; $g = ((v >>> 8 << 8) - ($r << 16)) >>> 8; $b = ((v >>> 0 << 0) - ($r << 16) - ($g << 8)) >>> 0; } /** * 無透明通道顏色值。 */ public function get value():uint { const R1:uint = $r >= 16 ? $r / 16 : 0; const R2:uint = $r >= 16 ? $r % 16 : $r; const G1:uint = $g >= 16 ? $g / 16 : 0; const G2:uint = $g >= 16 ? $g % 16 : $g; const B1:uint = $b >= 16 ? $b / 16 : 0; const B2:uint = $b >= 16 ? $b % 16 : $b; return (R1 << 20) + (R2 << 16) + (G1 << 12) + (G2 << 8) + (B1 << 4) + (B2 << 0); } /** * @private */ public function set value32(v:uint):void { $a = v >>> 24; $r = ((v >>> 16 << 16) - ($a << 24)) >>> 16; $g = ((v >>> 8 << 8) - ($a << 24) - ($r << 16)) >>> 8; $b = ((v >>> 0 << 0) - ($a << 24) - ($r << 16) - ($g << 8)) >>> 0; } /** * 32位4通道顏色值。 */ public function get value32():uint { const A1:uint = $a >= 16 ? $a / 16 : 0; const A2:uint = $a >= 16 ? $a % 16 : $a; const R1:uint = $r >= 16 ? $r / 16 : 0; const R2:uint = $r >= 16 ? $r % 16 : $r; const G1:uint = $g >= 16 ? $g / 16 : 0; const G2:uint = $g >= 16 ? $g % 16 : $g; const B1:uint = $b >= 16 ? $b / 16 : 0; const B2:uint = $b >= 16 ? $b % 16 : $b; return (A1 << 28) + (A2 << 24) + (R1 << 20) + (R2 << 16) + (G1 << 12) + (G2 << 8) + (B1 << 4) + (B2 << 0); } //========================================================================== // Public methods //========================================================================== /** * 設定顏色值為字符串所表示的無透明通道顏色值。<br/> * 例如: * <table> * <tr><th>参数</th></tr> * <tr><td>from("0xFFCC00");</td></tr> * <tr><td>from("23564");</td></tr> * </table> * @param value 表示數值的字符串。 */ public function from(value:String):void { const V:int = int(new Number(value)); $a = 255; $r = (V >>> 16 << 16) >>> 16; $g = ((V >>> 8 << 8) - ($r << 16)) >>> 8; $b = ((V >>> 0 << 0) - ($r << 16) - ($g << 8)) >>> 0; } /** * 設定顏色值為字符串所表示的32位4通道顏色值。<br/> * 例如: * <table> * <tr><th>参数</th></tr> * <tr><td>from32("0xFFFFCC00");</td></tr> * <tr><td>from32("3323564");</td></tr> * </table> * @param value 表示數值的字符串。 */ public function from32(value:String):void { const V:int = int(new Number(value)); $a = V >>> 24; $r = ((V >>> 16 << 16) - ($a << 24)) >>> 16; $g = ((V >>> 8 << 8) - ($a << 24) - ($r << 16)) >>> 8; $b = ((V >>> 0 << 0) - ($a << 24) - ($r << 16) - ($g << 8)) >>> 0; } /** * 無透明通道顏色值的16進制表示。 */ public function toString():String { return "[0x" + value.toString(16).toUpperCase() + "]"; } /** * 32位4通道顏色值的16進制表示。 */ public function toString32():String { return "[0x" + value32.toString(16).toUpperCase() + "]"; } } // <- end class -> }
HSLColor
HSL色彩模型。
package starpulse.geom { /** * RGB色彩。 */ public class HSLColor { //========================================================================== // Constructor //========================================================================== /** * HSL色彩。 * @param h Number - hue value [0-360] * @param s Number - saturation value [0-100] * @param v Number - brightness value [0-100] */ public function HSLColor(h:uint = 360, s:uint = 100, l:uint = 100) { $h = h; $s = s; $l = l; } //========================================================================== // Properties //========================================================================== private var $h:uint; private var $s:uint; private var $l:uint; //========================================================================== // Public accessors //========================================================================== /** * @private */ public function set hue(value:uint):void { $h = value; } /** * 色相。 */ public function get hue():uint { return $h; } /** * @private */ public function set saturation(value:uint):void { $s = value; } /** * 飽和度。 */ public function get saturation():uint { return $s; } /** * @private */ public function set lightness(value:uint):void { $l = value; } /** * 亮度。 */ public function get lightness():uint { return $l; } //========================================================================== // Public methods //========================================================================== public function toString():String { return "[H:" + hue + " S:" + saturation + " L:" + lightness + "]"; } } // <- end class -> }
ColorFormatTransform
色彩模型轉換器。
package starpulse.geom { /** * 顏色格式轉換器。 */ public class ColorFormatTransform { private static const FF:uint = 255; /** * RGB色彩到HSL色彩轉換。 * @param rgb RGB色彩對象。 * @return HSL色彩對象。 */ public static function rgb2hsl(rgb:RGBColor, overwrite:HSLColor = null):HSLColor { const R:Number = rgb.red / 255; const G:Number = rgb.green / 255; const B:Number = rgb.blue / 255; const MIN:Number = Math.min(R, G, B); const MAX:Number = Math.max(R, G, B); const DELTA_MAX:Number = MAX - MIN; const L:Number = MAX; var hue:Number; if(DELTA_MAX == 0) { if(overwrite) { overwrite.hue = 0; overwrite.saturation = 0; overwrite.lightness = L * 100; return overwrite; } else { return new HSLColor(0, 0, L * 100); } } const S:Number = DELTA_MAX / MAX; const DELTA_R:Number = (((MAX - R ) / 6) + (DELTA_MAX / 2)) / DELTA_MAX const DELTA_G:Number = (((MAX - G ) / 6) + (DELTA_MAX / 2)) / DELTA_MAX const DELTA_B:Number = (((MAX - B ) / 6) + (DELTA_MAX / 2)) / DELTA_MAX if(R == MAX) { hue = DELTA_B - DELTA_G; } else if(G == MAX) { hue = ( 1 / 3 ) + DELTA_R - DELTA_B; } else if(B == MAX) { hue = ( 2 / 3 ) + DELTA_G - DELTA_R; } hue = hue < 0 ? hue + 1 : hue > 1 ? hue - 1 : hue; if(overwrite) { overwrite.hue = hue * 360; overwrite.saturation = S * 100; overwrite.lightness = L * 100; return overwrite; } else { return new HSLColor(hue * 360, S * 100, L * 100); } } /** * HSL色彩到RGB色彩轉換。 * @param hsl HSL色彩對象。 * @return RGB色彩對象。 */ public static function hsl2rgb(hsl:HSLColor, overwrite:RGBColor = null):RGBColor { const H:Number = hsl.hue / 360; const S:Number = hsl.saturation / 100; const L:Number = hsl.lightness / 100; var r:uint; var g:uint; var b:uint; if(S == 0) { if(overwrite) { overwrite.red = L * FF; overwrite.green = L * FF; overwrite.blue = L * FF; return overwrite; } else { return new RGBColor(L * FF, L * FF, L * FF); } } else { const VH:Number = H == 1 ? 0 : H * 6; const VI:uint = int(VH); const A:Number = L * (1 - S); const B:Number = L * (1 - S * (VH - VI)); const C:Number = L * (1 - S * (1 - (VH - VI))); if(VI == 0) { r = L * FF; g = C * FF; b = A * FF; } else if (VI == 1) { r = B * FF; g = L * FF; b = A * FF; } else if (VI == 2) { r = A * FF; g = L * FF; b = C * FF; } else if (VI == 3) { r = A * FF; g = B * FF; b = L * FF; } else if (VI == 4) { r = C * FF; g = A * FF; b = L * FF; } else { r = L * FF; g = A * FF; b = B * FF; } if(overwrite) { overwrite.red = r; overwrite.green = g; overwrite.blue = b; return overwrite; } else { return new RGBColor(r, g, b); } } } } // <- end class -> }