Refactor color handling by introducing MutableRgbaColor and updating ImmutableRgbaColor to use it; modify G2DDrawWindow and G2DDrawControlTooltip to utilize new color class.

This commit is contained in:
extio-de
2025-12-20 17:20:35 +01:00
parent 4e4c4151ac
commit d92f45dcdb
5 changed files with 239 additions and 185 deletions

View File

@@ -38,7 +38,7 @@ public class G2DDrawWindow extends G2DAbstractRenderingBo implements DrawWindowR
} }
final var cMain = this.color.toAwtColor(); final var cMain = this.color.toAwtColor();
final var rgbaDarker = new RgbaColor(cMain); final var rgbaDarker = this.color.toMutable();
rgbaDarker.setR(rgbaDarker.getR() / 2); rgbaDarker.setR(rgbaDarker.getR() / 2);
rgbaDarker.setG(rgbaDarker.getG() / 2); rgbaDarker.setG(rgbaDarker.getG() / 2);
rgbaDarker.setB(rgbaDarker.getB() / 2); rgbaDarker.setB(rgbaDarker.getB() / 2);

View File

@@ -5,8 +5,8 @@ import java.awt.Graphics2D;
import de.extio.game_engine.renderer.g2d.bo.rendering.G2DAbstractRenderingBo; import de.extio.game_engine.renderer.g2d.bo.rendering.G2DAbstractRenderingBo;
import de.extio.game_engine.renderer.g2d.bo.rendering.G2DDrawFont; import de.extio.game_engine.renderer.g2d.bo.rendering.G2DDrawFont;
import de.extio.game_engine.renderer.model.MutableRgbaColor;
import de.extio.game_engine.renderer.model.RenderingBoLayer; import de.extio.game_engine.renderer.model.RenderingBoLayer;
import de.extio.game_engine.renderer.model.RgbaColor;
import de.extio.game_engine.spatial2.model.CoordI2; import de.extio.game_engine.spatial2.model.CoordI2;
public class G2DDrawControlTooltip extends G2DAbstractRenderingBo { public class G2DDrawControlTooltip extends G2DAbstractRenderingBo {
@@ -16,7 +16,7 @@ public class G2DDrawControlTooltip extends G2DAbstractRenderingBo {
public static TooltipRecord TOOLTIP = null; public static TooltipRecord TOOLTIP = null;
private static Color BACKGROUND = (new RgbaColor(0, 0, 0, 150)).toAwtColor(); private static Color BACKGROUND = (new MutableRgbaColor(0, 0, 0, 150)).toAwtColor();
public G2DDrawControlTooltip() { public G2DDrawControlTooltip() {
super(RenderingBoLayer.TOP); super(RenderingBoLayer.TOP);

View File

@@ -1,13 +1,26 @@
package de.extio.game_engine.renderer.model; package de.extio.game_engine.renderer.model;
import java.awt.Color; import java.awt.Color;
import java.util.Objects;
public final class ImmutableRgbaColor extends RgbaColor { public final class ImmutableRgbaColor implements RgbaColor {
private final int r;
private final int g;
private final int b;
private final int a;
private transient Color awtColor; private transient Color awtColor;
ImmutableRgbaColor() { public ImmutableRgbaColor(final Color color) {
this.r = color.getRed();
this.g = color.getGreen();
this.b = color.getBlue();
this.a = color.getAlpha();
this.awtColor = color;
} }
public ImmutableRgbaColor(final RgbaColor color) { public ImmutableRgbaColor(final RgbaColor color) {
@@ -15,7 +28,6 @@ public final class ImmutableRgbaColor extends RgbaColor {
this.g = color.getG(); this.g = color.getG();
this.b = color.getB(); this.b = color.getB();
this.a = color.getA(); this.a = color.getA();
this.awtColor = super.toAwtColor();
} }
public ImmutableRgbaColor(final int r, final int g, final int b) { public ImmutableRgbaColor(final int r, final int g, final int b) {
@@ -23,7 +35,6 @@ public final class ImmutableRgbaColor extends RgbaColor {
this.g = g; this.g = g;
this.b = b; this.b = b;
this.a = 255; this.a = 255;
this.awtColor = super.toAwtColor();
} }
public ImmutableRgbaColor(final int r, final int g, final int b, final int a) { public ImmutableRgbaColor(final int r, final int g, final int b, final int a) {
@@ -31,47 +42,26 @@ public final class ImmutableRgbaColor extends RgbaColor {
this.g = g; this.g = g;
this.b = b; this.b = b;
this.a = a; this.a = a;
this.awtColor = super.toAwtColor();
} }
@Override @Override
public void xor(final RgbaColor other) { public int getR() {
throw new UnsupportedOperationException(); return this.r;
} }
@Override @Override
public void or(final RgbaColor other) { public int getG() {
throw new UnsupportedOperationException(); return this.g;
} }
@Override @Override
public void and(final RgbaColor other) { public int getB() {
throw new UnsupportedOperationException(); return this.b;
} }
@Override @Override
public void screen(final RgbaColor other) { public int getA() {
throw new UnsupportedOperationException(); return this.a;
}
@Override
public void setA(final int a) {
throw new UnsupportedOperationException();
}
@Override
public void setB(final int b) {
throw new UnsupportedOperationException();
}
@Override
public void setG(final int g) {
throw new UnsupportedOperationException();
}
@Override
public void setR(final int r) {
throw new UnsupportedOperationException();
} }
@Override @Override
@@ -79,7 +69,39 @@ public final class ImmutableRgbaColor extends RgbaColor {
if (this.awtColor != null) { if (this.awtColor != null) {
return this.awtColor; return this.awtColor;
} }
return super.toAwtColor(); this.awtColor = new Color(this.r, this.g, this.b, this.a);
return this.awtColor;
}
@Override
public ImmutableRgbaColor toImmutable() {
return this;
}
@Override
public MutableRgbaColor toMutable() {
return new MutableRgbaColor(this);
}
@Override
public int hashCode() {
return Objects.hash(this.r, this.g, this.b, this.a);
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof final RgbaColor other)) {
return false;
}
return this.r == other.getR() && this.g == other.getG() && this.b == other.getB() && this.a == other.getA();
}
@Override
public String toString() {
return "ImmutableRgbaColor [r=" + this.r + ", g=" + this.g + ", b=" + this.b + ", a=" + this.a + "]";
} }
} }

View File

@@ -0,0 +1,140 @@
package de.extio.game_engine.renderer.model;
import java.awt.Color;
import java.util.Objects;
public final class MutableRgbaColor implements RgbaColor {
private int r;
private int g;
private int b;
private int a;
public MutableRgbaColor(final int r, final int g, final int b) {
this.r = r;
this.g = g;
this.b = b;
this.a = 255;
}
public MutableRgbaColor(final int r, final int g, final int b, final int a) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
public MutableRgbaColor(final Color color) {
this.r = color.getRed();
this.g = color.getGreen();
this.b = color.getBlue();
this.a = color.getAlpha();
}
public MutableRgbaColor(final RgbaColor color) {
this.r = color.getR();
this.g = color.getG();
this.b = color.getB();
this.a = color.getA();
}
@Override
public Color toAwtColor() {
return new Color(this.r, this.g, this.b, this.a);
}
public void xor(final RgbaColor other) {
this.r ^= other.getR();
this.g ^= other.getG();
this.b ^= other.getB();
}
public void or(final RgbaColor other) {
this.r |= other.getR();
this.g |= other.getG();
this.b |= other.getB();
}
public void and(final RgbaColor other) {
this.r &= other.getR();
this.g &= other.getG();
this.b &= other.getB();
}
public void screen(final RgbaColor other) {
this.r = (int) ((1.0 - (1.0 - (this.r / 255.0)) * (1.0 - (other.getR() / 255.0))) * 255.0);
this.g = (int) ((1.0 - (1.0 - (this.g / 255.0)) * (1.0 - (other.getG() / 255.0))) * 255.0);
this.b = (int) ((1.0 - (1.0 - (this.b / 255.0)) * (1.0 - (other.getB() / 255.0))) * 255.0);
}
@Override
public int getR() {
return this.r;
}
public void setR(final int r) {
this.r = r;
}
@Override
public int getG() {
return this.g;
}
public void setG(final int g) {
this.g = g;
}
@Override
public int getB() {
return this.b;
}
public void setB(final int b) {
this.b = b;
}
@Override
public int getA() {
return this.a;
}
public void setA(final int a) {
this.a = a;
}
@Override
public ImmutableRgbaColor toImmutable() {
return new ImmutableRgbaColor(this);
}
@Override
public MutableRgbaColor toMutable() {
return new MutableRgbaColor(this);
}
@Override
public int hashCode() {
return Objects.hash(this.r, this.g, this.b, this.a);
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof final RgbaColor other)) {
return false;
}
return this.r == other.getR() && this.g == other.getG() && this.b == other.getB() && this.a == other.getA();
}
@Override
public String toString() {
return "MutableRgbaColor [r=" + this.r + ", g=" + this.g + ", b=" + this.b + ", a=" + this.a + "]";
}
}

View File

@@ -1,189 +1,81 @@
package de.extio.game_engine.renderer.model; package de.extio.game_engine.renderer.model;
import java.awt.Color; import java.awt.Color;
import java.util.Objects;
public class RgbaColor { public interface RgbaColor {
public final static RgbaColor WHITE = new ImmutableRgbaColor(255, 255, 255, 255); RgbaColor WHITE = new ImmutableRgbaColor(255, 255, 255, 255);
public final static RgbaColor LIGHT_GRAY = new ImmutableRgbaColor(192, 192, 192, 255); RgbaColor LIGHT_GRAY = new ImmutableRgbaColor(192, 192, 192, 255);
public final static RgbaColor GRAY = new ImmutableRgbaColor(128, 128, 128, 255); RgbaColor GRAY = new ImmutableRgbaColor(128, 128, 128, 255);
public final static RgbaColor DARK_GRAY = new ImmutableRgbaColor(64, 64, 64, 255); RgbaColor DARK_GRAY = new ImmutableRgbaColor(64, 64, 64, 255);
public final static RgbaColor DARK_GRAY2 = new ImmutableRgbaColor(32, 32, 32, 255); RgbaColor DARK_GRAY2 = new ImmutableRgbaColor(32, 32, 32, 255);
public final static RgbaColor BLACK = new ImmutableRgbaColor(0, 0, 0, 255); RgbaColor BLACK = new ImmutableRgbaColor(0, 0, 0, 255);
public final static RgbaColor BLACK_DARKEN = new ImmutableRgbaColor(0, 0, 0, 100); RgbaColor BLACK_DARKEN = new ImmutableRgbaColor(0, 0, 0, 100);
public final static RgbaColor LIGHT_RED = new ImmutableRgbaColor(255, 128, 128, 255); RgbaColor LIGHT_RED = new ImmutableRgbaColor(255, 128, 128, 255);
public final static RgbaColor RED = new ImmutableRgbaColor(255, 0, 0, 255); RgbaColor RED = new ImmutableRgbaColor(255, 0, 0, 255);
public final static RgbaColor DARK_RED = new ImmutableRgbaColor(128, 0, 0, 255); RgbaColor DARK_RED = new ImmutableRgbaColor(128, 0, 0, 255);
public final static RgbaColor DARK_RED2 = new ImmutableRgbaColor(64, 0, 0, 255); RgbaColor DARK_RED2 = new ImmutableRgbaColor(64, 0, 0, 255);
public final static RgbaColor PINK = new ImmutableRgbaColor(255, 175, 175, 255); RgbaColor PINK = new ImmutableRgbaColor(255, 175, 175, 255);
public final static RgbaColor DARK_PINK = new ImmutableRgbaColor(128, 88, 88, 255); RgbaColor DARK_PINK = new ImmutableRgbaColor(128, 88, 88, 255);
public final static RgbaColor DARK_PINK2 = new ImmutableRgbaColor(64, 44, 44, 255); RgbaColor DARK_PINK2 = new ImmutableRgbaColor(64, 44, 44, 255);
public final static RgbaColor LIGHT_YELLOW = new ImmutableRgbaColor(235, 255, 151, 255); RgbaColor LIGHT_YELLOW = new ImmutableRgbaColor(235, 255, 151, 255);
public final static RgbaColor YELLOW = new ImmutableRgbaColor(214, 255, 43, 255); RgbaColor YELLOW = new ImmutableRgbaColor(214, 255, 43, 255);
public final static RgbaColor DARK_YELLOW = new ImmutableRgbaColor(160, 192, 33, 255); RgbaColor DARK_YELLOW = new ImmutableRgbaColor(160, 192, 33, 255);
public final static RgbaColor DARK_YELLOW2 = new ImmutableRgbaColor(64, 79, 0, 255); RgbaColor DARK_YELLOW2 = new ImmutableRgbaColor(64, 79, 0, 255);
public final static RgbaColor LIGHT_ORANGE = new ImmutableRgbaColor(255, 228, 128, 255); RgbaColor LIGHT_ORANGE = new ImmutableRgbaColor(255, 228, 128, 255);
public final static RgbaColor ORANGE = new ImmutableRgbaColor(255, 200, 0, 255); RgbaColor ORANGE = new ImmutableRgbaColor(255, 200, 0, 255);
public final static RgbaColor DARK_ORANGE = new ImmutableRgbaColor(128, 100, 0, 255); RgbaColor DARK_ORANGE = new ImmutableRgbaColor(128, 100, 0, 255);
public final static RgbaColor DARK_ORANGE2 = new ImmutableRgbaColor(64, 50, 0, 255); RgbaColor DARK_ORANGE2 = new ImmutableRgbaColor(64, 50, 0, 255);
public final static RgbaColor LIGHT_GREEN = new ImmutableRgbaColor(128, 255, 128, 255); RgbaColor LIGHT_GREEN = new ImmutableRgbaColor(128, 255, 128, 255);
public final static RgbaColor GREEN = new ImmutableRgbaColor(0, 255, 0, 255); RgbaColor GREEN = new ImmutableRgbaColor(0, 255, 0, 255);
public final static RgbaColor DARK_GREEN = new ImmutableRgbaColor(0, 128, 0, 255); RgbaColor DARK_GREEN = new ImmutableRgbaColor(0, 128, 0, 255);
public final static RgbaColor DARK_GREEN2 = new ImmutableRgbaColor(0, 64, 0, 255); RgbaColor DARK_GREEN2 = new ImmutableRgbaColor(0, 64, 0, 255);
public final static RgbaColor LIGHT_BLUE = new ImmutableRgbaColor(128, 192, 255, 255); RgbaColor LIGHT_BLUE = new ImmutableRgbaColor(128, 192, 255, 255);
public final static RgbaColor BLUE = new ImmutableRgbaColor(0, 0, 255, 255); RgbaColor BLUE = new ImmutableRgbaColor(0, 0, 255, 255);
public final static RgbaColor DARK_BLUE = new ImmutableRgbaColor(0, 0, 128, 255); RgbaColor DARK_BLUE = new ImmutableRgbaColor(0, 0, 128, 255);
public final static RgbaColor DARK_BLUE2 = new ImmutableRgbaColor(0, 0, 64, 255); RgbaColor DARK_BLUE2 = new ImmutableRgbaColor(0, 0, 64, 255);
protected int r; int getR();
protected int g; int getG();
protected int b; int getB();
protected int a; int getA();
RgbaColor() { Color toAwtColor();
} ImmutableRgbaColor toImmutable();
public RgbaColor(final int r, final int g, final int b) { MutableRgbaColor toMutable();
this.r = r;
this.g = g;
this.b = b;
this.a = 255;
}
public RgbaColor(final int r, final int g, final int b, final int a) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
public RgbaColor(final Color color) {
this.r = color.getRed();
this.g = color.getGreen();
this.b = color.getBlue();
this.a = color.getAlpha();
}
public RgbaColor(final RgbaColor color) {
this.r = color.getR();
this.g = color.getG();
this.b = color.getB();
this.a = color.getA();
}
public Color toAwtColor() {
return new Color(this.r, this.g, this.b, this.a);
}
public void xor(final RgbaColor other) {
this.r ^= other.r;
this.g ^= other.g;
this.b ^= other.b;
}
public void or(final RgbaColor other) {
this.r |= other.r;
this.g |= other.g;
this.b |= other.b;
}
public void and(final RgbaColor other) {
this.r &= other.r;
this.g &= other.g;
this.b &= other.b;
}
public void screen(final RgbaColor other) {
this.r = (int) ((1.0 - (1.0 - (this.r / 255.0)) * (1.0 - (other.r / 255.0))) * 255.0);
this.g = (int) ((1.0 - (1.0 - (this.g / 255.0)) * (1.0 - (other.g / 255.0))) * 255.0);
this.b = (int) ((1.0 - (1.0 - (this.b / 255.0)) * (1.0 - (other.b / 255.0))) * 255.0);
}
public int getR() {
return this.r;
}
public void setR(final int r) {
this.r = r;
}
public int getG() {
return this.g;
}
public void setG(final int g) {
this.g = g;
}
public int getB() {
return this.b;
}
public void setB(final int b) {
this.b = b;
}
public int getA() {
return this.a;
}
public void setA(final int a) {
this.a = a;
}
@Override
public int hashCode() {
return Objects.hash(this.r, this.g, this.b, this.a);
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof final RgbaColor other)) {
return false;
}
return this.r == other.r && this.g == other.g && this.b == other.b && this.a == other.a;
}
@Override
public String toString() {
return "RgbaColor [r=" + this.r + ", g=" + this.g + ", b=" + this.b + ", a=" + this.a + "]";
}
} }