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