    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>Contains the constant values for black, white, red,
    023 * green, blue, cyan, magenta, and yellow.</p>
    024 *
    025 * @see <a href="https://en.wikipedia.org/wiki/HSL_and_HSV">https://en.wikipedia.org/wiki/HSL_and_HSV</a>
    026 * @since 1.0-alpha1
    027 */
    028public final class ColorHsv {
    029
    030    /**
    031     * A constant for color black. Color components are:
    032     * <pre>
    033     *     Hue:        0
    034     *     Saturation: 0
    035     *     Value:      0
    036     * </pre>
    037     */
    038    public static final ColorHsv BLACK = new ColorHsv(0, 0, 0);
    039
    040    /**
    041     * A constant for color white. Color components are:
    042     * <pre>
    043     *     Hue:        0
    044     *     Saturation: 0
    045     *     Value:      100
    046     * </pre>
    047     */
    048    public static final ColorHsv WHITE = new ColorHsv(0, 0, 100);
    049
    050    /**
    051     * A constant for color red. Color components are:
    052     * <pre>
    053     *     Hue:        0
    054     *     Saturation: 100
    055     *     Value:      100
    056     * </pre>
    057     */
    058    public static final ColorHsv RED = new ColorHsv(0, 100, 100);
    059
    060    /**
    061     * A constant for color green. Color components are:
    062     * <pre>
    063     *     Hue:        120
    064     *     Saturation: 100
    065     *     Value:      100
    066     * </pre>
    067     */
    068    public static final ColorHsv GREEN = new ColorHsv(120, 100, 100);
    069
    070    /**
    071     * A constant for color blue. Color components are:
    072     * <pre>
    073     *     Hue:        240
    074     *     Saturation: 100
    075     *     Value:      100
    076     * </pre>
    077     */
    078    public static final ColorHsv BLUE = new ColorHsv(240, 100, 100);
    079
    080    public final double H;
    081    public final double S;
    082    public final double V;
    083
    084    public ColorHsv(final double H, final double S, final double V) {
    085        this.H = H;
    086        this.S = S;
    087        this.V = V;
    088    }
    089
    090    @Override
    091    public String toString() {
    092        return "{H: " + H + ", S: " + S + ", V: " + V + "}";
    093    }
    094
    095    @Override
    096    public boolean equals(final Object o) {
    097        if (this == o) {
    098            return true;
    099        }
    100        if (o == null || getClass() != o.getClass()) {
    101            return false;
    102        }
    103
    104        final ColorHsv colorHsv = (ColorHsv) o;
    105        if (Double.compare(colorHsv.H, H) != 0) {
    106            return false;
    107        }
    108        if (Double.compare(colorHsv.S, S) != 0) {
    109            return false;
    110        }
    111        if (Double.compare(colorHsv.V, V) != 0) {
    112            return false;
    113        }
    114
    115        return true;
    116    }
    117
    118    @Override
    119    public int hashCode() {
    120        int result;
    121        long temp;
    122        temp = Double.doubleToLongBits(H);
    123        result = (int) (temp ^ (temp >>> 32));
    124        temp = Double.doubleToLongBits(S);
    125        result = 31 * result + (int) (temp ^ (temp >>> 32));
    126        temp = Double.doubleToLongBits(V);
    127        result = 31 * result + (int) (temp ^ (temp >>> 32));
    128        return result;
    129    }
    130}