package textbender.o.awt; // Copyright 2000-2005, 2007, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.awt.Color; /** Color utilities. */ public class ColorX // OPT to allow construction of non-thread-safe instances that use pre-constructed arrays, e.g. per textbender.a.b.base.swing.SUI { private ColorX() {} /** Returns the brightness of the color, per the HSB model. */ public static float brightness( Color color ) { return Color.RGBtoHSB ( color.getRed(), color.getGreen(), color.getBlue(), /*pre-constructed array*/null )[2]; } /** Returns the hue of the color, per the HSB model. */ public static float hue( Color color ) { return Color.RGBtoHSB ( color.getRed(), color.getGreen(), color.getBlue(), /*pre-constructed array*/null )[0]; } /** Constructs a colour intermediate between two other colours * with respect to brightness. * * @param c0 first colour * @param b1 brightness of second colour * @param fraction along line from brightness of c0, to b1; * where 0.0 (or less) returns c0, and 1.0 (or more) returns c0 with brightness b1, * and anything in between returns a proportionately intermediate brightness */ public static Color interBright( Color c0, float b1, float fraction ) { if( fraction <= 0.0f ) return c0; if( fraction >= 1.0f ) fraction = 1.0f; float[] cc0 = Color.RGBtoHSB ( c0.getRed(), c0.getGreen(), c0.getBlue(), /*pre-constructed array*/null ); final float b0 = cc0[2]; return Color.getHSBColor( cc0[0], cc0[1], b0 + fraction * (b1 - b0) ); } /** Constructs a colour intermediate between two other colours. * * @param c0 first colour * @param c1 second colour * @param fraction along line from c0 to c1; * where 0.0 (or less) returns c0, and 1.0 (or more) returns c1, * and anything in between returns a proportionately intermediate colour */ public static Color interColor( Color c0, Color c1, float fraction ) { if( fraction <= 0.0f ) return c0; if( fraction >= 1.0f ) return c1; float[] cc0 = c0.getRGBColorComponents( /*pre-constructed array*/null ); float[] cc1 = c1.getRGBColorComponents( /*pre-constructed array*/null ); for( int i=0; i<3; ++i ) cc0[i] = cc0[i] + fraction * (cc1[i] - cc0[i]); return new Color( cc0[0], cc0[1], cc0[2] ); } /** Constructs a colour intermediate between two other colours * with respect to hue. * * @param c0 first colour * @param h1 hue of second colour * @param fraction along line from hue of c0, to h1; * where 0.0 (or less) returns c0, and 1.0 (or more) returns c0 with hue h1, * and anything in between returns a proportionately intermediate hue */ public static Color interHued( Color c0, float h1, float fraction ) { if( fraction <= 0.0f ) return c0; if( fraction >= 1.0f ) fraction = 1.0f; float[] cc0 = Color.RGBtoHSB ( c0.getRed(), c0.getGreen(), c0.getBlue(), /*pre-constructed array*/null ); final float h0 = cc0[0]; return Color.getHSBColor( h0 + fraction * (h1 - h0), cc0[1], cc0[2] ); } /** Constructs a colour intermediate between two other colours * with respect to saturation. * * @param c0 first colour * @param s1 saturation of second colour * @param fraction along line from saturation of c0, to s1; * where 0.0 (or less) returns c0, and 1.0 (or more) returns c0 with saturation s1, * and anything in between returns a proportionately intermediate saturation */ public static Color interSaturated( Color c0, float s1, float fraction ) { if( fraction <= 0.0f ) return c0; if( fraction >= 1.0f ) fraction = 1.0f; float[] cc0 = Color.RGBtoHSB ( c0.getRed(), c0.getGreen(), c0.getBlue(), /*pre-constructed array*/null ); final float s0 = cc0[1]; return Color.getHSBColor( cc0[0], s0 + fraction * (s1 - s0), cc0[2] ); } /** Returns a darker or brighter shade of the colour. * Same as: *
      *     offBright( color, fraction, brightness(color) < 0.7f )
      *     
*/ public static Color offBright( Color color, float fraction ) { return offBright( color, fraction, brightness(color) < 0.7f ); } /** Returns a darker or brighter shade of the colour. * * @param fraction to shade; where 0.0 (or less) returns the same (or equal) colour; * and 1.0 (or more) returns the shade having the most extreme contrast; * and intermediate values return intermediate shades * @param isDark whether or not you consider the colour dark */ public static Color offBright( Color color, float fraction, boolean isDark ) { final float contrastingBrightness; if( isDark ) contrastingBrightness = 1.0f; else contrastingBrightness = 0.0f; return interBright( color, contrastingBrightness, fraction ); } /** Returns the saturation of the color, per the HSB model. */ public static float saturation( Color color ) { return Color.RGBtoHSB ( color.getRed(), color.getGreen(), color.getBlue(), /*pre-constructed array*/null )[1]; } /** The entirely transparent colour, in the default sRGB space. */ public static final Color TRANSPARENT = new Color( 255, 255, 255, 0 ); }