Debian Codegraph @

src/com/jidesoft/converter/HexColorConverter.java

raw
1 /*
2 * @(#) HexColorConverter.java
3 *
4 * Copyright 2002 - 2003 JIDE Software. All rights reserved.
5 */
6 package com.jidesoft.converter;
7
8 import java.awt.*;
9
10 /**
11 * If alpha value is not included, converts Color to/from #xxxxxx format. For example #000000 is Color(0, 0, 0) and
12 * #FF00FF is Color(255, 0, 255). If alpha value is included, converts Color to/from #xxxxxxxx format. For example
13 * #FF000000 is Color(0, 0, 0, 255) and #64FF00FF is Color(255, 0, 255, 100).
14 */
15 public class HexColorConverter extends ColorConverter {
16
17 private boolean _alphaIncluded = false;
18
19 /**
20 * Creates a HexColorConverter. This is the default constructor and will not include alpha value.
21 */
22 public HexColorConverter() {
23 }
24
25 /**
26 * Creates a HexColorConverter. With this constructor, you can create a converter with alpha value included.
27 *
28 * @param alphaIncluded the flag if alpha value will be included in this converter
29 */
30 public HexColorConverter(boolean alphaIncluded) {
31 _alphaIncluded = alphaIncluded;
32 }
33
34 /**
35 * Get the flag if this converter should consider alpha value.
36 * <p/>
37 * If you use default constructor, the default value of this flag is false.
38 * <p/>
39 *
40 * @return true if this converter should consider alpha value.
41 *
42 * @see HexColorConverter
43 */
44 public boolean isAlphaIncluded() {
45 return _alphaIncluded;
46 }
47
48 /**
49 * Set the flag if this converter should consider alpha value.
50 * <p/>
51 *
52 * @param alphaIncluded the flag if this converter should consider alpha value.
53 * @see #isAlphaIncluded()
54 */
55 public void setAlphaIncluded(boolean alphaIncluded) {
56 _alphaIncluded = alphaIncluded;
57 }
58
59 protected String getHexString(int color) {
60 String value = Integer.toHexString(color).toUpperCase();
61 if (value.length() == 1) {
62 value = "0" + value;
63 }
64 return value;
65 }
66
67 public String toString(Object object, ConverterContext context) {
68 if (object instanceof Color) {
69 Color color = (Color) object;
70 StringBuffer colorText = new StringBuffer("#");
71 if (isAlphaIncluded()) {
72 colorText.append(getHexString(color.getAlpha()));
73 }
74 colorText.append(getHexString(color.getRed()));
75 colorText.append(getHexString(color.getGreen()));
76 colorText.append(getHexString(color.getBlue()));
77 return new String(colorText);
78 }
79 else {
80 return "";
81 }
82 }
83
84 public boolean supportToString(Object object, ConverterContext context) {
85 return true;
86 }
87
88 public boolean supportFromString(String string, ConverterContext context) {
89 return true;
90 }
91
92 public Object fromString(String string, ConverterContext context) {
93 if (string == null || string.trim().length() == 0) {
94 return null;
95 }
96 if (string.startsWith("#")) {
97 string = string.substring(1);
98 }
99 if (isAlphaIncluded()) {
100 if (string.length() > 8) {
101 string = string.substring(string.length() - 8);
102 }
103 }
104 else {
105 if (string.length() > 6) {
106 string = string.substring(string.length() - 6);
107 }
108 }
109 long value;
110 try {
111 value = Long.parseLong(string, 16);
112 }
113 catch (NumberFormatException e) {
114 return null;
115 }
116 return new Color((int) value, isAlphaIncluded());
117 }
118 }