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.palette;
18
19 import java.util.Comparator;
20 import java.util.Objects;
21
22 /**
23 * A comparator for {#link ColorCount} elements.
24 *
25 * <p>
26 * It uses a given {#link ColorComponent} to choose what channel must be used for the comparison.
27 * </p>
28 *
29 * <p>
30 * For example, if the comparator is created for the {@code ColorComponent.RED} channel, then it will compare the value of red of each {@code ColorCount} object
31 * in the array of elements.
32 * </p>
33 *
34 * @since 1.0-alpha2
35 */
36 public class ColorCountComparator implements Comparator<ColorCount> {
37
38 /**
39 * Color component used during the comparison.
40 */
41 private final ColorComponent colorComponent;
42
43 public ColorCountComparator(final ColorComponent colorComponent) {
44 this.colorComponent = Objects.requireNonNull(colorComponent, "colorComponent");
45 }
46
47 @Override
48 public int compare(final ColorCount c1, final ColorCount c2) {
49 switch (colorComponent) {
50 case ALPHA:
51 return c1.alpha - c2.alpha;
52 case RED:
53 return c1.red - c2.red;
54 case GREEN:
55 return c1.green - c2.green;
56 case BLUE:
57 return c1.blue - c2.blue;
58 default:
59 return 0;
60 }
61 }
62
63 }