Convert a RGB Color Value to a Hexadecimal

匿名 (未验证) 提交于 2019-12-03 03:10:03

问题:

In my Java application, I was able to get the value of JButton color in terms of Red, Green, Blue values; I have stored these values in three integers.

How to convert RGB values into the equivalent hexadecimal value?

Example of that like in this format #0033fA

回答1:

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).



回答2:

A one liner but without 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.



回答3:

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() 


回答4:

This is an adapted version of the answer given by Vivien Barousse with the update from Vulcan applied. 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.

This example does not include the proper constraints for the GridBagLayout. Though the code will work, the display will look strange.

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() 

A huge thank you to both Vivien and Vulcan. This solution works perfectly and was super simple to implement.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!