    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 CIE 1931 color space, also
    021 * known as XYZ color space.
    022 *
    023 * <p>Contains the constant values for black, white, red,
    024 * green, and blue.</p>
    025 *
    026 * @see <a href="https://en.wikipedia.org/wiki/CIE_1931_color_space">https://en.wikipedia.org/wiki/CIE_1931_color_space</a>
    027 * @since 1.0-alpha1
    028 */
    029public final class ColorXyz {
    030
    031    /**
    032     * A constant for color black. Color components are:
    033     * <pre>
    034     *     X: 0
    035     *     Y: 0
    036     *     Z: 0
    037     * </pre>
    038     */
    039    public static final ColorXyz BLACK = new ColorXyz(0, 0, 0);
    040
    041    /**
    042     * A constant for color white. Color components are:
    043     * <pre>
    044     *     X:  95.05
    045     *     Y: 100.00
    046     *     Z: 108.90
    047     * </pre>
    048     */
    049    public static final ColorXyz WHITE = new ColorXyz(95.05, 100, 108.9);
    050
    051    /**
    052     * A constant for color red. Color components are:
    053     * <pre>
    054     *     X: 41.24
    055     *     Y: 21.26
    056     *     Z:  1.93
    057     * </pre>
    058     */
    059    public static final ColorXyz RED = new ColorXyz(41.24, 21.26, 1.93);
    060
    061    /**
    062     * A constant for color green. Color components are:
    063     * <pre>
    064     *     X: 35.76
    065     *     Y: 71.52
    066     *     Z: 11.92
    067     * </pre>
    068     */
    069    public static final ColorXyz GREEN = new ColorXyz(35.76, 71.52, 11.92);
    070
    071    /**
    072     * A constant for color blue. Color components are:
    073     * <pre>
    074     *     X: 18.05
    075     *     Y:  7.22
    076     *     Z: 95.05
    077     * </pre>
    078     */
    079    public static final ColorXyz BLUE = new ColorXyz(18.05, 7.22, 95.05);
    080
    081    public final double X;
    082    public final double Y;
    083    public final double Z;
    084
    085    public ColorXyz(final double X, final double Y, final double Z) {
    086        this.X = X;
    087        this.Y = Y;
    088        this.Z = Z;
    089    }
    090
    091    @Override
    092    public String toString() {
    093        return "{X: " + X + ", Y: " + Y + ", Z: " + Z + "}";
    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 ColorXyz colorXyz = (ColorXyz) o;
    106        if (Double.compare(colorXyz.X, X) != 0) {
    107            return false;
    108        }
    109        if (Double.compare(colorXyz.Y, Y) != 0) {
    110            return false;
    111        }
    112        if (Double.compare(colorXyz.Z, Z) != 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(X);
    124        result = (int) (temp ^ (temp >>> 32));
    125        temp = Double.doubleToLongBits(Y);
    126        result = 31 * result + (int) (temp ^ (temp >>> 32));
    127        temp = Double.doubleToLongBits(Z);
    128        result = 31 * result + (int) (temp ^ (temp >>> 32));
    129        return result;
    130    }
    131}