    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.imaging.color;
    018
    019/**
    020 * Represents a color in the HSV color space.
    021 *
    022 * <p>
    023 * Contains the constant values for black, white, red, green, blue, cyan, magenta, and yellow.
    024 * </p>
    025 *
    026 * @see <a href="https://en.wikipedia.org/wiki/HSL_and_HSV">https://en.wikipedia.org/wiki/HSL_and_HSV</a>
    027 * @since 1.0-alpha1
    028 */
    029public final class ColorHsv {
    030
    031    /**
    032     * A constant for color black. Color components are:
    033     *
    034     * <pre>
    035     *     Hue:        0
    036     *     Saturation: 0
    037     *     Value:      0
    038     * </pre>
    039     */
    040    public static final ColorHsv BLACK = new ColorHsv(0, 0, 0);
    041
    042    /**
    043     * A constant for color white. Color components are:
    044     *
    045     * <pre>
    046     *     Hue:        0
    047     *     Saturation: 0
    048     *     Value:      100
    049     * </pre>
    050     */
    051    public static final ColorHsv WHITE = new ColorHsv(0, 0, 100);
    052
    053    /**
    054     * A constant for color red. Color components are:
    055     *
    056     * <pre>
    057     *     Hue:        0
    058     *     Saturation: 100
    059     *     Value:      100
    060     * </pre>
    061     */
    062    public static final ColorHsv RED = new ColorHsv(0, 100, 100);
    063
    064    /**
    065     * A constant for color green. Color components are:
    066     *
    067     * <pre>
    068     *     Hue:        120
    069     *     Saturation: 100
    070     *     Value:      100
    071     * </pre>
    072     */
    073    public static final ColorHsv GREEN = new ColorHsv(120, 100, 100);
    074
    075    /**
    076     * A constant for color blue. Color components are:
    077     *
    078     * <pre>
    079     *     Hue:        240
    080     *     Saturation: 100
    081     *     Value:      100
    082     * </pre>
    083     */
    084    public static final ColorHsv BLUE = new ColorHsv(240, 100, 100);
    085
    086    public final double h;
    087    public final double s;
    088    public final double v;
    089
    090    public ColorHsv(final double h, final double s, final double v) {
    091        this.h = h;
    092        this.s = s;
    093        this.v = v;
    094    }
    095
    096    @Override
    097    public boolean equals(final Object o) {
    098        if (this == o) {
    099            return true;
    100        }
    101        if (o == null || getClass() != o.getClass()) {
    102            return false;
    103        }
    104
    105        final ColorHsv colorHsv = (ColorHsv) o;
    106        if (Double.compare(colorHsv.h, h) != 0) {
    107            return false;
    108        }
    109        if (Double.compare(colorHsv.s, s) != 0) {
    110            return false;
    111        }
    112        if (Double.compare(colorHsv.v, v) != 0) {
    113            return false;
    114        }
    115
    116        return true;
    117    }
    118
    119    @Override
    120    public int hashCode() {
    121        int result;
    122        long temp;
    123        temp = Double.doubleToLongBits(h);
    124        result = (int) (temp ^ temp >>> 32);
    125        temp = Double.doubleToLongBits(s);
    126        result = 31 * result + (int) (temp ^ temp >>> 32);
    127        temp = Double.doubleToLongBits(v);
    128        return 31 * result + (int) (temp ^ temp >>> 32);
    129    }
    130
    131    @Override
    132    public String toString() {
    133        return "{H: " + h + ", S: " + s + ", V: " + v + "}";
    134    }
    135}