| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.xtext.reportdsl.common; |
| |
| import org.eclipse.osbp.xtext.reportdsl.Color; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| /** |
| * Utilities to convert different Color types. |
| */ |
| public class ColorUtilities { |
| |
| private final static Logger log = LoggerFactory.getLogger(ColorUtilities.class); |
| |
| /** |
| * @param source a color attribute of the Report DSL Grammar |
| * @return a AWT color is the source is parseable or <code>java.awt.Color.BLACK</code> otherwise |
| */ |
| public static java.awt.Color convertToAwtColor(Color source) { |
| return convertToAwtColor(source.getColor()); |
| } |
| |
| /** |
| * @param source a color string value |
| * @return a AWT color is the source is parseable or <code>java.awt.Color.BLACK</code> otherwise |
| */ |
| public static java.awt.Color convertToAwtColor(String source) { |
| java.awt.Color result = java.awt.Color.BLACK; |
| if (source != null) { |
| if (source.charAt(0) == '#') { |
| int r = 0; |
| int g = 0; |
| int b = 0; |
| switch (source.length()) { |
| case 3: |
| r = parseHex(source.substring(1, 3)); |
| g = r; |
| b = r; |
| break; |
| case 4: |
| r = parseHex(""+source.charAt(1)+source.charAt(1)); |
| g = parseHex(""+source.charAt(2)+source.charAt(2)); |
| b = parseHex(""+source.charAt(3)+source.charAt(3)); |
| break; |
| case 7: |
| r = parseHex(source.substring(1, 3)); |
| g = parseHex(source.substring(3, 5)); |
| b = parseHex(source.substring(5, 7)); |
| break; |
| default: |
| log.error("convertToAwtColor(): hex '"+source+"' is not parseable due to wrong value length!"); |
| break; |
| } |
| result = new java.awt.Color(r, g, b); |
| } |
| else { |
| log.error("convertToAwtColor(): string '"+source+"' is not parseable due to wrong value format! Please use #rrggbb format!"); |
| } |
| } |
| else { |
| log.error("convertToAwtColor(): string '<null>' is not parseable!"); |
| } |
| return result; |
| } |
| |
| private static int parseHex(String hex) { |
| try { |
| return Integer.decode("0x"+hex); |
| } |
| catch (NumberFormatException nfe) { |
| log.error("convertToAwtColor(): hex '"+hex+"' is not parseable!"); |
| return 0; |
| } |
| } |
| } |