Java 创造更亮的色彩?(爪哇)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18648142/
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
Creating Brighter Color? (Java)
提问by CJDKL
I was trying to create a brighter color using the default colors that java.awt.Color
provides
我试图使用java.awt.Color
提供的默认颜色创建更亮的颜色
but when I draw using the 2 colors, they appear to be the same?
但是当我使用 2 种颜色绘制时,它们看起来是一样的吗?
Color blue = Color.BLUE;
Color brighterBlue = blue.brighter();
test.setColor(blue);
test.fillCircle(size);
test.setColor(brighterBlue);
test.drawCircle(size);
回答by millimoose
Quoting the docs:
引用文档:
This method applies an arbitrary scale factor to each of the three RGB components of this Color to create a brighter version of this Color.
此方法将任意比例因子应用于此颜色的三个 RGB 分量中的每一个,以创建此颜色的更亮版本。
So, assuming that Color.blue
is the color rgb(0, 0, 255)
, the brighter method would attempt to:
因此,假设这Color.blue
是 color rgb(0, 0, 255)
,更亮的方法将尝试:
- Multiply the
0
s by the scale factor, resulting in 0s again; and - Multiply the
255
by a scale factor, which because of capping results in255
again.
- 将
0
s乘以比例因子,再次得到0s;和 - 乘以
255
一个比例因子,由于上限255
再次导致。
Do note that the hue-saturation-value(where "value" is the same as "brightness") colour coordinate model is not the same as the hue-saturation-lightnessmodel, which behaves somewhat more intuitively. (See Wikipedia on HSL and HSV.) Unfortunately Java doesn't have HSL calculations built in, but you should be able to find those by searching easily.
请注意,色调-饱和度-值(其中“值”与“亮度”相同)颜色坐标模型与色调-饱和度-亮度模型不同,后者表现得更直观一些。(请参阅有关HSL 和 HSV 的维基百科。)不幸的是,Java 没有内置 HSL 计算,但您应该能够通过轻松搜索找到这些计算。
回答by MadProgrammer
There are a lot of ways to approach this problem.
有很多方法可以解决这个问题。
The most obvious way is to add a fraction to an existing color value, for example...
最明显的方法是在现有颜色值上添加一个分数,例如...
int red = 128;
float fraction = 0.25f; // brighten by 25%
red = red + (red * float);
Instead, you could apply a load factor to the color instead. We know the maximum value for a color element is 255, so instead of trying to increasing the color factor itself, you could increase the color element by a factor of the over all color range, for example
相反,您可以将负载因子应用于颜色。我们知道一个颜色元素的最大值是 255,所以不要试图增加颜色因子本身,你可以将颜色元素增加整个颜色范围的一个因子,例如
int red = 128;
float fraction = 0.25f; // brighten by 25%
red = red + (255 * fraction); // 191.75
This basically increases the color element by a factor 255 instead...cheeky. Now we also need to take into consideration the fact that the color should never be greater than 255
这基本上将颜色元素增加了 255 倍......而不是厚脸皮。现在我们还需要考虑到颜色永远不应该大于 255 的事实
This will allow you to force colors to approach white as the brighten and black as the darken...
这将允许您强制颜色在变亮时接近白色,在变暗时接近黑色......
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class BrighterColor {
public static void main(String[] args) {
new BrighterColor();
}
public BrighterColor() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
Rectangle rec = new Rectangle(0, 0, getWidth(), getHeight() / 2);
g2d.setColor(Color.BLUE);
g2d.fill(rec);
g2d.translate(0, getHeight() / 2);
g2d.setColor(brighten(Color.BLUE, 0.25));
g2d.fill(rec);
g2d.dispose();
}
}
/**
* Make a color brighten.
*
* @param color Color to make brighten.
* @param fraction Darkness fraction.
* @return Lighter color.
*/
public static Color brighten(Color color, double fraction) {
int red = (int) Math.round(Math.min(255, color.getRed() + 255 * fraction));
int green = (int) Math.round(Math.min(255, color.getGreen() + 255 * fraction));
int blue = (int) Math.round(Math.min(255, color.getBlue() + 255 * fraction));
int alpha = color.getAlpha();
return new Color(red, green, blue, alpha);
}
}