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.palette; 018 019import java.util.Comparator; 020import java.util.Objects; 021 022/** 023 * A comparator for {#link ColorCount} elements. 024 * 025 * <p> 026 * It uses a given {#link ColorComponent} to choose what channel must be used for the comparison. 027 * </p> 028 * 029 * <p> 030 * For example, if the comparator is created for the {@link ColorComponent#RED} channel, then it will compare the value of red of each {@code ColorCount} object 031 * in the array of elements. 032 * </p> 033 * 034 * @since 1.0-alpha2 035 */ 036public class ColorCountComparator implements Comparator<ColorCount> { 037 038 /** 039 * Color component used during the comparison. 040 */ 041 private final ColorComponent colorComponent; 042 043 /** 044 * Constructs a new instance. 045 * 046 * @param colorComponent Color component to wrap. 047 */ 048 public ColorCountComparator(final ColorComponent colorComponent) { 049 this.colorComponent = Objects.requireNonNull(colorComponent, "colorComponent"); 050 } 051 052 @Override 053 public int compare(final ColorCount c1, final ColorCount c2) { 054 switch (colorComponent) { 055 case ALPHA: 056 return c1.alpha - c2.alpha; 057 case RED: 058 return c1.red - c2.red; 059 case GREEN: 060 return c1.green - c2.green; 061 case BLUE: 062 return c1.blue - c2.blue; 063 default: 064 return 0; 065 } 066 } 067 068}