    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 CMYK 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/CMYK_color_model">https://en.wikipedia.org/wiki/CMYK_color_model</a>
    026 * @since 1.0-alpha1
    027 */
    028public final class ColorCmyk {
    029
    030    /**
    031     * A constant for color cyan. Color components are:
    032     * <pre>
    033     *     cyan:    100
    034     *     magenta: 0
    035     *     yellow:  0
    036     *     key:     0
    037     * </pre>
    038     */
    039    public static final ColorCmyk CYAN = new ColorCmyk(100, 0, 0, 0);
    040
    041    /**
    042     * A constant for color magenta. Color components are:
    043     * <pre>
    044     *     cyan:    0
    045     *     magenta: 100
    046     *     yellow:  0
    047     *     key:     0
    048     * </pre>
    049     */
    050    public static final ColorCmyk MAGENTA = new ColorCmyk(0, 100, 0, 0);
    051
    052    /**
    053     * A constant for color yellow. Color components are:
    054     * <pre>
    055     *     cyan:    0
    056     *     magenta: 0
    057     *     yellow:  100
    058     *     key:     0
    059     * </pre>
    060     */
    061    public static final ColorCmyk YELLOW = new ColorCmyk(0, 0, 100, 0);
    062
    063    /**
    064     * A constant for color black. Color components are:
    065     * <pre>
    066     *     cyan:    0
    067     *     magenta: 0
    068     *     yellow:  0
    069     *     key:     100
    070     * </pre>
    071     */
    072    public static final ColorCmyk BLACK = new ColorCmyk(0, 0, 0, 100);
    073
    074    /**
    075     * A constant for color white. Color components are:
    076     * <pre>
    077     *     cyan:    0
    078     *     magenta: 0
    079     *     yellow:  0
    080     *     key:     0
    081     * </pre>
    082     */
    083    public static final ColorCmyk WHITE = new ColorCmyk(0, 0, 0, 0);
    084
    085    /**
    086     * A constant for color red. Color components are:
    087     * <pre>
    088     *     cyan:    0
    089     *     magenta: 100
    090     *     yellow:  100
    091     *     key:     0
    092     * </pre>
    093     */
    094    public static final ColorCmyk RED = new ColorCmyk(0, 100, 100, 0);
    095
    096    /**
    097     * A constant for color green. Color components are:
    098     * <pre>
    099     *     cyan:    100
    100     *     magenta: 0
    101     *     yellow:  100
    102     *     key:     0
    103     * </pre>
    104     */
    105    public static final ColorCmyk GREEN = new ColorCmyk(100, 0, 100, 0);
    106
    107    /**
    108     * A constant for color blue. Color components are:
    109     * <pre>
    110     *     cyan:    100
    111     *     magenta: 100
    112     *     yellow:  0
    113     *     key:     0
    114     * </pre>
    115     */
    116    public static final ColorCmyk BLUE = new ColorCmyk(100, 100, 0, 0);
    117
    118    public final double C;
    119    public final double M;
    120    public final double Y;
    121    public final double K;
    122
    123    public ColorCmyk(final double C, final double M, final double Y, final double K) {
    124        this.C = C;
    125        this.M = M;
    126        this.Y = Y;
    127        this.K = K;
    128    }
    129
    130    @Override
    131    public String toString() {
    132        return "{C: " + C + ", M: " + M + ", Y: " + Y + ", K: " + K + "}";
    133    }
    134
    135    @Override
    136    public boolean equals(final Object o) {
    137        if (this == o) {
    138            return true;
    139        }
    140        if (o == null || getClass() != o.getClass()) {
    141            return false;
    142        }
    143
    144        final ColorCmyk colorCmyk = (ColorCmyk) o;
    145        if (Double.compare(colorCmyk.C, C) != 0) {
    146            return false;
    147        }
    148        if (Double.compare(colorCmyk.K, K) != 0) {
    149            return false;
    150        }
    151        if (Double.compare(colorCmyk.M, M) != 0) {
    152            return false;
    153        }
    154        if (Double.compare(colorCmyk.Y, Y) != 0) {
    155            return false;
    156        }
    157
    158        return true;
    159    }
    160
    161    @Override
    162    public int hashCode() {
    163        int result;
    164        long temp;
    165        temp = Double.doubleToLongBits(C);
    166        result = (int) (temp ^ (temp >>> 32));
    167        temp = Double.doubleToLongBits(M);
    168        result = 31 * result + (int) (temp ^ (temp >>> 32));
    169        temp = Double.doubleToLongBits(Y);
    170        result = 31 * result + (int) (temp ^ (temp >>> 32));
    171        temp = Double.doubleToLongBits(K);
    172        result = 31 * result + (int) (temp ^ (temp >>> 32));
    173        return result;
    174    }
    175}