如何在大表面上制作平滑的 JavaFX LinearGradient?
为什么以下代码会导致块状渐变? 即渐变不平滑,您可以看到一些组成它的矩形。
有办法解决这个问题吗?
顺便说一句,我在 Vista 上运行这个,但我也在 Mac 上经历过这个。
var stage:Stage = Stage {
title: "Louis' Photo Wall"
width: 900
height: 600
scene: Scene {
content : Rectangle {
width: bind stage.scene.width
height: bind stage.scene.height
fill:LinearGradient {
startX : 0.0
startY : 0.0
endX : 0.0
endY : 1.0
stops: [
Stop {
color : Color {
red:0.0
blue:0.0
green:0.0
}
offset: 0.0
},
Stop {
color : Color {
red:0.8
blue:0.8
green:0.8
}
offset: 1.0
},
]
}
}//OuterRectangle
}
}
Why does the following code lead to a blocky gradient? i.e. the gradient is not smooth, you can see some of the rectangles that make it up.
Is there a way to fix this?
BTW I'm running this on Vista, but I've also experienced this on a Mac as well.
var stage:Stage = Stage {
title: "Louis' Photo Wall"
width: 900
height: 600
scene: Scene {
content : Rectangle {
width: bind stage.scene.width
height: bind stage.scene.height
fill:LinearGradient {
startX : 0.0
startY : 0.0
endX : 0.0
endY : 1.0
stops: [
Stop {
color : Color {
red:0.0
blue:0.0
green:0.0
}
offset: 0.0
},
Stop {
color : Color {
red:0.8
blue:0.8
green:0.8
}
offset: 1.0
},
]
}
}//OuterRectangle
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
块状数量并不引人注目。
转换为 1.0 的 endX 似乎会通过块状结果的对角化产生更明显的变化。
一个假设是有几件事正在发生。
1. r/g/b 颜色并不是真正连续的,而是有 256 级。 (8 位)。 255 的 0.8 是 204。
2. 当将一种颜色从 0 到 0.8 映射到 600 个像素时,每个像素都会获得增量,而这不能完全平滑。 当我跑步时,场景大小实际上是 792x566。 因此,在切换到下一种颜色之前,渐变将使用 566/204 = 2.775 像素表示一种颜色。 这会导致转换发生波动。
这并不能解释为什么使用 0.0 到 1.0 作为停止点(而不是 0.0 到 0.8)会导致(至少在我的跑步中)看似平滑的过渡。
后续:LinearGradient 可能使用一个
ofTheWay
方法进行插值。 代码示例和结果...打印
The blocky amount is not dramatic.
Shifting to a endX of 1.0 seems to give a more obvious variation with a diagonalization of the blocky result.
A hypothesis is that a couple of things are going on.
1. A r/g/b color is not really continuous, but has 256 steps. (8 bits). A 0.8 of 255 is 204.
2. When one maps from 0 to 0.8 for a color into 600 pixels, one gets an increment per pixel that cannot be completely smooth. The scene size is actually 792x566 when I make a run. So the gradient would use has 566/204 = 2.775 pixels for one color before shifting to the next. That causes fluctuation where transitions are made.
This doesn't explain why using 0.0 to 1.0 for the stops (instead of 0.0 to 0.8) results (at least in my runs) with what seems to be a smooth transition.
Follow-up: There is an
ofTheWay
method that LinearGradient probably uses for interpolation. A code example and results...which prints