Skip to content

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

Merged
merged 7 commits into from
Jun 24, 2025

Conversation

capdevon
Copy link
Contributor

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:

  • DRY Principle Adherence: Extensively applied the "Don't Repeat Yourself" principle by refactoring constructors and various set methods to delegate to a single, authoritative set(float r, float g, float b, float a). This ensures consistent behavior and simplifies updates.
  • Improved Bitwise Operations Readability: Complex conversions between float color components and int/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.
  • Consolidated Array and Interpolation Logic: Methods like getColorArray() and toArray() now share a common internal implementation, as do the interpolateLocal overloads, leading to less redundant code.

These changes result in a more efficient, less error-prone, and more maintainable ColorRGBA class.

Copy link

github-actions bot commented Jun 19, 2025

🖼️ 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:

  • Go to the (failed run) > Summary > Artifacts > screenshot-test-report
  • Download the zip and open jme3-screenshot-tests/build/reports/ScreenshotDiffReport.html

⚠️ If you didn't expect to change anything visual:
Fix your changes so the screenshot tests pass.

If you did mean to change things:
Review the replacement images in jme3-screenshot-tests/build/changed-images to make sure they really are improvements and then replace and commit the replacement images at jme3-screenshot-tests/src/test/resources.

If you are creating entirely new tests:
Find the new images in jme3-screenshot-tests/build/changed-images and commit the new images at jme3-screenshot-tests/src/test/resources.

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

@yaRnMcDonuts yaRnMcDonuts added this to the v3.9.0 milestone Jun 19, 2025
Copy link
Contributor

@JNightRider JNightRider left a 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:

  1. asIntRGBA()
  2. asIntABGR()
  3. 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]

@capdevon
Copy link
Contributor Author

capdevon commented Jun 20, 2025

@JNightRider @yaRnMcDonuts thanks for your feedback. I fixed the bug on the toInt() method, now all tests passed successfully.

@capdevon
Copy link
Contributor Author

capdevon commented Jun 20, 2025

I also added the ColorRGBATest class in the test package ;)

@yaRnMcDonuts yaRnMcDonuts merged commit cc97e3a into jMonkeyEngine:master Jun 24, 2025
16 checks passed
@capdevon capdevon deleted the capdevon-ColorRGBA branch June 25, 2025 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants