MrBIMC / VintageChroma

A Beautiful and very advanced material color picker for Android(written in Java).
83 stars 15 forks source link

HSL class conversion from HSV #8

Closed vladamc closed 8 years ago

vladamc commented 8 years ago

I found your HSL class not working as expected, which results in wrong initial position of the progress bars when working in HSL mode

Current code:

    float[] hsv2hsl(float[] hsv) {
        float a = hsv[0];
        float b = hsv[1];
        float c = hsv[2];

        return new float[] { a, b * c / ((a = (2 - b) * c) < 1 ? a : 2), a/2 };
    }

........

    @Override
    public List<Channel> getChannels() {
        List<Channel> list = new ArrayList<>();

        list.add(new Channel(R.string.channel_hue, 0, 360, new Channel.ColorExtractor() {
            @Override
            public int extract(int color) {
                return (int) color2hsl(color)[0];
            }
        }));

        list.add(new Channel(R.string.channel_saturation, 0, 100, new Channel.ColorExtractor() {
            @Override
            public int extract(int color) {
                return 100 - (int) color2hsl(color)[1] * 100;
            }
        }));

        list.add(new Channel(R.string.channel_lightness, 0, 100, new Channel.ColorExtractor() {
            @Override
            public int extract(int color) {
                return Math.abs(50 - (int) color2hsl(color)[2] * 100);
            }
        }));

        return list;
    }

See proposed fix code below, it works for me as a solution:

    float[] hsv2hsl(float[] hsv) {
        float a = hsv[0];
        float b = hsv[1];
        float c = hsv[2];

        float l = (2 - b) * c / 2;
        return new float[] {a, b * c / (l < 0.5 ? l * 2 : 2 - l * 2), l}; 
    }

.........

    @Override
    public List<Channel> getChannels() {
        List<Channel> list = new ArrayList<>();

        list.add(new Channel(R.string.channel_hue, 0, 360, new Channel.ColorExtractor() {
            @Override
            public int extract(int color) {
                return (int) color2hsl(color)[0];
            }
        }));

        list.add(new Channel(R.string.channel_saturation, 0, 100, new Channel.ColorExtractor() {
            @Override
            public int extract(int color) {
                return (int) (color2hsl(color)[1] * 100);
            }
        }));

        list.add(new Channel(R.string.channel_lightness, 0, 100, new Channel.ColorExtractor() {
            @Override
            public int extract(int color) {
                return (int) (color2hsl(color)[2] * 100);
            }
        }));

        return list;
    }

Formulas are based on the code found here

pablode commented 8 years ago

Only partly fixed in my pull request.

MrBIMC commented 8 years ago

Solved in the latest release. Will push v1.4.1 in the next 30 mins.