/** * AverageColorColorクラス * カラーの平均を算出 * * @author scratchbrain * @version 1.0.0 * @update 2007/12/19 * @web site http://www.scratchbrain.net/ * @blog http://www.scratchbrain.net/blog/ver2/ * * Licensed under the MIT License * Copyright (c) 2008 scratchbrain * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ 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; // つぎの条件のとき、H==360にして計算する // 赤の色相0と360で結果が変わってしまうため /* カラー1 H1<=18(赤系) S1!=0(グレースケール以外) カラー2 H2 >= 180(青系) */ 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); } } }