Home | History | Annotate | Download | only in colorpicker
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.colorpicker;
     18 
     19 import android.graphics.Color;
     20 
     21 import java.util.Comparator;
     22 
     23 /**
     24  * A color comparator which compares based on hue, saturation, and value.
     25  */
     26 public class HsvColorComparator implements Comparator<Integer> {
     27 
     28     @Override
     29     public int compare(Integer lhs, Integer rhs) {
     30         float[] hsv = new float[3];
     31         Color.colorToHSV(lhs, hsv);
     32         float hue1 = hsv[0];
     33         float sat1 = hsv[1];
     34         float val1 = hsv[2];
     35 
     36         float[] hsv2 = new float[3];
     37         Color.colorToHSV(rhs, hsv2);
     38         float hue2 = hsv2[0];
     39         float sat2 = hsv2[1];
     40         float val2 = hsv2[2];
     41 
     42         if (hue1 < hue2) {
     43             return 1;
     44         } else if (hue1 > hue2) {
     45             return -1;
     46         } else {
     47             if (sat1 < sat2) {
     48                 return 1;
     49             } else if (sat1 > sat2) {
     50                 return -1;
     51             } else {
     52                 if (val1 < val2) {
     53                     return 1;
     54                 } else if (val1 > val2) {
     55                     return -1;
     56                 }
     57             }
     58         }
     59         return 0;
     60     }
     61 }
     62