向指定的文件中写入一个序列化的对象- -| 回首页 | 2005年索引 | - -转换字符串中的正则表达式字符

将#RRGGBB形式的字符串转换为Color对象- -

                                      

将#RRGGBB形式的字符串转换为Color对象 发表时间: 2005-01-28 09:50
回复
前导'#'符号可有可无。
  1.   public static Color getColor(String color) {
  2.     if (color.charAt(0) == '#') {
  3.       color = color.substring(1);
  4.     }
  5.     if (color.length() != 6) {
  6.       return null;
  7.     }
  8.     try {
  9.       int r = Integer.parseInt(color.substring(0, 2), 16);
  10.       int g = Integer.parseInt(color.substring(2, 4), 16);
  11.       int b = Integer.parseInt(color.substring(4), 16);
  12.       return new Color(r, g, b);
  13.     }
  14.     catch (NumberFormatException e) {
  15.       return null;
  16.     }
  17.   }

- 作者: fqydwu 访问统计: 2005年10月22日, 星期六 10:12 加入博采

Trackback

你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=3291506

博客手拉手

[2005-10-22 02:53:27.0]    全能音频转换通

[2005-10-22 02:52:32.0]    天亿图片格式转换器

[2005-10-22 04:36:39.0]    日期程序库

[2005-10-22 08:21:34.0]    电厂运行技术问答

[2005-10-22 06:25:13.0]    一段真实的录音

回复

评论内容: