ColorPickUper类--算出图片的主要色调
/* * Copyright(c) 2007 Muraken <muraken.biz> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ /** * ColorPickUper * * BitmapDataに主に使用されている色のuintをいい感じに減色しつつ配列で返してくれるクラス * @usage * <code> * import biz.muraken.ColorPickUper; * * var cp:ColoerPickUper = new ColorPickUper(); * trace(cp.pickUp(_bmd)); * * </code> * @author むらけん[http://www.muraken.biz/] * @since Flash Player 9 (ActionScript 3.0) * @version 0.5 * @history 2007.12.17 作成 * 2007.12.19 アプローチを若干修正 * 2007.12.20 19日の修正によるスペルミスを発見、修正。 * 2007.12.21 ループ周りの最適化 * 2007.12.21 微調整 * 2007.12.22 微調整 */ package biz.muraken{ import flash.display.BitmapData; public class ColorPickUper{ //___________________________________________________________________________________________________________________________________ // vars private var setNum:uint; private var difColor:uint = 10; //___________________________________________________________________________________________________________________________________ // init public function ColorPickUper(quality:uint = 2):void{ switch(quality){ case 1:setNum = 16;break; case 2:setNum = 32;break; case 3:setNum = 64;break; default:setNum = 32; } } //___________________________________________________________________________________________________________________________________ // functions public function pickUp(_bmd:BitmapData):Array{ var _array:Array = new Array(); var coeX:Number = _bmd.width/(setNum+1); var coeY:Number = _bmd.height/(setNum+1); var red:uint; var green:uint; var blue:uint; var _color16:uint; var _color8:uint; var _obj:Object = new Object(); for(var i:int = 1;i<=setNum;i++){ for(var j:int = 1;j<=setNum;j++){ _color16 = _bmd.getPixel(coeX*i,coeY*j); red = ((_color16 >> 20) << 4) | (_color16 >> 20); green = ((_color16 >> 12 & 0xF) << 4) | (_color16 >> 12 & 0xF); blue = ((_color16 >> 4 & 0xF) << 4) | (_color16 >> 4 & 0xF); _color8 = red <<16 | green<<8 | blue; if(_obj[_color8]==undefined){ _array.push({color:_color8,cnt:1}); _obj[_color8] = _array[_array.length-1]; }else{ _obj[_color8].cnt++; } } } _array.sortOn("cnt", Array.DESCENDING | Array.NUMERIC); return arrayReduce(_array); } private function arrayReduce(_array:Array):Array{ var _obj:Object; var _color:uint; var fix_array:Array = new Array(); var flag:Boolean; for each(_obj in _array){ flag = true; for each(_color in fix_array){ if(Math.abs((_obj.color >> 20) - (_color >> 20))+Math.abs((_obj.color >> 12 & 0xF) - (_color >> 12 & 0xF))+Math.abs((_obj.color >> 4 & 0xF) - (_color >> 4 & 0xF))<difColor){ flag=false; break; } } if(flag)fix_array.push(_obj.color); } return fix_array; } } }
应用场景1:chrome
根据图标的主要颜色算出下方的横线显示的颜色
应用场景2 :iTunes11
点击专辑后,会自动根据专辑颜色主色调改变专辑信息的背景,太酷了....
as的应用场景自己想吧