/
WCRadialGradient.java
131 lines (120 loc) · 4.61 KB
/
WCRadialGradient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.javafx.webkit.prism;
import com.sun.prism.paint.Color;
import com.sun.prism.paint.RadialGradient;
import com.sun.prism.paint.Stop;
import com.sun.webkit.graphics.WCGradient;
import com.sun.webkit.graphics.WCPoint;
import com.sun.javafx.geom.transform.BaseTransform;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
final class WCRadialGradient extends WCGradient<RadialGradient> {
static final Comparator<Stop> COMPARATOR = (s1, s2) -> {
float o1 = s1.getOffset();
float o2 = s2.getOffset();
if (o1 < o2) {
return -1;
}
if (o1 > o2) {
return 1;
}
return 0;
};
private final boolean reverse;
private final WCPoint p1;
private final WCPoint p2;
private final float r1over;
private final float r1;
private final float r2;
private final List<Stop> stops = new ArrayList<Stop>();
WCRadialGradient(WCPoint p1, float r1, WCPoint p2, float r2) {
this.reverse = r1 < r2;
this.p1 = this.reverse ? p2 : p1;
this.p2 = this.reverse ? p1 : p2;
this.r1 = this.reverse ? r2 : r1;
this.r2 = this.reverse ? r1 : r2;
this.r1over = (this.r1 > 0.0f)
? 1.0f / this.r1
: 0.0f;
}
@Override
protected void addStop(Color color, float offset) {
if (this.reverse) {
offset = 1.0f - offset;
}
offset = 1.0f - offset + offset * this.r2 * this.r1over;
this.stops.add(new Stop(color, offset));
}
public RadialGradient getPlatformGradient() {
Collections.sort(this.stops, COMPARATOR);
float dx = this.p2.getX() - this.p1.getX();
float dy = this.p2.getY() - this.p1.getY();
return new RadialGradient(
this.p1.getX(),
this.p1.getY(),
(float) (Math.atan2(dy, dx) * 180 / Math.PI),
(float) Math.sqrt(dx * dx + dy * dy) * this.r1over,
this.r1,
BaseTransform.IDENTITY_TRANSFORM,
isProportional(),
getSpreadMethod() - 1, // convert webkit to prism
this.stops);
}
@Override
public String toString() {
return toString(this, this.p1, this.p2, this.r1, this.stops);
}
static String toString(WCGradient g, WCPoint p1, WCPoint p2, Float radius, List<Stop> stops) {
StringBuilder sb = new StringBuilder(g.getClass().getSimpleName());
switch (g.getSpreadMethod()) {
case PAD:
sb.append("[spreadMethod=PAD");
break;
case REFLECT:
sb.append("[spreadMethod=REFLECT");
break;
case REPEAT:
sb.append("[spreadMethod=REPEAT");
break;
}
sb.append(", proportional=").append(g.isProportional());
if (radius != null) {
sb.append(", radius=").append(radius);
}
sb.append(", x1=").append(p1.getX());
sb.append(", y1=").append(p1.getY());
sb.append(", x2=").append(p2.getX());
sb.append(", y2=").append(p2.getY());
sb.append(", stops=");
for (int i = 0; i < stops.size(); i++) {
sb.append(i == 0 ? "[" : ", ");
sb.append(stops.get(i).getOffset()).append(":").append(stops.get(i).getColor());
}
return sb.append("]]").toString();
}
}