/*******************************************************************************
 * This file is part of TERMINAL RECALL
 * Copyright (c) 2012-2015 Chuck Ritola
 * Part of the jTRFP.org project
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     chuck - initial API and implementation
 ******************************************************************************/
package org.jtrfp.trcl.file;

import java.awt.Color;

import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
import org.jtrfp.jfdt.Parser;
import org.jtrfp.jfdt.ThirdPartyParseable;
import org.jtrfp.jfdt.UnrecognizedFormatException;

public class LTEFile implements ThirdPartyParseable {
    private byte [] lteData;
    @Override
    public void describeFormat(Parser parser)
	    throws UnrecognizedFormatException {
	parser.bytesOfCount(4096, parser.property("lteData", byte[].class));
    }//end describeFormat(...)
    /**
     * @return the lteData
     */
    public byte[] getLteData() {
        return lteData;
    }
    /**
     * @param lteData the lteData to set
     */
    public void setLteData(byte[] lteData) {
        this.lteData = lteData;
    }
    
    public int gradientIndex(int colorIndex, int lightLevel){
	if(lightLevel<0 || lightLevel>15)
	    throw new IllegalArgumentException("Lightlevel should be in range [0,15]. Got "+lightLevel);
	if(colorIndex<0 || colorIndex>255){
	    throw new IllegalArgumentException("ColorIndex should be in range [0,255]. Got "+colorIndex);
	}
	return lteData[colorIndex*16+lightLevel]&0xFF;
    }//end gradientIndex(...)
    /**
     * Reverse-compatible form of the emissive components in ESTuTv form where:<br>
     * R = E<br>
     * G = S<br>
     * B = Tu<br>
     * A = Tv<br>
     * @return
     * @since Feb 5, 2015
     */
    public Color[] toColors(Color [] referencePalette) {
	Color[] result = new Color[256];
	for(int i=0; i<256; i++){
	    final Color loColor = referencePalette[gradientIndex(i,0)];
	    final Color hiColor = referencePalette[gradientIndex(i,15)];
	    final double loIntensity = new Vector3D(loColor.getRed(),loColor.getGreen(),loColor.getBlue()).getNorm();
	    final double hiIntensity = new Vector3D(hiColor.getRed(),hiColor.getGreen(),hiColor.getBlue()).getNorm();
	    
	    final boolean isEmissive = (Math.abs(hiIntensity - loIntensity)<30);
	    result[i]=new Color(isEmissive?255:0,0,0,0);
	    }
	return result;
    }
}//end LTEFile

@@ -2097,16 +2097,63 @@
 ; i++)%7B%0A
+%09    final Color rColor = referencePalette%5Bi%5D;%0A
 %09    fin
@@ -2205,24 +2205,24 @@
 ndex(i,0)%5D;%0A
-
 %09    final C
@@ -2382,24 +2382,41 @@
 .getNorm();%0A
+%09    //Brightest%0A
 %09    final d
@@ -2535,29 +2535,195 @@
 nal 
-boolean isEmissive = 
+float %5B%5DhsbVals = new float%5B3%5D;%0A%09    Color.RGBtoHSB(rColor.getRed(), rColor.getGreen(), rColor.getBlue(), hsbVals);%0A%09    final double saturation = hsbVals%5B1%5D;%0A%09    double dI = Math.pow(1-
 (Mat
@@ -2739,19 +2739,17 @@
 ntensity
- - 
+-
 loIntens
@@ -2756,12 +2756,93 @@
 ity)
-%3C30)
+/442.),1);%0A%09    //dI = nSaturation*128;%0A%09    if(dI%3E255)dI=255;%0A%09    else if(dI%3C0)dI=0
 ;%0A%09 
@@ -2868,24 +2868,33 @@
 lor(
-isEmissive?255:0
+(int)(dI*saturation*255.)
 ,0,0
@@ -2921,21 +2921,37 @@
 result;%0A
-
     %7D
+//end toColors()
 %0A%7D//end 

Add saturation weighting to LTE->S conversion.