/** * ConvertColorクラス * RGB/HSB/16進数カラー変換 * * @author scratchbrain * @version 1.1.0 * @update 2007/12/17 * @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 { public class ConvertColor { // RGBから16進数へ変換 public static function RGBToHex(r:int,g:int,b:int):uint { var hex:uint = r<<16 | g<<8 | b; return hex; } // 16進数からRGBへ変換 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; } // RGBからHSBへ変換 public static function RGBToHSB(r:int,g:int,b:int):Object{ var hsb:Object = new Object; var _max:Number = Math.max(r,g,b);//rgbを比較し最大値を取得 var _min:Number = Math.min(r,g,b);//rgbを比較し最小値を取得 // 彩度 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; } // HSBからRGBへ変換 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; } } }