Alpha transparency implementation

Transparency and Alpha values are now integrated in color painting.
main
sigonasr2, Sig, Sigo 3 years ago committed by GitHub
parent 3166050bca
commit 3c027104e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      src/sig/Color.java
  2. 30
      src/sig/Panel.java

@ -1,7 +1,7 @@
package sig; package sig;
public class Color { public class Color {
int r,g,b; int r,g,b,a;
final static public Color BLACK = new Color(0,0,0); final static public Color BLACK = new Color(0,0,0);
final static public Color RED = new Color(204,0,0); final static public Color RED = new Color(204,0,0);
@ -21,13 +21,18 @@ public class Color {
final static public Color BRIGHT_WHITE = new Color(255,255,255); final static public Color BRIGHT_WHITE = new Color(255,255,255);
public Color(int r, int g, int b) { public Color(int r, int g, int b) {
this(r,g,b,255);
}
public Color(int r, int g, int b,int a) {
super(); super();
this.r = r; this.r = r;
this.g = g; this.g = g;
this.b = b; this.b = b;
this.a = a;
} }
public int getColor() { public int getColor() {
return (r<<16)+(g<<8)+b; return (a<<24)+(r<<16)+(g<<8)+b;
} }
} }

@ -116,7 +116,8 @@ public class Panel extends JPanel implements Runnable {
new Point(30,218), new Point(30,218),
}); });
//FillRect(p,Color.BRIGHT_RED,200,200,600,64); //FillRect(p,Color.BRIGHT_RED,200,200,600,64);
FillCircle(p,Color.BRIGHT_BLUE,150,150,100); final Color testAlpha = new Color(150,0,0,128);
FillCircle(p,testAlpha,150,150,100);
FillOval(p,Color.BRIGHT_GREEN,300,150,100,50); FillOval(p,Color.BRIGHT_GREEN,300,150,100,50);
} }
@ -198,7 +199,7 @@ public class Panel extends JPanel implements Runnable {
for (int x=(int)Math.round(e1.x_of_min_y);x<=e2.x_of_min_y;x++) { for (int x=(int)Math.round(e1.x_of_min_y);x<=e2.x_of_min_y;x++) {
int index = (scanLine+(int)y_offset)*width+x+(int)x_offset; int index = (scanLine+(int)y_offset)*width+x+(int)x_offset;
if (index<p.length&&index>=0) { if (index<p.length&&index>=0) {
p[index]=col.getColor(); Draw(p,index,col.getColor());
} }
} }
} }
@ -260,6 +261,31 @@ public class Panel extends JPanel implements Runnable {
} }
} }
public void Draw(int[] canvas,int index, int col) {
int alpha = col>>>24;
if (alpha==0) {
return;}
else
if (alpha==255) {
canvas[index]=col;
} else {
float ratio=alpha/255f;
int prev_col=canvas[index];
int prev_r=(prev_col&0xFF);
int prev_g=(prev_col&0xFF00)>>>8;
int prev_b=(prev_col&0xFF0000)>>>16;
int r=(col&0xFF);
int g=(col&0xFF00)>>>8;
int b=(col&0xFF0000)>>>16;
int new_r=(int)(ratio*r+(1-ratio)*prev_r);
int new_g=(int)(ratio*g+(1-ratio)*prev_g);
int new_b=(int)(ratio*b+(1-ratio)*prev_b);
canvas[index]=new_r+(new_g<<8)+(new_b<<16)+(col&0xFF000000);
}
}
@Override @Override
public void run() { public void run() {
while (true) { while (true) {

Loading…
Cancel
Save