Java 以编程方式生成梯度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generating gradients programmatically?
提问by Paul Wicks
Given 2 rgb colors and a rectangular area, I'd like to generate a basic linear gradient between the colors. I've done a quick search and the only thing I've been able to find is this blog entry, but the example code seems to be missing, or at least it was as of this posting. Anything helps, algorithms, code examples, whatever. This will be written in Java, but the display layer is already taken care of, I just need to figure out how to figure out what to display.
给定 2 个 rgb 颜色和一个矩形区域,我想在颜色之间生成一个基本的线性渐变。我进行了快速搜索,唯一能找到的是这个博客条目,但示例代码似乎丢失了,或者至少在这篇文章中是这样。任何帮助,算法,代码示例,等等。这个会用Java写,但是显示层已经处理好了,我只需要弄清楚如何弄清楚要显示的内容。
采纳答案by Konrad Rudolph
you want an interpolation between the first and the second colour. Interpolating colours is easy by calculating the same interpolation for each of its components (R, G, B). There are many ways to interpolate. The easiest is to use linear interpolation: just take percentage pof the first colour and percentage 1 - pof the second:
您想要在第一种颜色和第二种颜色之间进行插值。通过为其每个分量(R、G、B)计算相同的插值,插值颜色很容易。有多种插值方法。最简单的方法是使用线性插值:只需取第一种颜色的百分比p和第二种颜色的百分比 1 - p:
R = firstCol.R * p + secondCol.R * (1 - p)
There's another questionrelated to this.
There are other methods of interpolation that sometimes work better. For example, using a bell-shaped (sigmoidal)interpolation function makes the transition smoother.
还有其他插值方法有时效果更好。例如,使用钟形(sigmoidal)插值函数可以使过渡更平滑。
/EDIT: Oops, you mean using a predefined function. OK, even easier. The blog post you linked now has an example code in Python.
/编辑:哎呀,你的意思是使用预定义的函数。好吧,更简单了。您链接的博客文章现在有一个 Python 示例代码。
In Java, you could use the GradientPaint
.
在 Java 中,您可以使用GradientPaint
.
回答by David Crow
Using the basic AWT classes, you could do something like this:
使用基本的 AWT 类,您可以执行以下操作:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
public class LinearGradient extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Color color1 = Color.RED;
Color color2 = Color.BLUE;
int steps = 30;
int rectWidth = 10;
int rectHeight = 10;
for (int i = 0; i < steps; i++) {
float ratio = (float) i / (float) steps;
int red = (int) (color2.getRed() * ratio + color1.getRed() * (1 - ratio));
int green = (int) (color2.getGreen() * ratio + color1.getGreen() * (1 - ratio));
int blue = (int) (color2.getBlue() * ratio + color1.getBlue() * (1 - ratio));
Color stepColor = new Color(red, green, blue);
Rectangle2D rect2D = new Rectangle2D.Float(rectWidth * i, 0, rectWidth, rectHeight);
g2.setPaint(stepColor);
g2.fill(rect2D);
}
}
}
回答by dbkk
You can use the built in GradientPaintclass.
您可以使用内置的GradientPaint类。
void Paint(Graphics2D g, Regtangle r, Color c1, Color c2)
{
GradientPaint gp = new GradientPaint(0,0,c1,r.getWidth(),r.getHeight(),c2);
g.setPaint(gp);
g.fill(rect);
}
回答by Thibaut Barrère
I've been using RMagick for that. If you need to go further the simple gradient, ImageMagick and one of its wrappers (like RMagick or JMagickfor Java) could be useful.
为此,我一直在使用RMagick。如果您需要更进一步的简单渐变,ImageMagick 及其包装器之一(如Java 的RMagick 或JMagick)可能会很有用。
回答by Holger Brandl
Following up on the execllent answer of David Crow, here's a Kotlin example implementation
跟进 David Crow 的回答,这是一个 Kotlin 示例实现
fun gradientColor(x: Double, minX: Double, maxX: Double,
from: Color = Color.RED, to: Color = Color.GREEN): Color {
val range = maxX - minX
val p = (x - minX) / range
return Color(
from.red * p + to.red * (1 - p),
from.green * p + to.green * (1 - p),
from.blue * p + to.blue * (1 - p),
1.0
)
}