Convert a RGB Color Value to a Hexadecimal String

前端 未结 4 1943
南旧
南旧 2020-11-30 01:06

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

4条回答
  •  鱼传尺愫
    2020-11-30 01:44

    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;
    

提交回复
热议问题