felix@1090: package de.intevation.flys.utils;
felix@1090: 
felix@1090: import java.awt.Color;
felix@1090: 
felix@1090: import org.w3c.dom.Document;
felix@1090: 
felix@1090: import de.intevation.artifacts.common.utils.XMLUtils;
felix@1090: 
felix@1090: 
felix@1090: /**
felix@1090:  * Utility to deal with themes and their representations.
felix@1090:  */
felix@1090: public class ThemeUtil {
felix@1090:     public final static String XPATH_LINE_COLOR = "/theme/field[@name='linecolor']/@default";
felix@1090: 
felix@1090:     /**
felix@1090:      * Parse a string like "103, 100, 0" and return a corresping color.
felix@1090:      * @param rgbtext Color as string representation, e.g. "255,0,20".
felix@1090:      * @return Color, null in case of issues.
felix@1090:      */
felix@1090:     public static Color parseRGB(String rgbtext) {
felix@1090:         if (rgbtext == null) {
felix@1090:             return null;
felix@1090:         }
felix@1090:         String rgb[] = rgbtext.split(",");
felix@1090:         Color c = null;
felix@1090:         try {
felix@1090:             c = new Color(
felix@1090:                     Integer.valueOf(rgb[0].trim()),
felix@1090:                     Integer.valueOf(rgb[1].trim()),
felix@1090:                     Integer.valueOf(rgb[2].trim()));
felix@1090:         }
felix@1090:         catch (NumberFormatException nfe) {
felix@1090:             c = null;
felix@1090:         }
felix@1090:         return c;
felix@1090:     }
felix@1090: 
felix@1090:     /**
felix@1090:      * Gets color from color field.
felix@1090:      * @param theme    the theme document.
felix@1090:      * @return color.
felix@1090:      */
felix@1090:     public static Color parseLineColorField(Document theme) {
felix@1090:         String color = XMLUtils.xpathString(theme, XPATH_LINE_COLOR, null);
felix@1090:         return parseRGB(color);
felix@1090:     }
felix@1090: }