2
* Copyright (c) 2005-2010 Substance Kirill Grouchnikov. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
10
* o Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
14
* o Neither the name of Substance Kirill Grouchnikov nor the names of
15
* its contributors may be used to endorse or promote products derived
16
* from this software without specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
package org.pushingpixels.substance.internal.colorscheme;
32
import java.awt.Color;
34
import org.pushingpixels.substance.api.SubstanceColorScheme;
35
import org.pushingpixels.substance.api.colorscheme.BaseColorScheme;
36
import org.pushingpixels.substance.internal.utils.HashMapKey;
37
import org.pushingpixels.substance.internal.utils.LazyResettableHashMap;
38
import org.pushingpixels.substance.internal.utils.SubstanceColorUtilities;
39
import org.pushingpixels.substance.internal.utils.SubstanceCoreUtilities;
42
* Base class for shifted color schemes. A shifted color scheme is based on some
43
* original color scheme, a shift color and a shift factor. All colors of the
44
* original color scheme are shifted towards the shift color based on the shift
45
* factor. The closer the shift factor value is to 1.0, the closer the colors of
46
* the shifted color scheme will be to the shift color.
48
* @author Kirill Grouchnikov
50
public class ShiftColorScheme extends BaseColorScheme {
52
* Shift factor for background colors.
54
private double backgroundShiftFactor;
57
* Shift factor for foreground colors.
59
private double foregroundShiftFactor;
62
* Shift color for background colors.
64
private Color backgroundShiftColor;
67
* Shift color for foreground color.
69
private Color foregroundShiftColor;
72
* The main ultra-light color.
74
private Color mainUltraLightColor;
77
* The main extra-light color.
79
private Color mainExtraLightColor;
82
* The main light color.
84
private Color mainLightColor;
87
* The main medium color.
89
private Color mainMidColor;
92
* The main dark color.
94
private Color mainDarkColor;
97
* The main ultra-dark color.
99
private Color mainUltraDarkColor;
102
* The foreground color.
104
private Color foregroundColor;
107
* The original color scheme.
109
private SubstanceColorScheme origScheme;
112
* Cache of shifted schemes.
114
protected final static LazyResettableHashMap<SubstanceColorScheme> shiftedCache = new LazyResettableHashMap<SubstanceColorScheme>(
115
"ShiftColorScheme.shiftedSchemes");
118
* Creates a new shifted color scheme.
121
* The original color scheme.
123
* Shift color for the colors.
125
* Shift factor for the colors. Should be in 0.0-1.0 range.
127
public ShiftColorScheme(SubstanceColorScheme origScheme, Color shiftColor,
128
double shiftFactor) {
129
this(origScheme, shiftColor, shiftFactor, shiftColor,
130
shiftFactor / 2.0, false);
134
* Creates a new shifted color scheme.
137
* The original color scheme.
138
* @param backgroundShiftColor
139
* Shift color for the background colors.
140
* @param backgroundShiftFactor
141
* Shift factor for the background colors. Should be in 0.0-1.0
143
* @param foregroundShiftColor
144
* Shift color for the foreground colors.
145
* @param foregroundShiftFactor
146
* Shift factor for the foreground colors. Should be in 0.0-1.0
148
* @param shiftByBrightness
149
* If <code>true</code>, the shift will account for the
150
* brightness of the original color scheme colors.
152
public ShiftColorScheme(SubstanceColorScheme origScheme,
153
Color backgroundShiftColor, double backgroundShiftFactor,
154
Color foregroundShiftColor, double foregroundShiftFactor,
155
boolean shiftByBrightness) {
156
super("Shift " + origScheme.getDisplayName() + " to backgr ["
157
+ backgroundShiftColor + "] "
158
+ (int) (100 * backgroundShiftFactor) + "%, foregr ["
159
+ foregroundShiftColor + "]"
160
+ (int) (100 * foregroundShiftFactor) + "%", getResolver(origScheme));
161
this.backgroundShiftColor = backgroundShiftColor;
162
this.backgroundShiftFactor = backgroundShiftFactor;
163
this.foregroundShiftColor = foregroundShiftColor;
164
this.foregroundShiftFactor = foregroundShiftFactor;
165
this.origScheme = origScheme;
166
this.foregroundColor = (this.foregroundShiftColor != null) ? SubstanceColorUtilities
167
.getInterpolatedColor(this.foregroundShiftColor, origScheme
168
.getForegroundColor(), this.foregroundShiftFactor)
169
: origScheme.getForegroundColor();
170
shiftByBrightness = shiftByBrightness
171
&& (this.backgroundShiftColor != null);
172
Color ultraDarkToShiftTo = shiftByBrightness ? SubstanceColorUtilities
173
.deriveByBrightness(this.backgroundShiftColor, origScheme
174
.getUltraDarkColor()) : this.backgroundShiftColor;
175
this.mainUltraDarkColor = (this.backgroundShiftColor != null) ? SubstanceColorUtilities
176
.getInterpolatedColor(ultraDarkToShiftTo, origScheme
177
.getUltraDarkColor(), this.backgroundShiftFactor)
178
: origScheme.getUltraDarkColor();
179
Color darkToShiftTo = shiftByBrightness ? SubstanceColorUtilities
180
.deriveByBrightness(this.backgroundShiftColor, origScheme
181
.getDarkColor()) : this.backgroundShiftColor;
182
this.mainDarkColor = (this.backgroundShiftColor != null) ? SubstanceColorUtilities
183
.getInterpolatedColor(darkToShiftTo, origScheme.getDarkColor(),
184
this.backgroundShiftFactor)
185
: origScheme.getDarkColor();
186
Color midToShiftTo = shiftByBrightness ? SubstanceColorUtilities
187
.deriveByBrightness(this.backgroundShiftColor, origScheme
188
.getMidColor()) : this.backgroundShiftColor;
189
this.mainMidColor = (this.backgroundShiftColor != null) ? SubstanceColorUtilities
190
.getInterpolatedColor(midToShiftTo, origScheme.getMidColor(),
191
this.backgroundShiftFactor)
192
: origScheme.getMidColor();
193
Color lightToShiftTo = shiftByBrightness ? SubstanceColorUtilities
194
.deriveByBrightness(this.backgroundShiftColor, origScheme
195
.getLightColor()) : this.backgroundShiftColor;
196
this.mainLightColor = (this.backgroundShiftColor != null) ? SubstanceColorUtilities
197
.getInterpolatedColor(lightToShiftTo, origScheme
198
.getLightColor(), this.backgroundShiftFactor)
199
: origScheme.getLightColor();
200
Color extraLightToShiftTo = shiftByBrightness ? SubstanceColorUtilities
201
.deriveByBrightness(this.backgroundShiftColor, origScheme
202
.getExtraLightColor()) : this.backgroundShiftColor;
203
this.mainExtraLightColor = (this.backgroundShiftColor != null) ? SubstanceColorUtilities
204
.getInterpolatedColor(extraLightToShiftTo, origScheme
205
.getExtraLightColor(), this.backgroundShiftFactor)
206
: origScheme.getExtraLightColor();
207
Color ultraLightToShiftTo = shiftByBrightness ? SubstanceColorUtilities
208
.deriveByBrightness(this.backgroundShiftColor, origScheme
209
.getUltraLightColor()) : this.backgroundShiftColor;
210
this.mainUltraLightColor = (this.backgroundShiftColor != null) ? SubstanceColorUtilities
211
.getInterpolatedColor(ultraLightToShiftTo, origScheme
212
.getUltraLightColor(), this.backgroundShiftFactor)
213
: origScheme.getUltraLightColor();
219
* @see org.pushingpixels.substance.color.ColorScheme#getForegroundColor()
222
public Color getForegroundColor() {
223
return this.foregroundColor;
229
* @see org.pushingpixels.substance.color.ColorScheme#getUltraLightColor()
232
public Color getUltraLightColor() {
233
return this.mainUltraLightColor;
239
* @see org.pushingpixels.substance.color.ColorScheme#getExtraLightColor()
242
public Color getExtraLightColor() {
243
return this.mainExtraLightColor;
249
* @see org.pushingpixels.substance.color.ColorScheme#getLightColor()
252
public Color getLightColor() {
253
return this.mainLightColor;
259
* @see org.pushingpixels.substance.color.ColorScheme#getMidColor()
262
public Color getMidColor() {
263
return this.mainMidColor;
269
* @see org.pushingpixels.substance.color.ColorScheme#getDarkColor()
272
public Color getDarkColor() {
273
return this.mainDarkColor;
279
* @see org.pushingpixels.substance.color.ColorScheme#getUltraDarkColor()
282
public Color getUltraDarkColor() {
283
return this.mainUltraDarkColor;
287
* Returns the original color scheme.
289
* @return The original color scheme.
291
public SubstanceColorScheme getOrigScheme() {
292
return this.origScheme;
296
* Returns the shift factor.
298
* @return Shift factor.
300
public double getShiftFactor() {
301
return this.backgroundShiftFactor;
305
* Returns a shifted color scheme. This method is for internal use only.
308
* The original color scheme.
309
* @param backgroundShiftColor
310
* Shift color for the background color scheme colors. May be
311
* <code>null</code> - in this case, the background color scheme
312
* colors will not be shifted.
313
* @param backgroundShiftFactor
314
* Shift factor for the background color scheme colors. If the
315
* shift color for the background color scheme colors is
316
* <code>null</code>, this value is ignored.
317
* @param foregroundShiftColor
318
* Shift color for the foreground color scheme colors. May be
319
* <code>null</code> - in this case, the foreground color scheme
320
* colors will not be shifted.
321
* @param foregroundShiftFactor
322
* Shift factor for the foreground color scheme colors. If the
323
* shift color for the foreground color scheme colors is
324
* <code>null</code>, this value is ignored.
325
* @return Shifted scheme.
327
public static SubstanceColorScheme getShiftedScheme(
328
SubstanceColorScheme orig, Color backgroundShiftColor,
329
double backgroundShiftFactor, Color foregroundShiftColor,
330
double foregroundShiftFactor) {
331
HashMapKey key = SubstanceCoreUtilities.getHashKey(orig
332
.getDisplayName(), backgroundShiftColor == null ? ""
333
: backgroundShiftColor.getRGB(), backgroundShiftFactor,
334
foregroundShiftColor == null ? "" : foregroundShiftColor
335
.getRGB(), foregroundShiftFactor);
336
SubstanceColorScheme result = shiftedCache.get(key);
337
if (result == null) {
338
result = orig.shift(backgroundShiftColor, backgroundShiftFactor,
339
foregroundShiftColor, foregroundShiftFactor);
340
shiftedCache.put(key, result);