From 3c027104e9de5dcbfa6fce49768b1300bfdd7ce7 Mon Sep 17 00:00:00 2001 From: "sigonasr2, Sig, Sigo" Date: Fri, 15 Apr 2022 10:46:55 -0500 Subject: [PATCH] Alpha transparency implementation Transparency and Alpha values are now integrated in color painting. --- src/sig/Color.java | 9 +++++++-- src/sig/Panel.java | 30 ++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/sig/Color.java b/src/sig/Color.java index c6e0056..79297c6 100644 --- a/src/sig/Color.java +++ b/src/sig/Color.java @@ -1,7 +1,7 @@ package sig; 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 RED = new Color(204,0,0); @@ -21,13 +21,18 @@ public class Color { final static public Color BRIGHT_WHITE = new Color(255,255,255); public Color(int r, int g, int b) { + this(r,g,b,255); + } + + public Color(int r, int g, int b,int a) { super(); this.r = r; this.g = g; this.b = b; + this.a = a; } public int getColor() { - return (r<<16)+(g<<8)+b; + return (a<<24)+(r<<16)+(g<<8)+b; } } diff --git a/src/sig/Panel.java b/src/sig/Panel.java index 3abffb4..9d9101f 100644 --- a/src/sig/Panel.java +++ b/src/sig/Panel.java @@ -116,7 +116,8 @@ public class Panel extends JPanel implements Runnable { new Point(30,218), }); //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); } @@ -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++) { int index = (scanLine+(int)y_offset)*width+x+(int)x_offset; if (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 public void run() { while (true) {