/*
 * Copyright 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.css.compiler.gssfunctions;

import java.awt.Color;

/**
 * Utility functions to deal with colors.
 *
 * @author dgajda@google.com (Damian Gajda)
 */
class ColorUtil {

  public static final int H = 0;
  public static final int S = 1;
  public static final int B = 2;

  public static float[] toHsb(Color color) {
    return Color.RGBtoHSB(
        color.getRed(), color.getGreen(), color.getBlue(), null);
  }

  public static String formatColor(Color color) {
    return String.format("#%02X%02X%02X",
        color.getRed(), color.getGreen(), color.getBlue());
  }

  public static Color hsbToColor(float[] inputHsb) {
    return Color.getHSBColor(inputHsb[H], inputHsb[S], inputHsb[B]);
  }

  /**
   * Tests whether the given colors are contrasting colors, according to the
   * test described in the W3C accessibility evaluation working draft
   * {@link "http://www.w3.org/TR/AERT#color-contrast"}.  This is a lenient
   * version of the test which allows the user to pass in the accepted leniency
   * margin.
   *
   * <p>The value of the leniency margin is in the range 0 to 1.0.  0 means
   * that the test is not lenient at all, 1.0 means that the test will pass for
   * all colors that are different.  It is recommended not to use values of more
   * than 0.01.
   *
   * @param color1 the first of the two checked colors
   * @param color2 the second of the two checked colors
   * @param margin the test leniency margin
   * @return whether the given colors are considered contrasting, taking the
   *     leniency margin into account
   */
  public static boolean testContrast(Color color1, Color color2, float margin) {
    float differenceFraction = 1f - margin;
    return luminanceDiff(color1, color2) > 125 * differenceFraction
        && colorDiff(color1, color2) > 500 * differenceFraction;
  }

  /**
   * Tests whether the given colors are contrasting colors, according to the
   * test described in the W3C accessibility evaluation working draft
   * {@link "http://www.w3.org/TR/AERT#color-contrast"}.
   *
   * @param color1 the first of the two checked colors
   * @param color2 the second of the two checked colors
   * @return whether the given colors are considered contrasting
   */
  public static boolean testContrast(Color color1, Color color2) {
    return luminanceDiff(color1, color2) > 125
        && colorDiff(color1, color2) > 500;
  }

  /**
   * Computes the luminance difference of two colors (a value in range 0-255).
   * It is the luminance value equal to the Y component of the YIQ or the YUV
   * color space models.
   */
  public static int luminanceDiff(Color c1, Color c2) {
    return Math.abs(luminance(c1) - luminance(c2));
  }

  /**
   * Computes the luminance value of a color (a value in range 0-255).
   * It is the luminance value equal to the Y component of the YIQ or the YUV
   * color space models.
   */
  public static int luminance(Color color) {
    return luminance(color.getRed(), color.getGreen(), color.getBlue());
  }

  /**
   * Computes the luminance value of a color (a value in range 0-255).
   * It is the luminance value equal to the Y component of the YIQ or the YUV
   * color space models.
   */
  public static int luminance(int red, int green, int blue) {
    return (red * 299 + green * 587 + blue * 114) / 1000;
  }

  /**
   * Calculates the Manhattan distance of two colors in the RGB color space
   * (a value in range 0-(255*3)).
   */
  public static int colorDiff(Color color1, Color color2) {
    return colorDiff(
        color1.getRed(), color1.getGreen(), color1.getBlue(),
        color2.getRed(), color2.getGreen(), color2.getBlue());
  }

  /**
   * Calculates the Manhattan distance of two colors in the RGB color space
   * (a value in range 0-(255*3)).
   */
  public static int colorDiff(int r1, int g1, int b1, int r2, int g2, int b2) {
    return Math.abs(r1 - r2) + Math.abs(g1 - g2) + Math.abs(b1 - b2);
  }

  // Utility class, static methods only.
  private ColorUtil() {
  }
}

@@ -781,31 +781,24 @@
 orUtil %7B%0A%0A  
-public 
 static final
@@ -807,31 +807,24 @@
 nt H = 0;%0A  
-public 
 static final
@@ -837,23 +837,16 @@
  = 1;%0A  
-public 
 static f
@@ -860,31 +860,24 @@
 t B = 2;%0A%0A  
-public 
 static float
@@ -996,31 +996,24 @@
 ll);%0A  %7D%0A%0A  
-public 
 static Strin
@@ -1150,23 +1150,16 @@
 %0A  %7D%0A%0A  
-public 
 static C
@@ -2123,39 +2123,32 @@
 account%0A   */%0A  
-public 
 static boolean t
@@ -2780,31 +2780,24 @@
 ing%0A   */%0A  
-public 
 static boole
@@ -3126,39 +3126,32 @@
 models.%0A   */%0A  
-public 
 static int lumin
@@ -3418,39 +3418,32 @@
 models.%0A   */%0A  
-public 
 static int lumin
@@ -3720,39 +3720,32 @@
 models.%0A   */%0A  
-public 
 static int lumin
@@ -3961,39 +3961,32 @@
 55*3)).%0A   */%0A  
-public 
 static int color
@@ -4301,15 +4301,8 @@
 /%0A  
-public 
 stat

Remove public visibility as ColorUtil is package private