Skip to content
This repository was archived by the owner on Jun 24, 2021. It is now read-only.

Commit 655922e

Browse files
committed
Add css fadein/fadeout functions.
fadein fadein( <color> , <number>% ) The fadein function takes a color and computes a more opaque version of that color. The second parameter is the opacity, ranging from 0% to 100%. Has no effect on fully opaque colors. Examples: fadein(rgba(35, 70, 112, 0.8), 10%) => rgba(35, 70, 112, 0.88) fadein(rgba(35, 70, 112, 0), 100%) => rgba(35, 70, 112) = rgb(35, 70, 112) fadeout fadeout( <color>, <number>% ) The fadeout functions takes a color and computes a more transparent version of that color. The second parameter is the transparency, ranging from 0% to 100%. Has no effect on fully transparent colors. Examples: fadeout(rgba(35, 70, 112, 0.8), 10%) => rgba(35, 70, 112, 0.72) fadeout(rgba(35, 70, 122), 30%) => rgba(35, 70, 122, 0.7) fadeout(rgb(35, 70, 112), 100%) => rgba(35, 70, 112, 0)
1 parent b5fb55f commit 655922e

File tree

6 files changed

+373
-4
lines changed

6 files changed

+373
-4
lines changed

modules/javafx.graphics/src/main/docs/javafx/scene/doc-files/cssref.html

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@
223223
}
224224

225225
/* Color Table Styles */
226-
div.colorsquare {
226+
div.colorsquare {
227227
display:inline-block;
228228
width:20em;
229229
margin: 3px 3px
@@ -1529,9 +1529,9 @@ <h4>Color Functions <span class="grammar" style="font-size: smaller;">&lt;color-
15291529
<p>JavaFX supports some color computation functions. These compute new
15301530
colors from input colors at the time the color style is applied. This
15311531
enables a color theme to be specified using a single base color and to
1532-
have variant colors computed from that base color. There are two color
1533-
functions: derive() and ladder().</p>
1534-
<p class="grammar">&lt;derive&gt; | &lt;ladder&gt;</p>
1532+
have variant colors computed from that base color. There are four color
1533+
functions: derive(), ladder(), fadein() and fadeout().</p>
1534+
<p class="grammar">&lt;derive&gt; | &lt;ladder&gt; | &lt;fadein&gt; | &lt;fadeout&gt;</p>
15351535
<p><strong>Derive </strong><span class="grammar" style="font-size: smaller;">&lt;derive&gt;</span></p>
15361536
<p class="grammar">derive( <a href="#typecolor" class="typelink">&lt;color&gt;</a>
15371537
, <a href="#typenumber" class="typelink">&lt;number&gt;</a>% )</p>
@@ -1576,6 +1576,22 @@ <h4>Color Functions <span class="grammar" style="font-size: smaller;">&lt;color-
15761576
href="#typecolor"
15771577
class="typelink">&lt;color&gt;</a>
15781578
) ]+ </span></p>
1579+
<p><strong>Fadein </strong><span class="grammar" style="font-size: smaller;">&lt;fadein&gt;</span></p>
1580+
<p class="grammar">fadein( <a href="#typecolor" class="typelink">&lt;color&gt;</a>
1581+
, <a href="#typenumber" class="typelink">&lt;number&gt;</a>% )</p>
1582+
<p>The fadein function takes a color and computes a more opaque version of that color.
1583+
The second parameter is the opacity, ranging from 0% to 100%. Has no effect on
1584+
fully opaque colors.</p>
1585+
<p class="example">fadein(rgba(35, 70, 112, 0.8), 10%)</p>
1586+
<p>Will result in the color rgba(35, 70, 112, 0.88)</p>
1587+
<p><strong>Fadeout </strong><span class="grammar" style="font-size: smaller;">&lt;fadeout&gt;</span></p>
1588+
<p class="grammar">fadeout( <a href="#typecolor" class="typelink">&lt;color&gt;</a>
1589+
, <a href="#typenumber" class="typelink">&lt;number&gt;</a>% )</p>
1590+
<p>The fadeout functions takes a color and computes a more transparent version of that
1591+
color. The second parameter is the transparency, ranging from 0% to 100%. Has no effect
1592+
on fully transparent colors.</p>
1593+
<p class="example">fadeout(rgba(35, 70, 112, 0.8), 10%)</p>
1594+
<p>Will result in the color rgba(35, 70, 112, 0.72)</p>
15791595
<h2><a id="stage">Stage</a></h2>
15801596
<table class="package" width="100%">
15811597
<tbody>

modules/javafx.graphics/src/main/java/javafx/css/CssParser.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import javafx.css.converter.DurationConverter;
3535
import javafx.css.converter.EffectConverter;
3636
import javafx.css.converter.EnumConverter;
37+
import javafx.css.converter.FadeinColorConverter;
38+
import javafx.css.converter.FadeoutColorConverter;
3739
import javafx.css.converter.FontConverter;
3840
import javafx.css.converter.InsetsConverter;
3941
import javafx.css.converter.PaintConverter;
@@ -1409,6 +1411,10 @@ private ParsedValueImpl parseFunction(final Term root) throws ParseException {
14091411
return innershadow(root);
14101412
} else if ("dropshadow".regionMatches(true, 0, fcn, 0, 10)) {
14111413
return dropshadow(root);
1414+
} else if ("fadein".regionMatches(true, 0, fcn, 0, 6)) {
1415+
return fadein(root);
1416+
} else if ("fadeout".regionMatches(true, 0, fcn, 0, 7)) {
1417+
return fadeout(root);
14121418
} else if ("linear-gradient".regionMatches(true, 0, fcn, 0, 15)) {
14131419
return parseLinearGradient(root);
14141420
} else if ("radial-gradient".regionMatches(true, 0, fcn, 0, 15)) {
@@ -1556,6 +1562,53 @@ private ParsedValueImpl dropshadow(final Term root) throws ParseException {
15561562
return new ParsedValueImpl<ParsedValue[],Effect>(values, EffectConverter.DropShadowConverter.getInstance());
15571563
}
15581564

1565+
// fadein <color> <number>%
1566+
private ParsedValueImpl<ParsedValue[],Color> fadein(final Term root) throws ParseException {
1567+
// first term in the chain is the function name...
1568+
final String fn = (root.token != null) ? root.token.getText() : null;
1569+
if (fn == null || !"fadein".regionMatches(true, 0, fn, 0, 6)) {
1570+
final String msg = "Expected \'fadein\'";
1571+
error(root, msg);
1572+
}
1573+
1574+
Term arg = root;
1575+
if ((arg = arg.firstArg) == null) error(root, "Expected \'<color>\'");
1576+
1577+
final ParsedValueImpl<?,Color> color = parseColor(arg);
1578+
1579+
final Term prev = arg;
1580+
if ((arg = arg.nextArg) == null) error(prev, "Expected \'<percent\'");
1581+
1582+
final ParsedValueImpl<?,Size> opacity = parseSize(arg);
1583+
1584+
ParsedValueImpl[] values = new ParsedValueImpl[] { color, opacity };
1585+
return new ParsedValueImpl<ParsedValue[],Color>(values, FadeinColorConverter.getInstance());
1586+
1587+
}
1588+
1589+
// fadeout <color> <number>%
1590+
private ParsedValueImpl<ParsedValue[],Color> fadeout(final Term root) throws ParseException {
1591+
// first term in the chain is the function name...
1592+
final String fn = (root.token != null) ? root.token.getText() : null;
1593+
if (fn == null || !"fadeout".regionMatches(true, 0, fn, 0, 7)) {
1594+
final String msg = "Expected \'fadeout\'";
1595+
error(root, msg);
1596+
}
1597+
1598+
Term arg = root;
1599+
if ((arg = arg.firstArg) == null) error(root, "Expected \'<color>\'");
1600+
1601+
final ParsedValueImpl<?,Color> color = parseColor(arg);
1602+
1603+
final Term prev = arg;
1604+
if ((arg = arg.nextArg) == null) error(prev, "Expected \'<percent\'");
1605+
1606+
final ParsedValueImpl<?,Size> transparency = parseSize(arg);
1607+
1608+
ParsedValueImpl[] values = new ParsedValueImpl[] { color, transparency };
1609+
return new ParsedValueImpl<ParsedValue[],Color>(values, FadeoutColorConverter.getInstance());
1610+
}
1611+
15591612
// returns null if the Term is null or is not a cycle method.
15601613
private ParsedValueImpl<String, CycleMethod> cycleMethod(final Term root) {
15611614
CycleMethod cycleMethod = null;

modules/javafx.graphics/src/main/java/javafx/css/StyleConverter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import javafx.css.converter.DurationConverter;
3333
import javafx.css.converter.EffectConverter;
3434
import javafx.css.converter.EnumConverter;
35+
import javafx.css.converter.FadeinColorConverter;
36+
import javafx.css.converter.FadeoutColorConverter;
3537
import javafx.css.converter.FontConverter;
3638
import javafx.css.converter.InsetsConverter;
3739
import javafx.css.converter.LadderConverter;
@@ -503,6 +505,14 @@ static StyleConverter<?,?> getInstance(final String converterClass) {
503505
case "com.sun.javafx.css.parser.DeriveSizeConverter" :
504506
styleConverter = DeriveSizeConverter.getInstance();
505507
break;
508+
case "javafx.css.converter.FadeinColorConverter":
509+
case "com.sun.javafx.css.parser.FadeinColorConverter":
510+
styleConverter = FadeinColorConverter.getInstance();
511+
break;
512+
case "javafx.css.converter.FadeoutColorConverter":
513+
case "com.sun.javafx.css.parser.FadeoutColorConverter":
514+
styleConverter = FadeoutColorConverter.getInstance();
515+
break;
506516
case "javafx.css.converter.LadderConverter":
507517
case "com.sun.javafx.css.parser.LadderConverter" :
508518
styleConverter = LadderConverter.getInstance();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package javafx.css.converter;
27+
28+
import javafx.css.Size;
29+
import javafx.css.SizeUnits;
30+
import javafx.css.StyleConverter;
31+
import javafx.css.ParsedValue;
32+
import javafx.scene.paint.Color;
33+
import javafx.scene.text.Font;
34+
35+
/**
36+
* Fade in (opacify) a Color from a Color and a opacity value.
37+
*
38+
* @since 12
39+
*/
40+
public final class FadeinColorConverter extends StyleConverter<ParsedValue[], Color> {
41+
42+
// lazy, thread-safe instantiation
43+
private static class Holder {
44+
static final FadeinColorConverter INSTANCE = new FadeinColorConverter();
45+
}
46+
47+
public static FadeinColorConverter getInstance() {
48+
return Holder.INSTANCE;
49+
}
50+
51+
private FadeinColorConverter() {
52+
super();
53+
}
54+
55+
@Override
56+
public Color convert(ParsedValue<ParsedValue[], Color> value, Font font) {
57+
ParsedValue[] values = value.getValue();
58+
final Color color = (Color) values[0].convert(font);
59+
final Size opacity = (Size) values[1].convert(font);
60+
return Color.rgb((int) (color.getRed() * 255), (int) (color.getGreen() * 255),
61+
(int) (color.getBlue() * 255), opacity.pixels(font) == 1d ?
62+
1d : Math.min(1d, Math.round(color.getOpacity() * 255 * (1 + opacity.pixels(font))) / 255d));
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return "FadeinColorConverter";
68+
}
69+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package javafx.css.converter;
27+
28+
import javafx.css.Size;
29+
import javafx.css.SizeUnits;
30+
import javafx.css.StyleConverter;
31+
import javafx.css.ParsedValue;
32+
import javafx.scene.paint.Color;
33+
import javafx.scene.text.Font;
34+
35+
/**
36+
* Fade out (transparentize) a Color from a Color and a transparency value.
37+
*
38+
* @since 12
39+
*/
40+
public final class FadeoutColorConverter extends StyleConverter<ParsedValue[], Color> {
41+
42+
// lazy, thread-safe instantiation
43+
private static class Holder {
44+
static final FadeoutColorConverter INSTANCE = new FadeoutColorConverter();
45+
}
46+
47+
public static FadeoutColorConverter getInstance() {
48+
return Holder.INSTANCE;
49+
}
50+
51+
private FadeoutColorConverter() {
52+
super();
53+
}
54+
55+
@Override
56+
public Color convert(ParsedValue<ParsedValue[], Color> value, Font font) {
57+
ParsedValue[] values = value.getValue();
58+
final Color color = (Color) values[0].convert(font);
59+
final Size transparency = (Size) values[1].convert(font);
60+
return Color.rgb((int) (color.getRed() * 255), (int) (color.getGreen() * 255),
61+
(int) (color.getBlue() * 255), transparency.pixels(font) == 0d ?
62+
0d : Math.round(color.getOpacity() * 255 * (1 - transparency.pixels(font))) / 255d);
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return "FadeoutColorConverter";
68+
}
69+
}

0 commit comments

Comments
 (0)