AS3 顏色操作函式庫
阿新 • • 發佈:2019-02-04
package lib.base.maths { /** * 顏色轉換函式庫 * <p> * 作者:Brant.Hu<br> * 日期:2009<br> */ publicclass CColorMath extends Object { /** * RGB顏色互補色減法混合 * @param color1 * @param color2 * @return * <p> * <listing> * import lib.base.maths.CColorMath; * * var color1:uint = 0xFD6A2D; * var color2:uint = 0xA221A3; * var color3:uint = 0xFF0000; * * var circle1:Shape = new Shape(); * var circle2:Shape = new Shape(); * var circle3:Shape = new Shape(); * circle2.y = 100; * circle3.y = 200; * shapeRedraw(circle1, color1); * shapeRedraw(circle2, color2); * shapeRedraw(circle3, color3); * addChild(circle1); * addChild(circle2); * addChild(circle3); * * var btn:Sprite = new Sprite(); * btn.x = 200; * btn.y = 200; * shapeRedraw(btn, 0); * addChild(btn); * btn.addEventListener(MouseEvent.CLICK,onMouseClick); * function onMouseClick(evt:MouseEvent):void * { * color3= CColorMath.subTract(color1, color2); * shapeRedraw(circle3,color3); * } * * function shapeRedraw(shape:*,color:uint):void * { * shape.graphics.clear(); * shape.graphics.beginFill(color); * shape.graphics.drawRect(0,0, 100, 60); * shape.graphics.endFill(); * } * </listing> */ publicstatic function subTract(color1:uint, color2:uint):uint { vara:Array = toARGB(color1); varb:Array = toARGB(color2); varmr:uint = Math.max(Math.max(b[0] - (256 - a[0]), a[0] - (256 - b[0])), 0); varmg:uint = Math.max(Math.max(b[1] - (256 - a[1]), a[1] - (256 - b[1])), 0); varmb:uint = Math.max(Math.max(b[2] - (256 - a[2]), a[2] - (256 - b[2])), 0); returntoDec(mr, mg, mb); } /** * RGB顏色加法混合. 示例參考"GB顏色互補色減法混合"中的例子 * * @param color1 * @param color2 * @return */ publicstatic function sum(color1:uint, color2:uint):uint { vara:Array = toARGB(color1); varb:Array = toARGB(color2); varmr:uint = Math.min(a[0] + b[0], 255); varmg:uint = Math.min(a[1] + b[1], 255); varmb:uint = Math.min(a[2] + b[2], 255); returntoDec(mr, mg, mb); } /** * RGB顏色減法混合.示例參考"GB顏色互補色減法混合"中的例子 * * @param color1 * @param color2 * @return */ publicstatic function sub(color1:uint , color2:uint):uint { vara:Array = toARGB(color1); varb:Array = toARGB(color2); varmr:uint = Math.max(a[0] - b[0], 0); varmg:uint = Math.max(a[1] - b[1], 0); varmb:uint = Math.max(a[2] - b[2], 0); returntoDec(mr, mg, mb); } /** * 取兩個顏色的暗調.示例參考"GB顏色互補色減法混合"中的例子 * * @param color1 * @param color2 * @return */ publicstatic function min(color1:uint, color2:uint):uint { vara:Array = toARGB(color1); varb:Array = toARGB(color2); varmr:uint = Math.min(a[0], b[0]); varmg:uint = Math.min(a[1], b[1]); varmb:uint = Math.min(a[2], b[2]); returntoDec(mr, mg, mb); } /** * 取兩個顏色的明調.示例參考"GB顏色互補色減法混合"中的例子 * * @param color1 * @param color2 * @return */ publicstatic function max(color1:uint, color2:uint):uint { vara:Array = toARGB(color1); varb:Array = toARGB(color2); varmr:uint = Math.max(a[0], b[0]); varmg:uint = Math.max(a[1], b[1]); varmb:uint = Math.max(a[2], b[2]); returntoDec(mr, mg, mb); } /** * 拆分ARGB顏色,返回包含R,G,B,A通道值的陣列 * @param color 要分解的ARGB顏色數值 * @return * * <listing> * import lib.base.maths.CColorMath; * var color:uint = 0xFF80FFFF; * trace(CColorMath.toARGB(color));//128,255,255,255 * </listing> */ public static functiontoARGB(color:uint):Array { vara:uint = color >> 24 & 0xFF; varr:uint = color >> 16 & 0xFF; varg:uint = color >> 8 & 0xFF; varb:uint = color & 0xFF; return[r, g, b, a]; } /** * 合併A,R,G,B顏色通道 * @param r 紅色通道 * @param g 綠色通道 * @param b 藍色通道 * @param a 透明度 * @return */ publicstatic function toDec(r:uint, g:uint, b:uint, a:uint = 255):uint { varsa:uint = a << 24; varsr:uint = r << 16; varsg:uint = g << 8; returnsa | sr | sg | b; } /** * RGB顏色轉換為HSV顏色 * @param r 紅色通道 * @param g 綠色通道 * @param b 藍色通道 * @return * * H引數表示色彩資訊,即所處的光譜顏色的位置。該引數用一角度量來表示,紅、綠、藍分別相隔120度。互補色分別相差180 * 度。純度S為一比例值,範圍從0到1,它表示成所選顏色的純度和該顏色最大的純度之間的比率。S=0時,只有灰度。V表示色 * 彩的明亮程度,範圍從0到1。有一點要注意:它和光強度之間並沒有直接的聯絡.RGB轉化到HSV的演算法如下: * * <listing> * max=max(R,G,B) * min=min(R,G,B) * if R = max, H = (G-B)/(max-min) * if G = max, H = 2 + (B-R)/(max-min) * if B = max, H = 4 + (R-G)/(max-min) * * H = H * 60 * if H < 0, H = H + 360 * * V=max(R,G,B) * S=(max-min)/max * </listing> */ publicstatic function rToH(r:uint, g:uint, b:uint):Array { varh:Number = 0; varv:Number = 0; vars:Number = 0; varmax:uint = v = Math.max(Math.max(r, g), b); varmin:uint = Math.min(Math.min(r, g), b); varsub:uint = max - min; switch(max) { caser: h= (g - b) / sub; break; caseg: h= 2 + (b - r) / sub; break; caseb: h= 4 + (r - g) / sub; break; } if((h *= 60) < 0) h+= 360; s= sub / max; return[h, s, v]; } /** * HSV顏色轉換為RGB顏色 * @param h * @param s * @param v * @return * * HSV顏色到RGB顏色的轉化公式如下: * <listing> * if s = 0 * R=G=B=V * else * H /= 60; * i = INTEGER(H) * * f = H - i * a = V * ( 1 - s ) * b = V * ( 1 - s * f ) * c = V * ( 1 - s * (1 - f ) ) * * switch(i) * case 0: R = V; G = c; B = a; * case 1: R = b; G = v; B = a; * case 2: R = a; G = v; B = c; * case 3: R = a; G = b; B = v; * case 4: R = c; G = a; B = v; * case 5: R = v; G = a; B = b; * </listing> */ publicstatic function hToR(h:Number, s:Number, v:Number):Array { varr:uint; varg:uint; varb:uint; vari:uint; if(!s) r= g = b = v; else { h/= 60; i= uint(h); } varf:uint = h - i; var x:uint = v * (1 - s); vary:uint = v * (1 - s * f); varz:uint = v * (1 - s * (1 - f)); switch(i) { case0: r = v; g = z; b = x; break; case1: r = y; g = v; b = x; break; case2: r = x; g = v; b = z; break; case3: r = x; g = y; b = v; break; case4: r = z; g = x; b = v; break; case5: r = v; g = x; b = y; break; } return[r, g, b]; } } }