使用JColorChooser获取HTML颜色代码 [英] Getting Html color codes with a JColorChooser

查看:107
本文介绍了使用JColorChooser获取HTML颜色代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从JColorChooser获取html颜色代码

Is there a way to get the html color code from a JColorChooser

我的Java Applet从用户那里获得三种颜色,并将它们取平均值并显示颜色

My java Applet takes three colors from the user and averages them and displays the color

我希望在查看平均颜色后获得html颜色代码

I want to get the html color code after they look at the average color

我该怎么做

推荐答案

编写一种将Color转换为String的方法.

Write a method to convert a Color to a String.

HTML颜色代码只是将R,G和B值转换为十六进制,并显示为前面带有井号的字符串.这是一种相当简单的编写方法.

An HTML color code is just the R, G, and B values converted to hex and displayed as a string with a pound sign in front. This is a fairly simple method to write.


public static String toHexString(Color c) {
  StringBuilder sb = new StringBuilder("#");

  if (c.getRed() < 16) sb.append('0');
  sb.append(Integer.toHexString(c.getRed()));

  if (c.getGreen() < 16) sb.append('0');
  sb.append(Integer.toHexString(c.getGreen()));

  if (c.getBlue() < 16) sb.append('0');
  sb.append(Integer.toHexString(c.getBlue()));

  return sb.toString();
}

这篇关于使用JColorChooser获取HTML颜色代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆