Skip to content

After hsluvToRgb() the resulted rgb_r may be neagtive #3

Description

@stokito

After the hsluvToRgb() conversion the rgb_r may be negative and the java.awt.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 close to zero, 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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions