~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/internal/colorscheme/ShiftColorScheme.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2005-2010 Substance Kirill Grouchnikov. All Rights Reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without 
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 * 
 
7
 *  o Redistributions of source code must retain the above copyright notice, 
 
8
 *    this list of conditions and the following disclaimer. 
 
9
 *     
 
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. 
 
13
 *     
 
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. 
 
17
 *     
 
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. 
 
29
 */
 
30
package org.pushingpixels.substance.internal.colorscheme;
 
31
 
 
32
import java.awt.Color;
 
33
 
 
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;
 
40
 
 
41
/**
 
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.
 
47
 * 
 
48
 * @author Kirill Grouchnikov
 
49
 */
 
50
public class ShiftColorScheme extends BaseColorScheme {
 
51
        /**
 
52
         * Shift factor for background colors.
 
53
         */
 
54
        private double backgroundShiftFactor;
 
55
 
 
56
        /**
 
57
         * Shift factor for foreground colors.
 
58
         */
 
59
        private double foregroundShiftFactor;
 
60
 
 
61
        /**
 
62
         * Shift color for background colors.
 
63
         */
 
64
        private Color backgroundShiftColor;
 
65
 
 
66
        /**
 
67
         * Shift color for foreground color.
 
68
         */
 
69
        private Color foregroundShiftColor;
 
70
 
 
71
        /**
 
72
         * The main ultra-light color.
 
73
         */
 
74
        private Color mainUltraLightColor;
 
75
 
 
76
        /**
 
77
         * The main extra-light color.
 
78
         */
 
79
        private Color mainExtraLightColor;
 
80
 
 
81
        /**
 
82
         * The main light color.
 
83
         */
 
84
        private Color mainLightColor;
 
85
 
 
86
        /**
 
87
         * The main medium color.
 
88
         */
 
89
        private Color mainMidColor;
 
90
 
 
91
        /**
 
92
         * The main dark color.
 
93
         */
 
94
        private Color mainDarkColor;
 
95
 
 
96
        /**
 
97
         * The main ultra-dark color.
 
98
         */
 
99
        private Color mainUltraDarkColor;
 
100
 
 
101
        /**
 
102
         * The foreground color.
 
103
         */
 
104
        private Color foregroundColor;
 
105
 
 
106
        /**
 
107
         * The original color scheme.
 
108
         */
 
109
        private SubstanceColorScheme origScheme;
 
110
 
 
111
        /**
 
112
         * Cache of shifted schemes.
 
113
         */
 
114
        protected final static LazyResettableHashMap<SubstanceColorScheme> shiftedCache = new LazyResettableHashMap<SubstanceColorScheme>(
 
115
                        "ShiftColorScheme.shiftedSchemes");
 
116
 
 
117
        /**
 
118
         * Creates a new shifted color scheme.
 
119
         * 
 
120
         * @param origScheme
 
121
         *            The original color scheme.
 
122
         * @param shiftColor
 
123
         *            Shift color for the colors.
 
124
         * @param shiftFactor
 
125
         *            Shift factor for the colors. Should be in 0.0-1.0 range.
 
126
         */
 
127
        public ShiftColorScheme(SubstanceColorScheme origScheme, Color shiftColor,
 
128
                        double shiftFactor) {
 
129
                this(origScheme, shiftColor, shiftFactor, shiftColor,
 
130
                                shiftFactor / 2.0, false);
 
131
        }
 
132
 
 
133
        /**
 
134
         * Creates a new shifted color scheme.
 
135
         * 
 
136
         * @param origScheme
 
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
 
142
         *            range.
 
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
 
147
         *            range.
 
148
         * @param shiftByBrightness
 
149
         *            If <code>true</code>, the shift will account for the
 
150
         *            brightness of the original color scheme colors.
 
151
         */
 
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();
 
214
        }
 
215
 
 
216
        /*
 
217
         * (non-Javadoc)
 
218
         * 
 
219
         * @see org.pushingpixels.substance.color.ColorScheme#getForegroundColor()
 
220
         */
 
221
        @Override
 
222
    public Color getForegroundColor() {
 
223
                return this.foregroundColor;
 
224
        }
 
225
 
 
226
        /*
 
227
         * (non-Javadoc)
 
228
         * 
 
229
         * @see org.pushingpixels.substance.color.ColorScheme#getUltraLightColor()
 
230
         */
 
231
        @Override
 
232
    public Color getUltraLightColor() {
 
233
                return this.mainUltraLightColor;
 
234
        }
 
235
 
 
236
        /*
 
237
         * (non-Javadoc)
 
238
         * 
 
239
         * @see org.pushingpixels.substance.color.ColorScheme#getExtraLightColor()
 
240
         */
 
241
        @Override
 
242
    public Color getExtraLightColor() {
 
243
                return this.mainExtraLightColor;
 
244
        }
 
245
 
 
246
        /*
 
247
         * (non-Javadoc)
 
248
         * 
 
249
         * @see org.pushingpixels.substance.color.ColorScheme#getLightColor()
 
250
         */
 
251
        @Override
 
252
    public Color getLightColor() {
 
253
                return this.mainLightColor;
 
254
        }
 
255
 
 
256
        /*
 
257
         * (non-Javadoc)
 
258
         * 
 
259
         * @see org.pushingpixels.substance.color.ColorScheme#getMidColor()
 
260
         */
 
261
        @Override
 
262
    public Color getMidColor() {
 
263
                return this.mainMidColor;
 
264
        }
 
265
 
 
266
        /*
 
267
         * (non-Javadoc)
 
268
         * 
 
269
         * @see org.pushingpixels.substance.color.ColorScheme#getDarkColor()
 
270
         */
 
271
        @Override
 
272
    public Color getDarkColor() {
 
273
                return this.mainDarkColor;
 
274
        }
 
275
 
 
276
        /*
 
277
         * (non-Javadoc)
 
278
         * 
 
279
         * @see org.pushingpixels.substance.color.ColorScheme#getUltraDarkColor()
 
280
         */
 
281
        @Override
 
282
    public Color getUltraDarkColor() {
 
283
                return this.mainUltraDarkColor;
 
284
        }
 
285
 
 
286
        /**
 
287
         * Returns the original color scheme.
 
288
         * 
 
289
         * @return The original color scheme.
 
290
         */
 
291
        public SubstanceColorScheme getOrigScheme() {
 
292
                return this.origScheme;
 
293
        }
 
294
 
 
295
        /**
 
296
         * Returns the shift factor.
 
297
         * 
 
298
         * @return Shift factor.
 
299
         */
 
300
        public double getShiftFactor() {
 
301
                return this.backgroundShiftFactor;
 
302
        }
 
303
 
 
304
        /**
 
305
         * Returns a shifted color scheme. This method is for internal use only.
 
306
         * 
 
307
         * @param orig
 
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.
 
326
         */
 
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);
 
341
                }
 
342
                return result;
 
343
        }
 
344
}