001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package org.jvnet.substance.painter;
031:
032: import java.awt.Shape;
033: import java.awt.image.BufferedImage;
034:
035: import org.jvnet.substance.color.ColorScheme;
036: import org.jvnet.substance.color.MixColorScheme;
037: import org.jvnet.substance.utils.SubstanceCoreUtilities;
038:
039: /**
040: * Base class for gradient painters. Handles mixed themes. This class is part of
041: * officially supported API.
042: *
043: * @author Kirill Grouchnikov
044: */
045: public abstract class BaseGradientPainter implements
046: SubstanceGradientPainter {
047: /**
048: * Returns the background (including border) that matches the specified
049: * parameters.
050: *
051: * @param width
052: * Width of a UI component.
053: * @param height
054: * Height of a UI component.
055: * @param contour
056: * Contour of a UI component.
057: * @param isFocused
058: * Indication whether component owns the focus.
059: * @param colorScheme1
060: * The first color scheme.
061: * @param colorScheme2
062: * The second color scheme.
063: * @param cyclePos
064: * Cycle position. Is used for rollover and pulsation effects.
065: * Must be in 0..10 range.
066: * @param hasShine
067: * Indication whether the returned image should have a 3D shine
068: * spot in its top half.
069: * @param useCyclePosAsInterpolation
070: * Indicates the algorithm to use for computing various colors.
071: * If <code>true</code>, the <code>cyclePos</code> is used
072: * to interpolate colors between different color components of
073: * both color schemes. If <code>false</code>, the
074: * <code>cyclePos</code> is used to interpolate colors between
075: * different color components of the first color scheme.
076: * @return The background (including border) that matches the specified
077: * parameters if one of the color schemes is {@link MixColorScheme},
078: * <code>null</code> otherwise.
079: */
080: protected BufferedImage getMixContourBackground(int width,
081: int height, Shape contour, boolean isFocused,
082: ColorScheme colorScheme1, ColorScheme colorScheme2,
083: float cyclePos, boolean hasShine,
084: boolean useCyclePosAsInterpolation) {
085: if (colorScheme1 instanceof MixColorScheme) {
086: MixColorScheme mixColorScheme = (MixColorScheme) colorScheme1;
087: ColorScheme[] origSchemes = mixColorScheme.getOrigSchemes();
088:
089: BufferedImage[] components = new BufferedImage[origSchemes.length];
090: for (int i = 0; i < origSchemes.length; i++) {
091: components[i] = this .getContourBackground(width,
092: height, contour, isFocused, origSchemes[i],
093: colorScheme2, cyclePos, hasShine,
094: useCyclePosAsInterpolation);
095: }
096:
097: // Let the blending begin
098: BufferedImage current = components[0];
099: for (int i = 1; i < components.length; i++) {
100: double start = (i - 0.3) / components.length;
101: double end = (i + 0.3) / components.length;
102: current = SubstanceCoreUtilities.blendImagesHorizontal(
103: current, components[i], start, end);
104: }
105: return current;
106: }
107:
108: if (colorScheme2 instanceof MixColorScheme) {
109: MixColorScheme mixColorScheme = (MixColorScheme) colorScheme2;
110: ColorScheme[] origSchemes = mixColorScheme.getOrigSchemes();
111:
112: BufferedImage[] components = new BufferedImage[origSchemes.length];
113: for (int i = 0; i < origSchemes.length; i++) {
114: components[i] = this .getContourBackground(width,
115: height, contour, isFocused, colorScheme1,
116: origSchemes[i], cyclePos, hasShine,
117: useCyclePosAsInterpolation);
118: }
119:
120: // Let the blending begin
121: BufferedImage current = components[0];
122: for (int i = 1; i < components.length; i++) {
123: double start = (i - 0.3) / components.length;
124: double end = (i + 0.3) / components.length;
125: current = SubstanceCoreUtilities.blendImagesHorizontal(
126: current, components[i], start, end);
127: }
128: return current;
129: }
130: return null;
131: }
132: }
|