首页 > Sktech > 习作BK_简易混色器
2018
01-17

习作BK_简易混色器

习作BK_简易混色器 - 第1张  | Processing编程艺术


简单练习鼠标滚轮的用法.


鼠标位置:选择RGB颜色
鼠标滚轮:调整RGB色值
[Q]:退出程序


在2.0内制作, 用3.0以上运行可能会报错:


int pbLeRoller=0;
int pbLeFocus=0;

EcSelector pbSelectorRed;
EcSelector pbSelectorGreen;
EcSelector pbSelectorBlue;

void setup(){
  size(320,240,P2D);
  frameRate(16);
  noStroke();
  //--
  int[] lpStep={
    0x11,0x33,0x66,0x99,0xCC,0xEE,
  };
  int lpStepNormal=10;
  int lpWidthNorm=(width-(lpStepNormal*4))/3;
  int lpYNormal=30;
  //--
  pbSelectorRed=new EcSelector(10,lpYNormal,lpWidthNorm,16,0);
  pbSelectorGreen=new EcSelector(pbSelectorRed.ccTellNextPointX(10),lpYNormal,lpWidthNorm,16,1);
  pbSelectorBlue=new EcSelector(pbSelectorGreen.ccTellNextPointX(10),lpYNormal,lpWidthNorm,16,2);
  for(int it:lpStep){
    pbSelectorRed.cmColorModel.append(color(it,0x00,0x00));
    pbSelectorGreen.cmColorModel.append(color(0x00,it,0x00));
    pbSelectorBlue.cmColorModel.append(color(0x00,0x00,it));
  }
  //--
}//+++


void draw(){
  background(0);
  //--
  pbLeRoller++;pbLeRoller&=0x0F;
  //--
  int lpCurrentRed=(int)red(pbSelectorRed.ccTellCurrentColor());
  int lpCurrentGreen=(int)green(pbSelectorGreen.ccTellCurrentColor());
  int lpCurrentBlue=(int)blue(pbSelectorBlue.ccTellCurrentColor());
  int lpCurrentColor=color(lpCurrentRed&0xFF,lpCurrentGreen&0xFF,lpCurrentBlue&0xFF);
  //--
  pbSelectorRed.ccSetLable("R:"+hex(lpCurrentRed,2));pbSelectorRed.ccUpdate(pbLeRoller);
  pbSelectorGreen.ccSetLable("G:"+hex(lpCurrentGreen,2));pbSelectorGreen.ccUpdate(pbLeRoller);
  pbSelectorBlue.ccSetLable("B:"+hex(lpCurrentBlue,2));pbSelectorBlue.ccUpdate(pbLeRoller);
  //--
  if(pbSelectorRed.ccIsMouseOver()){pbLeFocus=0;}pbSelectorRed.ccCheckFocused(pbLeFocus);
  if(pbSelectorGreen.ccIsMouseOver()){pbLeFocus=1;}pbSelectorGreen.ccCheckFocused(pbLeFocus);
  if(pbSelectorBlue.ccIsMouseOver()){pbLeFocus=2;}pbSelectorBlue.ccCheckFocused(pbLeFocus);
  //--
  fill(0xEE);text("#"+hex(lpCurrentColor,6),131,220);
  fill(lpCurrentColor);stroke(0xEE);{
    rect(10,180,300,20);
  }noStroke();
  //--
}//+++


void mouseWheel(MouseEvent evt){
  int lpShift=(int)evt.getCount();
  switch(pbLeFocus){
    case 0:pbSelectorRed.ccShiftCursor(lpShift);break;
    case 1:pbSelectorGreen.ccShiftCursor(lpShift);break;
    case 2:pbSelectorBlue.ccShiftCursor(lpShift);break;
    default:break;
  }
  //--
}//+++


void keyPressed(){switch(key){
  //--
  case 'q':fsPover();break;
  default:break;
}}//+++


void fsPover(){
  exit();
}//+++


class EcSelector{
  //--  
  IntList cmColorModel;
  //--  
  int cmX,cmY,cmW,cmH;
  int cmTotalH;
  int cmCursor;
  int cmID;
  //-- 
  String cmLable;
  boolean cmIsFocused;
  //-- 
  EcSelector(int pxX, int pxY, int pxW, int pxH, int pxID){
    cmX=pxX;cmY=pxY;
    cmW=pxW;cmH=pxH;
    cmColorModel=new IntList();
    cmTotalH=0;cmCursor=0;
    cmLable="..";
    cmID=pxID;cmIsFocused=false;
  }
  //--
  void ccUpdate(int pxRoller){
    int lpSetY=cmY;
    //--
    for(int i=0,s=cmColorModel.size();i<s;i++){
      fill(cmColorModel.get(i));
      rect(cmX,lpSetY,cmW,cmH);
      lpSetY+=cmH;
    }
    cmTotalH=lpSetY+cmH;
    fill(0xEE);text(cmLable,cmX,cmTotalH);
    //--
    if((cmIsFocused&&(pxRoller%2==0))){return;}
    noFill();stroke(0xEE);strokeWeight(2);{
      rect(cmX,cmY+cmCursor*cmH,cmW,cmH);
    }strokeWeight(1);noStroke();
    //--
  }
  //--
  void ccSetLable(String pxLable){cmLable=pxLable;}
  void ccCheckFocused(int pxID){cmIsFocused=(pxID==cmID);}
  int ccTellCurrentColor(){return cmColorModel.get(cmCursor);}
  int ccTellNextPointX(int pxOffset){return cmX+cmW+pxOffset;}
  //--
  void ccShiftCursor(int pxOffset){
    if(pxOffset==0){cmCursor=0;return;}
    cmCursor+=pxOffset;
    cmCursor=constrain(cmCursor,0,cmColorModel.size()-1);
  }
  //--
  boolean ccIsMouseOver(){
    return (mouseX>cmX)&&(mouseX<(cmX+cmW))
         &&(mouseY>cmY)&&(mouseY<(cmY+cmTotalH-cmH*2));
  }
  //--
}//+++

//EOF

 



最后编辑:
作者:constrain
constrain
nullpointerexception

留下一个回复

你的email不会被公开。