用Flash MX 2004自制调色版和配色组件(二)

王咏刚,2005年4月

在这种使用了HSB色环的调色版组件中,最重要的基本函数是HSB和RGB色彩空间的相互转换。网上好像有很多人在找类似的代码,实际上,Java API中的Color类就包含这样的函数,而Java SDK又提供了源代码,我们可以直接把Java代码改为Flash用的JavaScript代码。下面是我的修改结果:

 public static function hsb2rgb(hue:Number, saturation:Number, brightness:Number):Number {
  var r, g, b, h, f, p, q, t;
  var rgbvals = new Array(3);
  hue /= 360; saturation /= 100; brightness /= 100;
  if (saturation == 0) {
   r = g = b = Math.floor(brightness * 255.0 + 0.5);
  } else {
   h = (hue - Math.floor(hue)) * 6.0;
   f = h - Math.floor(h);
   p = brightness * (1.0 - saturation);
   q = brightness * (1.0 - saturation * f);
   t = brightness * (1.0 - (saturation * (1.0 - f)));
   switch (Math.floor(h)) {
   case 0:
    r = Math.floor(brightness * 255.0 + 0.5);
    g = Math.floor(t * 255.0 + 0.5);
    b = Math.floor(p * 255.0 + 0.5);
    break;
   case 1:
    r = Math.floor(q * 255.0 + 0.5);
    g = Math.floor(brightness * 255.0 + 0.5);
    b = Math.floor(p * 255.0 + 0.5);
    break;
   case 2:
    r = Math.floor(p * 255.0 + 0.5);
    g = Math.floor(brightness * 255.0 + 0.5);
    b = Math.floor(t * 255.0 + 0.5);
    break;
   case 3:
    r = Math.floor(p * 255.0 + 0.5);
    g = Math.floor(q * 255.0 + 0.5);
    b = Math.floor(brightness * 255.0 + 0.5);
    break;
   case 4:
    r = Math.floor(t * 255.0 + 0.5);
    g = Math.floor(p * 255.0 + 0.5);
    b = Math.floor(brightness * 255.0 + 0.5);
    break;
   case 5:
    r = Math.floor(brightness * 255.0 + 0.5);
    g = Math.floor(p * 255.0 + 0.5);
    b = Math.floor(q * 255.0 + 0.5);
    break;
   }
  }
  rgbvals[0] = r;
  rgbvals[1] = g;
  rgbvals[2] = b;
  return rgbvals; 
 }
 
 public static function hsb2rgbValue(hue:Number, saturation:Number, brightness:Number):Number {
  var rgbvals = hsb2rgb(hue, saturation, brightness);
  return (rgbvals[0] << 16) | (rgbvals[1] << 8) | (rgbvals[2] << 0);
 }
 
 public static function rgb2hsb(r:Number, g:Number, b:Number):Array {
  var hue, saturation, brightness;
  var hsbvals = new Array(3);
  var cmax = (r > g) ? r : g;
  if (b > cmax) cmax = b;
  var cmin = (r < g) ? r : g;
  if (b < cmin) cmin = b;
 
  brightness = cmax / 255.0;
  if (cmax != 0)
   saturation = (cmax - cmin) / cmax;
  else
   saturation = 0;
  if (saturation == 0)
   hue = 0;
  else {
   var redc = (cmax - r) / (cmax - cmin);
   var greenc = (cmax - g) / (cmax - cmin);
   var bluec = (cmax - b) / (cmax - cmin);
   if (r == cmax)
   hue = bluec - greenc;
   else if (g == cmax)
    hue = 2.0 + redc - bluec;
    else
   hue = 4.0 + greenc - redc;
   hue = hue / 6.0;
   if (hue < 0)
   hue = hue + 1.0;
  }
  hsbvals[0] = Math.floor(hue * 360 + 0.5);
  hsbvals[1] = Math.floor(saturation * 100 + 0.5);
  hsbvals[2] = Math.floor(brightness * 100 + 0.5);
  return hsbvals;
 }
 
 public static function rgbValue2hsb(rgb:Number):Array {
  var r = rgb >> 16;
  var g = (rgb - (r << 16)) >> 8;
  var b = rgb - (r << 16) - (g << 8);
  return rgb2hsb(r, g, b);
 }

……未完待续……

posted on 2005-04-12 22:39  毛小娃  阅读(153)  评论(0编辑  收藏  举报

导航