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