From d92f45dcdb25c839185428409027b7359ab99154 Mon Sep 17 00:00:00 2001 From: extio-de Date: Sat, 20 Dec 2025 17:20:35 +0100 Subject: [PATCH] Refactor color handling by introducing MutableRgbaColor and updating ImmutableRgbaColor to use it; modify G2DDrawWindow and G2DDrawControlTooltip to utilize new color class. --- .../g2d/bo/rendering/G2DDrawWindow.java | 2 +- .../g2d/control/G2DDrawControlTooltip.java | 4 +- .../renderer/model/ImmutableRgbaColor.java | 94 +++++---- .../renderer/model/MutableRgbaColor.java | 140 +++++++++++++ .../game_engine/renderer/model/RgbaColor.java | 184 ++++-------------- 5 files changed, 239 insertions(+), 185 deletions(-) create mode 100644 src/main/java/de/extio/game_engine/renderer/model/MutableRgbaColor.java diff --git a/src/main/java/de/extio/game_engine/renderer/g2d/bo/rendering/G2DDrawWindow.java b/src/main/java/de/extio/game_engine/renderer/g2d/bo/rendering/G2DDrawWindow.java index 8060788..f4cf951 100644 --- a/src/main/java/de/extio/game_engine/renderer/g2d/bo/rendering/G2DDrawWindow.java +++ b/src/main/java/de/extio/game_engine/renderer/g2d/bo/rendering/G2DDrawWindow.java @@ -38,7 +38,7 @@ public class G2DDrawWindow extends G2DAbstractRenderingBo implements DrawWindowR } final var cMain = this.color.toAwtColor(); - final var rgbaDarker = new RgbaColor(cMain); + final var rgbaDarker = this.color.toMutable(); rgbaDarker.setR(rgbaDarker.getR() / 2); rgbaDarker.setG(rgbaDarker.getG() / 2); rgbaDarker.setB(rgbaDarker.getB() / 2); diff --git a/src/main/java/de/extio/game_engine/renderer/g2d/control/G2DDrawControlTooltip.java b/src/main/java/de/extio/game_engine/renderer/g2d/control/G2DDrawControlTooltip.java index 6f9af76..b04b412 100644 --- a/src/main/java/de/extio/game_engine/renderer/g2d/control/G2DDrawControlTooltip.java +++ b/src/main/java/de/extio/game_engine/renderer/g2d/control/G2DDrawControlTooltip.java @@ -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.G2DDrawFont; +import de.extio.game_engine.renderer.model.MutableRgbaColor; import de.extio.game_engine.renderer.model.RenderingBoLayer; -import de.extio.game_engine.renderer.model.RgbaColor; import de.extio.game_engine.spatial2.model.CoordI2; public class G2DDrawControlTooltip extends G2DAbstractRenderingBo { @@ -16,7 +16,7 @@ public class G2DDrawControlTooltip extends G2DAbstractRenderingBo { 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() { super(RenderingBoLayer.TOP); diff --git a/src/main/java/de/extio/game_engine/renderer/model/ImmutableRgbaColor.java b/src/main/java/de/extio/game_engine/renderer/model/ImmutableRgbaColor.java index 2ef7c2a..876b900 100644 --- a/src/main/java/de/extio/game_engine/renderer/model/ImmutableRgbaColor.java +++ b/src/main/java/de/extio/game_engine/renderer/model/ImmutableRgbaColor.java @@ -1,21 +1,33 @@ package de.extio.game_engine.renderer.model; 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; - 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) { this.r = color.getR(); this.g = color.getG(); this.b = color.getB(); this.a = color.getA(); - this.awtColor = super.toAwtColor(); } 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.b = b; this.a = 255; - this.awtColor = super.toAwtColor(); } 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.b = b; this.a = a; - this.awtColor = super.toAwtColor(); } @Override - public void xor(final RgbaColor other) { - throw new UnsupportedOperationException(); + public int getR() { + return this.r; } @Override - public void or(final RgbaColor other) { - throw new UnsupportedOperationException(); + public int getG() { + return this.g; } @Override - public void and(final RgbaColor other) { - throw new UnsupportedOperationException(); + public int getB() { + return this.b; } @Override - public void screen(final RgbaColor other) { - throw new UnsupportedOperationException(); - } - - @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(); + public int getA() { + return this.a; } @Override @@ -79,7 +69,39 @@ public final class ImmutableRgbaColor extends RgbaColor { if (this.awtColor != null) { 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 + "]"; } } diff --git a/src/main/java/de/extio/game_engine/renderer/model/MutableRgbaColor.java b/src/main/java/de/extio/game_engine/renderer/model/MutableRgbaColor.java new file mode 100644 index 0000000..091d4f8 --- /dev/null +++ b/src/main/java/de/extio/game_engine/renderer/model/MutableRgbaColor.java @@ -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 + "]"; + } + +} diff --git a/src/main/java/de/extio/game_engine/renderer/model/RgbaColor.java b/src/main/java/de/extio/game_engine/renderer/model/RgbaColor.java index 54fd7f4..83fcdc5 100644 --- a/src/main/java/de/extio/game_engine/renderer/model/RgbaColor.java +++ b/src/main/java/de/extio/game_engine/renderer/model/RgbaColor.java @@ -1,189 +1,81 @@ package de.extio.game_engine.renderer.model; 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(); - public RgbaColor(final int r, final int g, final int b) { - this.r = r; - this.g = g; - this.b = b; - this.a = 255; - } + ImmutableRgbaColor toImmutable(); - 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 + "]"; - } + MutableRgbaColor toMutable(); }