j2me 颜色渐变与透明处理

final class MagicColorsUtiles
 {
  //grain ==是渐变的粒度
  public static final void drawStateBar(Graphics g,int rgb,int x,int y,int wid,int hig,int grain)
  {
   int[] Rgb1={(rgb&0xff0000)>>>16,(rgb&0x00ff00)>>>8,rgb&0x0000ff};
   int[] xyz=new int[3];
   for (int j = 0, k = 0; j <= hig/2; j++, k+=grain)
   {
      if((Rgb1[0]+k)<0xff)
    xyz[0]=Rgb1[0]+k;
     else
    xyz[0]=0xff;

     if((Rgb1[1]+k)<0xff)
    xyz[1]=Rgb1[1]+k;
     else
    xyz[1]=0xff;
 
     if((Rgb1[2]+k)<0xff)
    xyz[2]=Rgb1[2]+k;
     else
     xyz[2]=0xff; 
     g.setColor(xyz[0],xyz[1],xyz[2]);
     g.drawLine(x, y + j, x + wid, y + j);
     g.drawLine(x, y + hig - j, x + wid, y + hig - j);
     }
  }

     public final static void drawShadeRect(Graphics g,   int color, int x , int y, int width, int height)
     {        
      int[] rgb = getShadeColor(color, width);
            for (int by = y; by < y + height; by +=4)
            {
               int nTemp = y + height - (by - y);
               nTemp = nTemp>4 ? 4:nTemp;
               g.drawRGB(rgb, 0, width, x, by, width,nTemp, true);
           }
  
        }
     public final static int[] getShadeColor(int color , int width)
  {
         int[] rgb;    
          int shadeWidth = width;
         int nRgbData = shadeWidth<<2;  //shadeWidth*4
          rgb = new int[nRgbData];
          int alpha = -127;
          for (int i = 0; i < shadeWidth; i++)
          {
            alpha = -127 + i;
            //主要算法在这里。
            int col = color | (128 - alpha << 24);
            rgb[i]                  = col;
   rgb[i + shadeWidth    ] = col;
   rgb[i + shadeWidth * 2] = col;
   rgb[i + shadeWidth * 3] = col;              
          }
          return rgb;
  }
  
 }

你可能感兴趣的:(算法,Class,j2me)