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

王咏刚,2005年4月

ColorPicker类里封装了HS色环和B亮度槽的MovieClip,这两种MovieClip又分别对应了ColorWheel和ColorTube两个类。它们的代码如下:

import wix.*;
class wix.ColorWheel extends MovieClip
{
 private var pressed:Boolean;
 private var pointer:MovieClip = null;
 private var mask:MovieClip = null;
 
 public function init(p:MovieClip, m:MovieClip) {
  pointer = p;
  mask = m;
 }
 
 public function ColorWheel() {
  pressed = false;
  this.onMouseDown = doPress;
  this.onMouseUp = doRelease;
  this.onMouseMove = doMove;
 }
 
 private function doPress(){
  if (enabled) {
   if (mask.hitTest(_root._xmouse, _root._ymouse, true)) {
    pressed = true;
    doMove();
   }
  }
 }
 
 private function doRelease() {
  if (enabled)
   pressed = false;
 }
 
 private function doMove() {
  if (enabled) {
   if (pressed && pointer && mask ) {
    if (mask.hitTest(_root._xmouse, _root._ymouse, true)) {
     pointer._x = _parent._xmouse;
     pointer._y = _parent._ymouse;
     _parent.updateHSB();
    }
    else {    
     var x1 = _parent._xmouse - this._x - this._width / 2;
     var y1 = _parent._ymouse - this._y - this._height / 2;
     var theta = ColorMan.getThetaByXY(x1, y1);    
     _parent.setHSB(theta, 100, null);
    }    
   }
  }
 }
 
 public function setBrightness(brightness:Number) {
  if (enabled)
   this._alpha = brightness;
 }
}


import wix.ColorMan;
class wix.ColorTube extends MovieClip
{
 private var pressed:Boolean;
 private var pointer:MovieClip = null;
 
 public function init(p:MovieClip) {
  pointer = p;
 }
 
 public function ColorTube() {
  pressed = false;
  this.onMouseDown = doPress;
  this.onMouseUp = doRelease;
  this.onMouseMove = doMove;
 }
 
 private function doPress(){
  if (enabled) {
   if (this.hitTest(_root._xmouse, _root._ymouse, true) ||
     pointer.hitTest(_root._xmouse, _root._ymouse, true)) {
    pressed = true;
    doMove();
   }
  }
 }
 
 private function doRelease() {
  if (enabled)
   pressed = false;
 }
 
 private function doMove() {
  if (enabled) {
   if (pressed && pointer) {
    pointer._y = Math.min(Math.max(this._y + 2, _parent._ymouse),
              this._y + this._height - 2);
    _parent.updateHSB();
   }
  }
 }
 
 public function paint(h:Number, s:Number) {
  if (enabled) {
   var b, rgb:Number;
   this.clear();
   for(b = 0; b <= 100; b++)
   {
    rgb = ColorMan.hsb2rgbValue(h, s, b);  
    this.lineStyle(2, rgb, 100);
    this.moveTo(2, 203 - b * 2);
    this.lineTo(19, 203 - b * 2);
   }
  }
 }
}

……未完待续……

原文地址:https://www.cnblogs.com/xiaomaohai/p/6157224.html