compare JavaFX HSB Color - Java JavaFX

Java examples for JavaFX:Color

Description

compare JavaFX HSB Color

Demo Code

/*//from   ww  w.  j ava2  s. com
 * Copyright year Yuliyan Rusev - Inspix
 *
 * ColorHelpers.java is part of ColorSwapper.
 *
 *  ColorSwapper is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * ColorSwapper is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with Foobar. If not, see http://www.gnu.org/licenses/.
 *
 */
import javafx.scene.paint.Color;
import java.util.Comparator;

public class Main{
    public static Comparator<Color> hsbComparator = (o1, o2) -> {

        double step = 8;

        double lum1 = Math.sqrt(.241 * o1.getRed() + .691 * o1.getGreen() + .068 * o1.getBlue());
        double lum2 = Math.sqrt(.241 * o2.getRed() + .691 * o2.getGreen() + .068 * o2.getBlue());

        double[] hsv1 = ColorHelpers.RGBtoHSB(o1.getRed(), o1.getGreen(), o1.getBlue());
        double[] hsv2 = ColorHelpers.RGBtoHSB(o2.getRed(), o2.getGreen(), o2.getBlue());


        double res1h = hsv1[0] * step;
        double res1s = lum1 * step;
        double res1b = hsv1[2] * step;

        double res2h = hsv2[0] * step;
        double res2s = lum2 * step;
        double res2b = hsv2[2] * step;

        if (res1h != res2h) {
            return res1h < res2h ? -1 : 1;
        }
        if (res1s != res2s) {
            return res1s < res2s ? -1 : 1;
        }
        if (res1b != res2b) {
            return res1b < res2b ? -1 : 1;
        }
        return 0;
    };
    public static int compareHSB(Color color1, Color color2) {
        return hsbComparator.compare(color1, color2);
    }
}

Related Tutorials