Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.scene.effect;
027
028import javafx.beans.property.DoubleProperty;
029import javafx.beans.property.DoublePropertyBase;
030import javafx.beans.property.ObjectProperty;
031import javafx.scene.Node;
032
033import com.sun.javafx.Utils;
034import com.sun.javafx.effect.EffectDirtyBits;
035import com.sun.javafx.effect.EffectUtils;
036import com.sun.javafx.geom.BaseBounds;
037import com.sun.javafx.geom.transform.BaseTransform;
038import com.sun.javafx.scene.BoundsAccessor;
039
040
041/**
042 * An effect that allows for per-pixel adjustments of hue, saturation,
043 * brightness, and contrast.
044 *  
045 * <p>
046 * Example:
047 * <pre><code>
048 * ColorAdjust colorAdjust = new ColorAdjust();
049 * colorAdjust.setContrast(0.1);
050 * colorAdjust.setHue(-0.05);
051 * colorAdjust.setBrightness(0.1);
052 * colorAdjust.setSaturation(0.2);
053 * 
054 * Image image = new Image("boat.jpg");
055 * ImageView imageView = new ImageView(image);
056 * imageView.setFitWidth(200);
057 * imageView.setPreserveRatio(true);
058 * imageView.setEffect(colorAdjust);
059 * </pre></code> 
060 * <p> The code above applied on this image: </p>
061 * <p>
062 * <img src="doc-files/photo.png"/>
063 * </p>
064 * <p> produces the following: </p>
065 * <p>
066 * <img src="doc-files/coloradjust.png"/>
067 * </p>
068 */
069public class ColorAdjust extends Effect {
070    /**
071     * Creates a new instance of ColorAdjust with default parameters.
072     */
073    public ColorAdjust() {}
074
075    /**
076     * Creates a new instance of ColorAdjust with the specified hue, saturation,
077     * brightness, and contrast.
078     * @param hue the hue adjustment value
079     * @param saturation the saturation adjustment value
080     * @param brightness the brightness adjustment value
081     * @param contrast the contrast adjustment value
082     */
083    public ColorAdjust(double hue,
084                       double saturation,
085                       double brightness,
086                       double contrast) {
087        setBrightness(brightness);
088        setContrast(contrast);
089        setHue(hue);
090        setSaturation(saturation);
091    }
092
093    @Override
094    com.sun.scenario.effect.ColorAdjust impl_createImpl() {
095        return new com.sun.scenario.effect.ColorAdjust();
096    };
097    /**
098     * The input for this {@code Effect}.
099     * If set to {@code null}, or left unspecified, a graphical image of
100     * the {@code Node} to which the {@code Effect} is attached will be
101     * used as the input.
102     * @defaultValue null
103     */
104    private ObjectProperty<Effect> input;
105
106
107    public final void setInput(Effect value) {
108        inputProperty().set(value);
109    }
110
111    public final Effect getInput() {
112        return input == null ? null : input.get();
113    }
114
115    public final ObjectProperty<Effect> inputProperty() {
116        if (input == null) {
117            input = new EffectInputProperty("input");
118        }
119        return input;
120    }
121
122    @Override
123    boolean impl_checkChainContains(Effect e) {
124        Effect localInput = getInput();
125        if (localInput == null)
126            return false;
127        if (localInput == e)
128            return true;
129        return localInput.impl_checkChainContains(e);
130    }
131
132    /**
133     * The hue adjustment value.
134     * <pre>
135     *       Min: -1.0
136     *       Max: +1.0
137     *   Default:  0.0
138     *  Identity:  0.0
139     * </pre>
140     * @defaultValue 0.0
141     */
142    private DoubleProperty hue;
143
144
145    public final void setHue(double value) {
146        hueProperty().set(value);
147    }
148
149    public final double getHue() {
150        return hue == null ? 0 : hue.get();
151    }
152
153    public final DoubleProperty hueProperty() {
154        if (hue == null) {
155            hue = new DoublePropertyBase() {
156
157                @Override
158                public void invalidated() {
159                    markDirty(EffectDirtyBits.EFFECT_DIRTY);
160                    effectBoundsChanged();
161                }
162
163                @Override
164                public Object getBean() {
165                    return ColorAdjust.this;
166                }
167
168                @Override
169                public String getName() {
170                    return "hue";
171                }
172            };
173        }
174        return hue;
175    }
176
177    /**
178     * The saturation adjustment value.
179     * <pre>
180     *       Min: -1.0
181     *       Max: +1.0
182     *   Default:  0.0
183     *  Identity:  0.0
184     * </pre>
185     * @defaultValue 0.0
186     */
187    private DoubleProperty saturation;
188
189
190    public final void setSaturation(double value) {
191        saturationProperty().set(value);
192    }
193
194    public final double getSaturation() {
195        return saturation == null ? 0 : saturation.get();
196    }
197
198    public final DoubleProperty saturationProperty() {
199        if (saturation == null) {
200            saturation = new DoublePropertyBase() {
201
202                @Override
203                public void invalidated() {
204                    markDirty(EffectDirtyBits.EFFECT_DIRTY);
205                    effectBoundsChanged();
206                }
207
208                @Override
209                public Object getBean() {
210                    return ColorAdjust.this;
211                }
212
213                @Override
214                public String getName() {
215                    return "saturation";
216                }
217            };
218        }
219        return saturation;
220    }
221
222    /**
223     * The brightness adjustment value.
224     * <pre>
225     *       Min: -1.0
226     *       Max: +1.0
227     *   Default:  0.0
228     *  Identity:  0.0
229     * </pre>
230     * @defaultValue 0.0
231     */
232    private DoubleProperty brightness;
233
234
235    public final void setBrightness(double value) {
236        brightnessProperty().set(value);
237    }
238
239    public final double getBrightness() {
240        return brightness == null ? 0 : brightness.get();
241    }
242
243    public final DoubleProperty brightnessProperty() {
244        if (brightness == null) {
245            brightness = new DoublePropertyBase() {
246
247                @Override
248                public void invalidated() {
249                    markDirty(EffectDirtyBits.EFFECT_DIRTY);
250                    effectBoundsChanged();
251                }
252
253                @Override
254                public Object getBean() {
255                    return ColorAdjust.this;
256                }
257
258                @Override
259                public String getName() {
260                    return "brightness";
261                }
262            };
263        }
264        return brightness;
265    }
266
267    /**
268     * The contrast adjustment value.
269     * <pre>
270     *       Min: -1.0
271     *       Max: +1.0
272     *   Default:  0.0
273     *  Identity:  0.0
274     * </pre>
275     * @defaultValue 0.0
276     */
277    private DoubleProperty contrast;
278
279
280    public final void setContrast(double value) {
281        contrastProperty().set(value);
282    }
283
284    public final double getContrast() {
285        return contrast == null ? 0 : contrast.get();
286    }
287
288    public final DoubleProperty contrastProperty() {
289        if (contrast == null) {
290            contrast = new DoublePropertyBase() {
291
292                @Override
293                public void invalidated() {
294                    markDirty(EffectDirtyBits.EFFECT_DIRTY);
295                    effectBoundsChanged();
296                }
297
298                @Override
299                public Object getBean() {
300                    return ColorAdjust.this;
301                }
302
303                @Override
304                public String getName() {
305                    return "contrast";
306                }
307            };
308        }
309        return contrast;
310    }
311
312    @Override
313    void impl_update() {
314        Effect localInput = getInput();
315        if (localInput != null) {
316            localInput.impl_sync();
317        }
318
319        com.sun.scenario.effect.ColorAdjust peer =
320                (com.sun.scenario.effect.ColorAdjust) impl_getImpl();
321        peer.setInput(localInput == null ? null : localInput.impl_getImpl());
322        peer.setHue((float)Utils.clamp(-1, getHue(), 1));
323        peer.setSaturation((float)Utils.clamp(-1, getSaturation(), 1));
324        peer.setBrightness((float)Utils.clamp(-1, getBrightness(), 1));
325        peer.setContrast((float)Utils.clamp(-1, getContrast(), 1));
326    }
327
328    /**
329     * @treatAsPrivate implementation detail
330     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
331     */
332    @Deprecated
333    @Override
334    public BaseBounds impl_getBounds(BaseBounds bounds,
335                                     BaseTransform tx,
336                                     Node node,
337                                     BoundsAccessor boundsAccessor) {
338        return EffectUtils.getInputBounds(bounds, tx,
339                                          node, boundsAccessor,
340                                          getInput());
341    }
342
343    /**
344     * 
345     * @treatAsPrivate implementation detail
346     * @deprecated This is an internal API that is not intended for use and will be removed in the next version
347     */
348    @Deprecated
349    @Override
350    public Effect impl_copy() {
351        ColorAdjust ca = new ColorAdjust(this.getHue(), this.getSaturation(), 
352                this.getBrightness(), this.getContrast());
353        ca.setInput(ca.getInput());
354        return ca;
355    }
356}