【发布时间】:2011-04-06 04:34:44
【问题描述】:
在我的 Java 应用程序中,我能够得到 JButton 的红色、绿色和蓝色的 Color;我将这些值存储在三个ints 中。
如何将这些 RGB 值转换为包含等效十六进制表示的String?如#0033fA
【问题讨论】:
在我的 Java 应用程序中,我能够得到 JButton 的红色、绿色和蓝色的 Color;我将这些值存储在三个ints 中。
如何将这些 RGB 值转换为包含等效十六进制表示的String?如#0033fA
【问题讨论】:
即使 Alpha 通道值为零(例如 0000ff),也将 java.awt.Color 转换为 24 位十六进制 RGB 表示:
String.format("%06x", 0xFFFFFF & Color.BLUE.getRGB())
对于大写字母(例如0000FF):
String.format("%06X", 0xFFFFFF & Color.BLUE.getRGB())
【讨论】:
一个衬里,但没有 String.format 用于所有 RGB 颜色:
Color your_color = new Color(128,128,128);
String hex = "#"+Integer.toHexString(your_color.getRGB()).substring(2);
如果您想切换到大写字母,可以添加.toUpperCase()。请注意,这对于所有 RGB 颜色都是有效的(如问题中所述)。
当您拥有 ARGB 颜色时,您可以使用:
Color your_color = new Color(128,128,128,128);
String buf = Integer.toHexString(your_color.getRGB());
String hex = "#"+buf.substring(buf.length()-6);
理论上单行也是可行的,但需要调用 toHexString 两次。我对 ARGB 解决方案进行了基准测试,并将其与 String.format() 进行了比较:
【讨论】:
这是Vivien Barousse 给出的答案的改编版本,应用了Vulcan 的更新。在此示例中,我使用滑块从三个滑块中动态检索 RGB 值,并将该颜色显示在一个矩形中。然后在 toHex() 方法中,我使用这些值创建颜色并显示相应的十六进制颜色代码。
此示例不包括 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;
}
}
非常感谢 Vivien 和 Vulcan。该解决方案完美运行,实施起来超级简单。
【讨论】:
你可以使用
String hex = String.format("#%02x%02x%02x", r, g, b);
如果您希望生成的十六进制数字大写,请使用大写 X(#FFFFFF 与 #ffffff)。
【讨论】:
class java.util.IllegalFormatConversionException with message: x != java.lang.Float
String.format("#%06x", color.getRGB() & 0xFFFFFF);
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;
【讨论】:
Color.BLUE,它输出 #0ff,因为 &'ing Color.BLUE 的 RGB 值会导致 @987654324 @ 以 10 为底,即十六进制的 ff)。解决方法是在预置零时使用 while 循环而不是 if 语句。