1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.misc.converter; |
|
27 |
|
28 import java.awt.Color; |
|
29 import java.util.*; |
|
30 import com.oracle.solaris.vp.util.misc.TextUtil; |
|
31 |
|
32 public class ColorStringConverter extends StringConverter<Color> { |
|
33 // |
|
34 // Static data |
|
35 // |
|
36 |
|
37 public static final ColorStringConverter INSTANCE = |
|
38 new ColorStringConverter(); |
|
39 |
|
40 // |
|
41 // Converter methods |
|
42 // |
|
43 |
|
44 /** |
|
45 * Converts the given {@code String} to a {@code Color}. |
|
46 * |
|
47 * @param s |
|
48 * the {@code String} to convert: a list of one, three, or four |
|
49 * integers, separated by whitespace and/or commas; these |
|
50 * correspond to the arguments of the various {@code Color} |
|
51 * constructors. |
|
52 * |
|
53 * @return {@code null} if {@code s} is {@code null}, a {@code Color} |
|
54 * otherwise |
|
55 * |
|
56 * @exception IllegalArgumentException |
|
57 * if the given {@code String} could not be converted to a |
|
58 * {@code Color} |
|
59 */ |
|
60 @Override |
|
61 public Color convert(String s) { |
|
62 if (s == null) { |
|
63 return null; |
|
64 } |
|
65 |
|
66 List<Integer> list = new ArrayList<Integer>(); |
|
67 String[] groups; |
|
68 |
|
69 String remaining = s; |
|
70 while ((groups = TextUtil.match( |
|
71 remaining, "^[\\s,]*(\\d+)(.*)")) != null) { |
|
72 list.add(new Integer(groups[1])); |
|
73 remaining = groups[2]; |
|
74 } |
|
75 |
|
76 switch (list.size()) { |
|
77 // Single RGB int |
|
78 case 1: |
|
79 return new Color((int)list.get(0)); |
|
80 |
|
81 // R, G, B |
|
82 case 3: |
|
83 return new Color((int)list.get(0), (int)list.get(1), |
|
84 (int)list.get(2)); |
|
85 |
|
86 // R, G, B, A |
|
87 case 4: |
|
88 return new Color((int)list.get(0), (int)list.get(1), |
|
89 (int)list.get(2), (int)list.get(3)); |
|
90 } |
|
91 |
|
92 throw new IllegalArgumentException( |
|
93 String.format("invalid color: %s", s)); |
|
94 } |
|
95 |
|
96 // |
|
97 // DualConverter methods |
|
98 // |
|
99 |
|
100 @Override |
|
101 public String revert(Color c) { |
|
102 return String.format("%d, %d, %d, %d", |
|
103 c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha()); |
|
104 } |
|
105 } |
|