package textbender.o.awt; // Copyright 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.*; /** A background/foreground colour-pair. */ public final class ColorPair implements Cloneable { /** Constructs a ColorPair, initially with null values. */ public ColorPair() {} /** Constructs a ColorPair. * * @param background per {@linkplain #getBackground() getBackground}() * @param foreground per {@linkplain #getForeground() getForeground}() */ public ColorPair( Color background, Color foreground ) { this.background = background; this.foreground = foreground; } /** Constructs a ColorPair, initialized from a component. * * @param component per {@linkplain #setFromComponent(Component) setFromComponent}() */ public ColorPair( Component component ) { setFromComponent( component ); } // ------------------------------------------------------------------------------------ /** Returns the background colour. */ public Color getBackground() { return background; } /** The background colour. */ public Color background; /** Sets the background colour, per {@linkplain #getBackground() getBackground}(). */ public void setBackground( Color newBackground ) { background = newBackground; } /** Returns the foreground colour. */ public Color getForeground() { return foreground; } /** The foreground colour. */ public Color foreground; /** Sets the foreground colour, per {@linkplain #getForeground() getForeground}(). */ public void setForeground( Color newForeground ) { foreground = newForeground; } /** Interchanges background and foreground. */ public void invert() { Color hold = background; background = foreground; foreground = hold; } /** Sets the colour-pair from the background and foreground of a component. */ public void setFromComponent( Component component ) { background = component.getBackground(); foreground = component.getForeground(); } /** Sets the component background and foreground to the colour-pair. */ public void setOnComponent( Component component ) { component.setBackground( background ); component.setForeground( foreground ); } // - O b j e c t ---------------------------------------------------------------------- public ColorPair clone() { try{ return (ColorPair)super.clone(); } catch( CloneNotSupportedException x ) { throw new RuntimeException( x ); } // not possible } /** Returns true iff both colours are equal. */ public boolean equals( Object o ) { if( !( o instanceof ColorPair )) return false; ColorPair other = (ColorPair)o; return background.equals( other.background ) && foreground == other.foreground; } // ==================================================================================== /** Colour-pair utilities. */ public static final class X { private X() {} /** Highlights a colour-pair to a specified hue. * * @param colorPair to highlight * @param hue HSB-model hue of highlighting: * 0.000f(red) to 0.333f(green) to 0.666f(blue) */ public static void highlightByHue( ColorPair colorPair, float hue ) { // General colour blending by alpha compositor would be nicer. // But AlphaComposite doesn't use plain Color or its components; // rather it uses the far more complicated Raster and ColorModel. Color pureBright = Color.getHSBColor( hue, /*S*/1.0f, /*B*/1.0f ); if( ColorX.brightness(colorPair.foreground) > 0.7f ) // light on dark { colorPair.background = ColorX.interBright( colorPair.background, 0.0f, 0.2f ); colorPair.foreground = ColorX.interColor( colorPair.foreground, pureBright, 0.7f ); } else // dark on light { colorPair.background = ColorX.interColor( colorPair.background, pureBright, 0.1f ); colorPair.foreground = Color.black; } } } }