Skip to content

README: add RGB color conversion example#2

Merged
boronine merged 1 commit intohsluv:masterfrom
stokito:patch-1
Mar 11, 2026
Merged

README: add RGB color conversion example#2
boronine merged 1 commit intohsluv:masterfrom
stokito:patch-1

Conversation

@stokito
Copy link
Contributor

@stokito stokito commented Mar 9, 2026

No description provided.

@boronine
Copy link
Member

The Color constructor example seems useful but the double[] rgb line is redundant I think

@stokito
Copy link
Contributor Author

stokito commented Mar 10, 2026

Sorry, when copied I removed the line. I updated the PR.

Also it turned out that this is not enough for the java.awt.Color because sometimes the rgb_r may be negative and the Color(float, float, float) will throw an exception on the bad range IllegalArgumentException: Color parameter outside of expected range: Red.

Here is a sample:

        HsluvColorConverter conv = new HsluvColorConverter();
        conv.hsluv_s = 100;
        conv.hsluv_l = 50;
        conv.hsluv_h = 128; // 128...255
        conv.hsluvToRgb();
        System.out.println(conv.rgb_r); //=> -2.2506760344320753E-13

This is a very small value but still it's negative.

So in fact it needs for an additional rounding and most implementations do have it, e.g. Conversations:

        return Color.rgb(
                (int) Math.round(converter.rgb_r * 255),
                (int) Math.round(converter.rgb_g * 255),
                (int) Math.round(converter.rgb_b * 255));

Here it uses conversion to int with rounding that hides the negative value to just 0. But in my case I wanted to use the float value directly and that's why I got the exception.

So could you add a check to avoid negative values? Or maybe there are also possible values bigger than upper range. This was a difficult to catch and understand for me because I never worked with colors and faced the exception in runtime after some testing. It might be a bug on production.

#3

@boronine
Copy link
Member

Looks good, thanks!

@boronine boronine merged commit 53bd6d6 into hsluv:master Mar 11, 2026
1 check passed
@stokito stokito deleted the patch-1 branch March 11, 2026 13:43
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.

2 participants