@@ -138,7 +138,7 @@ void define(final NamedWidgetColor color)
138138 * @param name Name of the color
139139 * @param color Named color
140140 */
141- void define (String name , final NamedWidgetColor color )
141+ void defineAlias (String name , final NamedWidgetColor color )
142142 {
143143 colors .put (name , color );
144144 }
@@ -152,6 +152,46 @@ public Optional<NamedWidgetColor> getColor(final String name)
152152 return Optional .ofNullable (colors .get (name ));
153153 }
154154
155+ sealed interface ColorDefinition permits RGBA , FromNamedWidgetColor , Alias {}
156+ private record RGBA (int red , int green , int blue , int alpha ) implements ColorDefinition { }
157+ private record FromNamedWidgetColor (NamedWidgetColor namedWidgetColor ) implements ColorDefinition { };
158+ private record Alias (NamedWidgetColor color ) implements ColorDefinition { };
159+
160+ /** Parse the color definition
161+ * @param colorDefinitionString Color definition
162+ * @return Optionally (when successful), an instance of ColorDefinition representing the result of parsing
163+ */
164+ public Optional <ColorDefinition > parseColorDefinition (final String colorDefinitionString )
165+ {
166+ String colorDefinitionStringTrimmed = colorDefinitionString .trim ();
167+ if (colorDefinitionStringTrimmed .startsWith ("alias(" )) {
168+ if (colorDefinitionStringTrimmed .endsWith (")" )) {
169+ String colorName = colorDefinitionStringTrimmed .substring (6 ,colorDefinitionStringTrimmed .length ()-1 ).trim ();
170+ NamedWidgetColor color = colors .get (colorName );
171+ return Optional .of (new Alias (color ));
172+ }
173+ else {
174+ return Optional .empty ();
175+ }
176+ }
177+ else if (colors .containsKey (colorDefinitionStringTrimmed )) {
178+ NamedWidgetColor color = colors .get (colorDefinitionStringTrimmed );
179+ return Optional .of (new FromNamedWidgetColor (color ));
180+ }
181+ else {
182+ final StringTokenizer tokenizer = new StringTokenizer (colorDefinitionString , "," );
183+ try {
184+ final int red = Integer .parseInt (tokenizer .nextToken ().trim ());
185+ final int green = Integer .parseInt (tokenizer .nextToken ().trim ());
186+ final int blue = Integer .parseInt (tokenizer .nextToken ().trim ());
187+ final int alpha = tokenizer .hasMoreTokens () ? Integer .parseInt (tokenizer .nextToken ().trim ()) : 255 ;
188+ return Optional .of (new RGBA (red , green , blue , alpha ));
189+ } catch (Throwable throwable ) {
190+ return Optional .empty ();
191+ }
192+ }
193+ }
194+
155195 /** Resolve a named color
156196 * @param color Predefined color
157197 * @return Color as provided unless it was redefined
@@ -171,30 +211,21 @@ public Collection<NamedWidgetColor> getColors()
171211
172212 @ Override
173213 protected void parse ( final String name , final String value ) throws Exception {
214+ Optional <ColorDefinition > optionalColorDefinition = parseColorDefinition (value );
174215
175- Optional <NamedWidgetColor > optionalColor = getColor (value );
176-
177- if ( optionalColor .isPresent () ) {
178-
179- NamedWidgetColor namedColor = optionalColor .get ();
180-
181- define (name , namedColor );
182-
216+ if (optionalColorDefinition .isEmpty ()) {
217+ throw new Exception ("Cannot parse color '" + name + "' from '" + value + "'" );
183218 } else {
184-
185- final StringTokenizer tokenizer = new StringTokenizer (value , "," );
186-
187- try {
188-
189- final int red = Integer .parseInt (tokenizer .nextToken ().trim ());
190- final int green = Integer .parseInt (tokenizer .nextToken ().trim ());
191- final int blue = Integer .parseInt (tokenizer .nextToken ().trim ());
192- final int alpha = tokenizer .hasMoreTokens () ? Integer .parseInt (tokenizer .nextToken ().trim ()) : 255 ;
193-
194- define (new NamedWidgetColor (name , red , green , blue , alpha ));
195-
196- } catch ( Throwable ex ) {
197- throw new Exception ("Cannot parse color '" + name + "' from '" + value + "'" , ex );
219+ ColorDefinition colorDefinition = optionalColorDefinition .get ();
220+ if (colorDefinition instanceof RGBA rgba ) {
221+ define (new NamedWidgetColor (name , rgba .red , rgba .green , rgba .blue , rgba .alpha ));
222+ } else if (colorDefinition instanceof FromNamedWidgetColor fromNamedWidgetColor ) {
223+ NamedWidgetColor namedWidgetColor = fromNamedWidgetColor .namedWidgetColor ;
224+ define (new NamedWidgetColor (name , namedWidgetColor .getRed (), namedWidgetColor .getGreen (), namedWidgetColor .getBlue (), namedWidgetColor .getAlpha ()));
225+ } else if (colorDefinition instanceof Alias alias ) {
226+ defineAlias (name , alias .color );
227+ } else {
228+ throw new Exception ("Unhandled case: " + colorDefinition .toString ()); // This should never happen.
198229 }
199230
200231 }
0 commit comments