Common color utilities : Color « 2D Graphics GUI « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » 2D Graphics GUI » ColorScreenshots 
Common color utilities
     

/*
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 
 * MA  02111-1307, USA.
 */
//package no.geosoft.cc.color.ui;



import java.awt.Color;



/**
 * Common color utilities.
 
 @author <a href="mailto:[email protected]">Jacob Dreyer</a>
 */   
public class ColorUtil
{
  /**
   * Blend two colors.
   
   @param color1  First color to blend.
   @param color2  Second color to blend.
   @param ratio   Blend ratio. 0.5 will give even blend, 1.0 will return
   *                color1, 0.0 will return color2 and so on.
   @return        Blended color.
   */
  public static Color blend (Color color1, Color color2, double ratio)
  {
    float r  = (floatratio;
    float ir = (float1.0 - r;

    float rgb1[] new float[3];
    float rgb2[] new float[3];    

    color1.getColorComponents (rgb1);
    color2.getColorComponents (rgb2);    

    Color color = new Color (rgb1[0* r + rgb2[0* ir, 
                             rgb1[1* r + rgb2[1* ir, 
                             rgb1[2* r + rgb2[2* ir);
    
    return color;
  }


  
  /**
   * Make an even blend between two colors.
   
   @param c1     First color to blend.
   @param c2     Second color to blend.
   @return       Blended color.
   */
  public static Color blend (Color color1, Color color2)
  {
    return ColorUtil.blend (color1, color2, 0.5);
  }
  


  /**
   * Make a color darker.
   
   @param color     Color to make darker.
   @param fraction  Darkness fraction.
   @return          Darker color.
   */
  public static Color darker (Color color, double fraction)
  {
    int red   = (intMath.round (color.getRed()   (1.0 - fraction));
    int green = (intMath.round (color.getGreen() (1.0 - fraction));
    int blue  = (intMath.round (color.getBlue()  (1.0 - fraction));

    if (red   < 0red   = 0else if (red   > 255red   = 255;
    if (green < 0green = 0else if (green > 255green = 255;
    if (blue  < 0blue  = 0else if (blue  > 255blue  = 255;    

    int alpha = color.getAlpha();

    return new Color (red, green, blue, alpha);
  }

  

  /**
   * Make a color lighter.
   
   @param color     Color to make lighter.
   @param fraction  Darkness fraction.
   @return          Lighter color.
   */
  public static Color lighter (Color color, double fraction)
  {
    int red   = (intMath.round (color.getRed()   (1.0 + fraction));
    int green = (intMath.round (color.getGreen() (1.0 + fraction));
    int blue  = (intMath.round (color.getBlue()  (1.0 + fraction));

    if (red   < 0red   = 0else if (red   > 255red   = 255;
    if (green < 0green = 0else if (green > 255green = 255;
    if (blue  < 0blue  = 0else if (blue  > 255blue  = 255;    

    int alpha = color.getAlpha();

    return new Color (red, green, blue, alpha);
  }


  
  /**
   * Return the hex name of a specified color.
   
   @param color  Color to get hex name of.
   @return       Hex name of color: "rrggbb".
   */
  public static String getHexName (Color color)
  {
    int r = color.getRed();
    int g = color.getGreen();
    int b = color.getBlue();
    
    String rHex = Integer.toString (r, 16);
    String gHex = Integer.toString (g, 16);
    String bHex = Integer.toString (b, 16);        

    return (rHex.length() == "" + rHex : "0" + rHex+
           (gHex.length() == "" + gHex : "0" + gHex+
           (bHex.length() == "" + bHex : "0" + bHex);
  }



  /**
   * Return the "distance" between two colors. The rgb entries are taken
   * to be coordinates in a 3D space [0.0-1.0], and this method returnes
   * the distance between the coordinates for the first and second color.
   
   @param   r1, g1, b1  First color.
   @param   r2, g2, b2  Second color.
   @return  Distance bwetween colors.
   */
  public static double colorDistance (double r1, double g1, double b1,
                                      double r2, double g2, double b2)
  {
    double a = r2 - r1;
    double b = g2 - g1;
    double c = b2 - b1;
    
    return Math.sqrt (a*a + b*b + c*c);
  }
  
  
  
  /**
   * Return the "distance" between two colors.
   
   @param color1  First color [r,g,b].
   @param color2  Second color [r,g,b].
   @return        Distance bwetween colors.
   */
  public static double colorDistance (double[] color1, double[] color2)
  {
    return ColorUtil.colorDistance (color1[0], color1[1], color1[2],
                                    color2[0], color2[1], color2[2]);
  }


  
  /**
   * Return the "distance" between two colors.
   
   @param color1  First color.
   @param color2  Second color.
   @return        Distance between colors.
   */
  public static double colorDistance (Color color1, Color color2)
  {
    float rgb1[] new float[3];
    float rgb2[] new float[3];    

    color1.getColorComponents (rgb1);
    color2.getColorComponents (rgb2);    

    return ColorUtil.colorDistance (rgb1[0], rgb1[1], rgb1[2],
                                    rgb2[0], rgb2[1], rgb2[2]);
  }


  
  /**
   * Check if a color is more dark than light. Useful if an entity of
   * this color is to be labeled: Use white label on a "dark" color and
   * black label on a "light" color.
   *
   @param r,g,b  Color to check.
   @return       True if this is a "dark" color, false otherwise.
   */
  public static boolean isDark (double r, double g, double b)
  {
    // Measure distance to white and black respectively
    double dWhite = ColorUtil.colorDistance (r, g, b, 1.01.01.0);
    double dBlack = ColorUtil.colorDistance (r, g, b, 0.00.00.0);

    return dBlack < dWhite;
  }



  /**
   * Check if a color is more dark than light. Useful if an entity of
   * this color is to be labeled: Use white label on a "dark" color and
   * black label on a "light" color.
   *
   @param color  Color to check.
   @return       True if this is a "dark" color, false otherwise.
   */
  public static boolean isDark (Color color)
  {
    float r = color.getRed()   255.0f;
    float g = color.getGreen() 255.0f;
    float b = color.getBlue()  255.0f;

    return isDark (r, g, b);
  }
}


           
         
    
    
    
    
  
Related examples in the same category
1. Color class is used to work with colors in Java 2D
2. Color Utilities: common color operations
3. Color Difference
4. Rainbow ColorRainbow Color
5. XOR colorXOR color
6. Color Gradient
7. Drawing with Color
8. Color fading animation
9. 140 colors - defined for X Window System listed in O'Reilly html pocket reference 87pp
10. Color Util
11. Color Factory
12. An efficient color quantization algorithm
13. Utility for checking colors given either hexa or natural language string descriptions.
14. Derives a color by adding the specified offsets to the base color's hue, saturation, and brightness values
15. Map colors into names and vice versa.
16. Converts a given string into a color.
17. If the color is equal to one of the defined constant colors, that name is returned instead.
18. Converts the String representation of a color to an actual Color object.
ww__w.___j_a_va___2_s_.c___om__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.