Java 将 RGB 颜色值转换为十六进制字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3607858/
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
Convert a RGB Color Value to a Hexadecimal String
提问by Lalchand
In my Java application, I was able to get the Color
of a JButton
in terms of red, green and blue; I have stored these values in three int
s.
在我的Java应用程序,我能得到Color
的JButton
中的红色,绿色和蓝色的条款; 我已将这些值存储在三个int
s 中。
How do I convert those RGB values into a String
containing the equivalent hexadecimal value?
如何将这些 RGB 值转换为String
包含等效十六进制值的值?
An Example would be "#0033fA"
一个例子是 "#0033fA"
采纳答案by mhshams
You can use
您可以使用
String hex = String.format("#%02x%02x%02x", r, g, b);
Use capital X's if you want your resulting hex-digits to be capitalized (#FFFFFF
vs. #ffffff
).
如果您希望生成的十六进制数字大写(#FFFFFF
vs. #ffffff
),请使用大写 X。
回答by Vivien Barousse
Random ra = new Random();
int r, g, b;
r=ra.nextInt(255);
g=ra.nextInt(255);
b=ra.nextInt(255);
Color color = new Color(r,g,b);
String hex = Integer.toHexString(color.getRGB() & 0xffffff);
if (hex.length() < 6) {
hex = "0" + hex;
}
hex = "#" + hex;
回答by Lonzak
A one liner but without String.format:
单行但没有 String.format:
Color your_color = Color.BLACK;
String hex = "#"+Integer.toHexString(your_color.getRGB()).substring(2);
You can add a .toUpperCase()
if you want to switch to capital letters.
.toUpperCase()
如果要切换为大写字母,可以添加 a 。
Note that ARRG is right - only to be used for non transparent colors.
请注意, ARRG 是正确的 - 仅用于非透明颜色。
if(your_color.getAlpha() >= 16){
String hex = "#"+Integer.toHexString(your_color.getRGB()).substring(2);
}
else{...}
回答by AlyssaFox
This is an adapted version of the answer given by Vivien Baroussewith the update from Vulcanapplied. In this example I use sliders to dynamically retreive the RGB values from three sliders and display that color in a rectangle. Then in method toHex() I use the values to create a color and display the respective Hex color code.
这是Vivien Barousse给出的答案的改编版本,并应用了Vulcan的更新。在此示例中,我使用滑块从三个滑块动态检索 RGB 值并将该颜色显示在矩形中。然后在 toHex() 方法中,我使用这些值来创建颜色并显示相应的十六进制颜色代码。
This example does not include the proper constraints for the GridBagLayout. Though the code will work, the display will look strange.
此示例不包括 GridBagLayout 的适当约束。虽然代码可以工作,但显示看起来很奇怪。
public class HexColor
{
public static void main (String[] args)
{
JSlider sRed = new JSlider(0,255,1);
JSlider sGreen = new JSlider(0,255,1);
JSlider sBlue = new JSlider(0,255,1);
JLabel hexCode = new JLabel();
JPanel myPanel = new JPanel();
GridBagLayout layout = new GridBagLayout();
JFrame frame = new JFrame();
//set frame to organize components using GridBagLayout
frame.setLayout(layout);
//create gray filled rectangle
myPanel.paintComponent();
myPanel.setBackground(Color.GRAY);
//In practice this code is replicated and applied to sGreen and sBlue.
//For the sake of brevity I only show sRed in this post.
sRed.addChangeListener(
new ChangeListener()
{
@Override
public void stateChanged(ChangeEvent e){
myPanel.setBackground(changeColor());
myPanel.repaint();
hexCode.setText(toHex());
}
}
);
//add each component to JFrame
frame.add(myPanel);
frame.add(sRed);
frame.add(sGreen);
frame.add(sBlue);
frame.add(hexCode);
} //end of main
//creates JPanel filled rectangle
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawRect(360, 300, 10, 10);
g.fillRect(360, 300, 10, 10);
}
//changes the display color in JPanel
private Color changeColor()
{
int r = sRed.getValue();
int b = sBlue.getValue();
int g = sGreen.getValue();
Color c;
return c = new Color(r,g,b);
}
//Displays hex representation of displayed color
private String toHex()
{
Integer r = sRed.getValue();
Integer g = sGreen.getValue();
Integer b = sBlue.getValue();
Color hC;
hC = new Color(r,g,b);
String hex = Integer.toHexString(hC.getRGB() & 0xffffff);
while(hex.length() < 6){
hex = "0" + hex;
}
hex = "Hex Code: #" + hex;
return hex;
}
}
A huge thank you to both Vivien and Vulcan. This solution works perfectly and was super simple to implement.
非常感谢 Vivien 和 Vulcan。该解决方案非常有效,而且实施起来非常简单。