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 {@code 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 public ColorCountComparator(final ColorComponent colorComponent) { 044 this.colorComponent = Objects.requireNonNull(colorComponent, "colorComponent"); 045 } 046 047 @Override 048 public int compare(final ColorCount c1, final ColorCount c2) { 049 switch (colorComponent) { 050 case ALPHA: 051 return c1.alpha - c2.alpha; 052 case RED: 053 return c1.red - c2.red; 054 case GREEN: 055 return c1.green - c2.green; 056 case BLUE: 057 return c1.blue - c2.blue; 058 default: 059 return 0; 060 } 061 } 062 063}