View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.color;
18  
19  /**
20   * Represents a color in the CMYK color space.
21   *
22   * <p>
23   * Contains the constant values for black, white, red, green, blue, cyan, magenta, and yellow.
24   * </p>
25   *
26   * @see <a href="https://en.wikipedia.org/wiki/CMYK_color_model">https://en.wikipedia.org/wiki/CMYK_color_model</a>
27   * @since 1.0-alpha1
28   */
29  public final class ColorCmyk {
30  
31      /**
32       * A constant for color cyan. Color components are:
33       *
34       * <pre>
35       *     cyan:    100
36       *     magenta: 0
37       *     yellow:  0
38       *     key:     0
39       * </pre>
40       */
41      public static final ColorCmyk CYAN = new ColorCmyk(100, 0, 0, 0);
42  
43      /**
44       * A constant for color magenta. Color components are:
45       *
46       * <pre>
47       *     cyan:    0
48       *     magenta: 100
49       *     yellow:  0
50       *     key:     0
51       * </pre>
52       */
53      public static final ColorCmyk MAGENTA = new ColorCmyk(0, 100, 0, 0);
54  
55      /**
56       * A constant for color yellow. Color components are:
57       *
58       * <pre>
59       *     cyan:    0
60       *     magenta: 0
61       *     yellow:  100
62       *     key:     0
63       * </pre>
64       */
65      public static final ColorCmyk YELLOW = new ColorCmyk(0, 0, 100, 0);
66  
67      /**
68       * A constant for color black. Color components are:
69       *
70       * <pre>
71       *     cyan:    0
72       *     magenta: 0
73       *     yellow:  0
74       *     key:     100
75       * </pre>
76       */
77      public static final ColorCmyk BLACK = new ColorCmyk(0, 0, 0, 100);
78  
79      /**
80       * A constant for color white. Color components are:
81       *
82       * <pre>
83       *     cyan:    0
84       *     magenta: 0
85       *     yellow:  0
86       *     key:     0
87       * </pre>
88       */
89      public static final ColorCmyk WHITE = new ColorCmyk(0, 0, 0, 0);
90  
91      /**
92       * A constant for color red. Color components are:
93       *
94       * <pre>
95       *     cyan:    0
96       *     magenta: 100
97       *     yellow:  100
98       *     key:     0
99       * </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     public final double c;
128     public final double m;
129     public final double y;
130     public final double k;
131 
132     public ColorCmyk(final double c, final double m, final double y, final double k) {
133         this.c = c;
134         this.m = m;
135         this.y = y;
136         this.k = k;
137     }
138 
139     @Override
140     public boolean equals(final Object o) {
141         if (this == o) {
142             return true;
143         }
144         if (o == null || getClass() != o.getClass()) {
145             return false;
146         }
147 
148         final ColorCmyk colorCmyk = (ColorCmyk) o;
149         if (Double.compare(colorCmyk.c, c) != 0) {
150             return false;
151         }
152         if (Double.compare(colorCmyk.k, k) != 0) {
153             return false;
154         }
155         if (Double.compare(colorCmyk.m, m) != 0) {
156             return false;
157         }
158         if (Double.compare(colorCmyk.y, y) != 0) {
159             return false;
160         }
161 
162         return true;
163     }
164 
165     @Override
166     public int hashCode() {
167         int result;
168         long temp;
169         temp = Double.doubleToLongBits(c);
170         result = (int) (temp ^ temp >>> 32);
171         temp = Double.doubleToLongBits(m);
172         result = 31 * result + (int) (temp ^ temp >>> 32);
173         temp = Double.doubleToLongBits(y);
174         result = 31 * result + (int) (temp ^ temp >>> 32);
175         temp = Double.doubleToLongBits(k);
176         return 31 * result + (int) (temp ^ temp >>> 32);
177     }
178 
179     @Override
180     public String toString() {
181         return "{C: " + c + ", M: " + m + ", Y: " + y + ", K: " + k + "}";
182     }
183 }