1. package org.jr.awt;
  2. /**
  3. * Copyright: Copyright (c) 2002-2004
  4. * Company: JavaResearch(http://www.javaresearch.org)
  5. * 最后更新日期:2003年3月5日
  6. * @author Cherami
  7. */
  8. import java.awt.*;
  9. /**
  10. * AWT工具类,提供常见的AWT相关的工具方法。
  11. * @since 0.5
  12. */
  13. public class AWTUtil {
  14. /**
  15. * 从HTML格式的字符串得到Color对象。
  16. * 其前导'#'符号是可选的。
  17. * @param color 字符串格式的颜色值
  18. * @return 对应的颜色,字符串不是合法格式时返回null
  19. * @since 0.5
  20. */
  21. public static Color getColor(String color) {
  22. if (color.charAt(0) == '#') {
  23. color = color.substring(1);
  24. }
  25. if (color.length() != 6) {
  26. return null;
  27. }
  28. try {
  29. int r = Integer.parseInt(color.substring(0, 2), 16);
  30. int g = Integer.parseInt(color.substring(2, 4), 16);
  31. int b = Integer.parseInt(color.substring(4), 16);
  32. return new Color(r, g, b);
  33. }
  34. catch (NumberFormatException e) {
  35. return null;
  36. }
  37. }
  38. }