package org.csstudio.utility.adlparser.fileParser;

import java.util.ArrayList;

import org.csstudio.utility.adlparser.internationalization.Messages;
import org.eclipse.swt.graphics.RGB;

public class ColorMap {
	private RGB[] colors = new RGB[0];
	private int numColors;
	
	public ColorMap(ADLWidget colorMap) throws WrongADLFormatException {
		// find out the number of colors
		
		for (FileLine fileLine : colorMap.getBody()){
			String bodyPart = fileLine.getLine();
			String[] row = bodyPart.trim().split("=");
			if (row.length < 2){
				throw new WrongADLFormatException(Messages.Label_WrongADLFormatException_Parameter_Begin + bodyPart + Messages.Label_WrongADLFormatException_Parameter_End);
			}
			if (row[0].trim().equals("ncolors")){
				numColors = Integer.parseInt(row[1].replaceAll("\"", "").trim());
			}
			colors = new RGB[numColors];
		}
			

		ADLWidget colorsWidget = colorMap.getObjects().get(0);
		if (colorsWidget.getType().equals("colors")){
			int colorCounter = 0;
			for (FileLine fileLine  : colorsWidget.getBody()){
				String bodyPart = fileLine.getLine();
				String[] row = bodyPart.split(",");
				if (row.length == 1){
					String color = row[0].trim();
					colors[colorCounter] = getRGBColor(color);
					colorCounter++;
				}
			}
			
		}
	}

	/** 
	 * @return Object[] colors
	 */
	public RGB[] getColors() {
		return colors;
	}

	public int getNumColors() {
		return numColors;
	}
	public RGB getRGBColor(String inColor){
		String redStr = "#".concat(inColor.substring(0, 2));
		String greenStr = "#".concat(inColor.substring(2, 4));
		String blueStr = "#".concat(inColor.substring(4, 6));
		int red = Integer.decode(redStr);
		int green = Integer.decode(greenStr);
		int blue = Integer.decode(blueStr);
		System.out.println("Red, Green, Blue:"+ red +", " + green + ", " +blue);
		return new RGB(red, green, blue);
	}
}

@@ -1272,19 +1272,1052 @@
 %09%09%09%0A%09%09%7D%0A
+%09%09else if ( colorsWidget.getType().equals(%22dl_color%22))%7B%0A%09%09%09ArrayList%3CADLWidget%3E colorList = colorMap.getObjects();%0A%09%09%09if (colorList.size() == numColors) %7B%0A%09%09%09%09for (int ii=0; ii%3CnumColors; ii++)%7B%0A%09%09%09%09%09int red=0, green=0, blue =0;%0A%09%09%09%09%09ADLWidget dl_color = colorList.get(ii);%0A%09%09%09%09%09for ( FileLine fileLine : dl_color.getBody())%7B%0A%09%09%09%09%09%09String bodyPart = fileLine.getLine();%0A%09%09%09%09%09%09String%5B%5D row = bodyPart.trim().split(%22=%22);%0A%09%09%09%09%09%09if (row.length %3C 2)%7B%0A%09%09%09%09%09%09%09throw new WrongADLFormatException(Messages.Label_WrongADLFormatException_Parameter_Begin + bodyPart + Messages.Label_WrongADLFormatException_Parameter_End);%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09if (row%5B0%5D.trim().equals(%22r%22))%7B%0A%09%09%09%09%09%09%09red = Integer.parseInt(row%5B1%5D.replaceAll(%22%5C%22%22, %22%22).trim());%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09if (row%5B0%5D.trim().equals(%22g%22))%7B%0A%09%09%09%09%09%09%09green = Integer.parseInt(row%5B1%5D.replaceAll(%22%5C%22%22, %22%22).trim());%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09if (row%5B0%5D.trim().equals(%22b%22))%7B%0A%09%09%09%09%09%09%09blue = Integer.parseInt(row%5B1%5D.replaceAll(%22%5C%22%22, %22%22).trim());%09%09%09%09%09%09%09%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09colors%5Bii%5D = new RGB(red,green,blue);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%7D%0A
-
 %0A%09/** %0A%09
@@ -2730,24 +2730,24 @@
 (greenStr);%0A
+
 %09%09int blue =
@@ -2776,83 +2776,8 @@
 r);%0A
-%09%09System.out.println(%22Red, Green, Blue:%22+ red +%22, %22 + green + %22, %22 +blue);%0A
 %09%09re

Add in dl_color interpretation