-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Refactor: optimize ColorRGBA class + reduce code duplication #2507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor: optimize ColorRGBA class + reduce code duplication #2507
Conversation
🖼️ Screenshot tests have failed. The purpose of these tests is to ensure that changes introduced in this PR don't break visual features. They are visual unit tests. 📄 Where to find the report:
✅ If you did mean to change things: ✨ If you are creating entirely new tests: Note; it is very important that the committed reference images are created on the build pipeline, locally created images are not reliable. Similarly tests will fail locally but you can look at the report to check they are "visually similar". See https://github.com/jMonkeyEngine/jmonkeyengine/blob/master/jme3-screenshot-tests/README.md for more information |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @capdevon
I was testing the differences these changes can make and noticed some strange things with the following methods:
- asIntRGBA()
- asIntABGR()
- asIntARGB()
The new changes produce erroneous results. I'm not entirely sure why, but it could be a serious problem, as it doesn't produce the correct output.
This is my test class that I generated, to test the changes I created a new class ColorRGBAX that literally copies what is in this PR (the new changes), ColorRGBA is the one in 'master'.
public class TestColor {
public static void main(String[] args) {
float r = 1,
g = 0.2f,
b = 0.6f,
a = 0.8f;
ColorRGBA original = new ColorRGBA(r, g, b, a);
ColorRGBAX newColor = new ColorRGBAX(r, g, b, a);
int rgba1 = original.asIntRGBA(),
rgba2 = newColor.asIntRGBA();
System.out.println("asIntRGBA()");
System.out.println("ORIGINAL: " + rgba1);
System.out.println("NEW : " + rgba2);
int abgr1 = original.asIntABGR(),
abgr2 = newColor.asIntABGR();
System.out.println("\nasIntABGR()");
System.out.println("ORIGINAL: " + abgr1);
System.out.println("NEW : " + abgr2);
int argb1 = original.asIntARGB(),
argb2 = newColor.asIntARGB();
System.out.println("\nasIntARGB()");
System.out.println("ORIGINAL: " + argb1);
System.out.println("NEW : " + argb2);
System.out.println("\n");
System.out.println("COLOR RGBA : " + new ColorRGBA().fromIntRGBA(rgba1));
System.out.println("COLORX RGBA: "+ new ColorRGBAX().fromIntRGBA(rgba2));
System.out.println("\nCOLOR ABGR : " + new ColorRGBA().fromIntABGR(abgr1));
System.out.println("COLORX ABGR: " + new ColorRGBAX().fromIntABGR(abgr2));
System.out.println("\nCOLOR ABGR : " + new ColorRGBA().fromIntARGB(argb1));
System.out.println("COLORX ABGR: " + new ColorRGBAX().fromIntARGB(argb2));
}
}
With which I obtain the following result:
asIntRGBA()
ORIGINAL: -13395508
NEW : -52
asIntABGR()
ORIGINAL: -862374913
NEW : -1
asIntARGB()
ORIGINAL: -855690343
NEW : -103
COLOR RGBA : Color[1.0, 0.2, 0.6, 0.8]
COLORX RGBA: Color[1.0, 1.0, 1.0, 0.8]
COLOR ABGR : Color[1.0, 0.2, 0.6, 0.8]
COLORX ABGR: Color[1.0, 1.0, 1.0, 1.0]
COLOR ABGR : Color[1.0, 0.2, 0.6, 0.8]
COLORX ABGR: Color[1.0, 1.0, 0.6, 1.0]
@JNightRider @yaRnMcDonuts thanks for your feedback. I fixed the bug on the |
I also added the |
This pull request focuses on a significant code cleanup and optimization of the
ColorRGBA
class. The primary goal is to reduce code duplication, improve readability, and lay a stronger foundation for future development.Key improvements:
set
methods to delegate to a single, authoritativeset(float r, float g, float b, float a)
. This ensures consistent behavior and simplifies updates.float
color components andint
/byte
representations (asIntARGB
,fromIntARGB
, etc.) have been abstracted into clear, concise private helper methods (toByte
,fromByte
,toInt
). This makes the underlying logic much easier to follow and debug.getColorArray()
andtoArray()
now share a common internal implementation, as do theinterpolateLocal
overloads, leading to less redundant code.These changes result in a more efficient, less error-prone, and more maintainable
ColorRGBA
class.