Added multi-color specifications to the model of HyperCon (not GUI yet)

Former-commit-id: 3b3f38f1514bd2925e043333555461b3af1f7d88
This commit is contained in:
T. van der Zwan 2013-11-25 12:16:16 +01:00
parent ba54f3d8ce
commit 336647b95b
4 changed files with 210 additions and 101 deletions

View File

@ -8,19 +8,19 @@ import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.util.Properties;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.Vector;
public class ConfigurationFile {
private final Properties pProps = new Properties();
private final Properties mProps = new Properties();
public void load(String pFilename) {
pProps.clear();
mProps.clear();
// try (InputStream in = new InflaterInputStream(new FileInputStream(pFilename))){
try (InputStream in = new GZIPInputStream(new FileInputStream(pFilename))){
// try (InputStream in = new FileInputStream(pFilename)) {
pProps.load(in);
// try (InputStream in = new GZIPInputStream(new FileInputStream(pFilename))){
try (InputStream in = new FileInputStream(pFilename)) {
mProps.load(in);
} catch (Throwable t) {
// TODO Auto-generated catch block
t.printStackTrace();
@ -29,15 +29,18 @@ public class ConfigurationFile {
public void save(String pFilename) {
// try (OutputStream out = new DeflaterOutputStream(new FileOutputStream(pFilename))) {
try (OutputStream out = new GZIPOutputStream(new FileOutputStream(pFilename))) {
// try (OutputStream out = (new FileOutputStream(pFilename))) {
pProps.store(out, "Pesistent settings file for HyperCon");
// try (OutputStream out = new GZIPOutputStream(new FileOutputStream(pFilename))) {
try (OutputStream out = (new FileOutputStream(pFilename))) {
mProps.store(out, "Pesistent settings file for HyperCon");
} catch (IOException e) {
e.printStackTrace();
}
}
public void store(Object pObj) {
store(pObj, pObj.getClass().getSimpleName(), "");
}
public void store(Object pObj, String preamble, String postamble) {
String className = pObj.getClass().getSimpleName();
// Retrieve the member variables
Field[] fields = pObj.getClass().getDeclaredFields();
@ -48,27 +51,99 @@ public class ConfigurationFile {
continue;
}
String key = className + "." + field.getName();
String key = preamble + "." + field.getName() + postamble;
try {
Object value = field.get(pObj);
if (value.getClass().isEnum()) {
pProps.setProperty(key, ((Enum<?>)value).name());
mProps.setProperty(key, ((Enum<?>)value).name());
} else if (value.getClass().isAssignableFrom(Vector.class)) {
@SuppressWarnings("unchecked")
Vector<Object> v = (Vector<Object>) value;
for (int i=0; i<v.size(); ++i) {
store(v.get(i), key + "[" + i + "]", "");
}
} else {
pProps.setProperty(key, value.toString());
mProps.setProperty(key, value.toString());
}
} catch (Throwable t) {}
}
}
public void restore(Object pObj) {
String className = pObj.getClass().getSimpleName();
restore(pObj, mProps);
}
public void restore(Object pObj, Properties pProps) {
String className = pObj.getClass().getSimpleName();
restore(pObj, pProps, className + ".");
}
@SuppressWarnings("unchecked")
public void restore(Object pObj, Properties pProps, String pPreamble) {
// Retrieve the member variables
Field[] fields = pObj.getClass().getDeclaredFields();
// Iterate each variable
for (Field field : fields) {
String key = className + "." + field.getName();
if (field.getType().isAssignableFrom(Vector.class)) {
// Obtain the Vector
Vector<Object> vector;
try {
vector = (Vector<Object>)field.get(pObj);
} catch (Throwable t) {
t.printStackTrace();
break;
}
// Clear existing elements from the vector
vector.clear();
// Iterate through the properties to find the indices of the vector
int i=0;
while (true) {
String curIndexKey = pPreamble + field.getName() + "[" + i + "]";
Properties elemProps = new Properties();
// Find all the elements for the current vector index
for (Object keyObj : pProps.keySet()) {
String keyStr = (String)keyObj;
if (keyStr.startsWith(curIndexKey)) {
// Remove the name and dot
elemProps.put(keyStr.substring(curIndexKey.length()+1), pProps.get(keyStr));
}
}
if (elemProps.isEmpty()) {
// Found no more elements for the vector
break;
}
// Construct new instance of vectors generic type
ParameterizedType vectorElementType = (ParameterizedType) field.getGenericType();
Class<?> vectorElementClass = (Class<?>) vectorElementType.getActualTypeArguments()[0];
// Find the constructor with no arguments and create a new instance
Object newElement = null;
try {
newElement = vectorElementClass.getConstructor().newInstance();
} catch (Throwable t) {
System.err.println("Failed to find empty default constructor for " + vectorElementClass.getName());
break;
}
if (newElement == null) {
System.err.println("Failed to construct instance for " + vectorElementClass.getName());
break;
}
// Restore the instance members from the collected properties
restore(newElement, elemProps, "");
// Add the instance to the vector
vector.addElement(newElement);
++i;
}
continue;
}
String key = pPreamble + field.getName();
String value = pProps.getProperty(key);
if (value == null) {
System.out.println("Persistent settings does not contain value for " + key);
@ -97,6 +172,6 @@ public class ConfigurationFile {
@Override
public String toString() {
return pProps.toString();
return mProps.toString();
}
}

View File

@ -16,6 +16,7 @@ import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.hyperion.hypercon.spec.ColorConfig;
import org.hyperion.hypercon.spec.TransformConfig;
/**
* Configuration panel for the ColorConfig.
@ -24,7 +25,7 @@ import org.hyperion.hypercon.spec.ColorConfig;
*/
public class ColorPanel extends JPanel {
private final ColorConfig mColorConfig;
private final TransformConfig mColorConfig;
private JPanel mRgbTransformPanel;
private JLabel mThresholdLabel;
@ -56,7 +57,7 @@ public class ColorPanel extends JPanel {
public ColorPanel(ColorConfig pColorConfig) {
super();
mColorConfig = pColorConfig;
mColorConfig = pColorConfig.mTransforms.get(0);
initialise();
}

View File

@ -1,42 +1,19 @@
package org.hyperion.hypercon.spec;
import java.util.List;
import java.util.Locale;
import java.util.Vector;
/**
* The color tuning parameters of the different color channels (both in RGB space as in HSV space)
*/
public class ColorConfig {
/** The saturation gain (in HSV space) */
public double mSaturationGain = 1.0;
/** The value gain (in HSV space) */
public double mValueGain = 1.5;
/** The minimum required RED-value (in RGB space) */
public double mRedThreshold = 0.1;
/** The gamma-curve correct for the RED-value (in RGB space) */
public double mRedGamma = 2.0;
/** The black-level of the RED-value (in RGB space) */
public double mRedBlacklevel = 0.0;
/** The white-level of the RED-value (in RGB space) */
public double mRedWhitelevel = 0.8;
/** The minimum required GREEN-value (in RGB space) */
public double mGreenThreshold = 0.1;
/** The gamma-curve correct for the GREEN-value (in RGB space) */
public double mGreenGamma = 2.0;
/** The black-level of the GREEN-value (in RGB space) */
public double mGreenBlacklevel = 0.0;
/** The white-level of the GREEN-value (in RGB space) */
public double mGreenWhitelevel = 1.0;
/** The minimum required BLUE-value (in RGB space) */
public double mBlueThreshold = 0.1;
/** The gamma-curve correct for the BLUE-value (in RGB space) */
public double mBlueGamma = 2.0;
/** The black-level of the BLUE-value (in RGB space) */
public double mBlueBlacklevel = 0.0;
/** The white-level of the BLUE-value (in RGB space) */
public double mBlueWhitelevel = 1.0;
/** List with color transformations */
public List<TransformConfig> mTransforms = new Vector<>();
{
mTransforms.add(new TransformConfig());
}
public boolean mSmoothingEnabled = false;
/** The type of smoothing algorithm */
@ -70,65 +47,20 @@ public class ColorConfig {
strBuf.append("\t\"color\" :\n");
strBuf.append("\t{\n");
strBuf.append(hsvToJsonString() + ",\n");
strBuf.append(rgbToJsonString() + ",\n");
strBuf.append("\t\t\"transform\" :\n");
strBuf.append("\t\t[\n");
for (TransformConfig transform : mTransforms) {
strBuf.append(transform.toJsonString());
}
strBuf.append("\t\t]\n");
strBuf.append(smoothingToString() + "\n");
strBuf.append("\t}");
return strBuf.toString();
}
/**
* Creates the JSON string of the HSV-subconfiguration as used in the Hyperion deamon configfile
*
* @return The JSON string of the HSV-config
*/
private String hsvToJsonString() {
StringBuffer strBuf = new StringBuffer();
strBuf.append("\t\t\"hsv\" :\n");
strBuf.append("\t\t{\n");
strBuf.append(String.format(Locale.ROOT, "\t\t\t\"saturationGain\" : %.4f,\n", mSaturationGain));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\"valueGain\" : %.4f\n", mValueGain));
strBuf.append("\t\t}");
return strBuf.toString();
}
/**
* Creates the JSON string of the RGB-subconfiguration as used in the Hyperion deamon configfile
*
* @return The JSON string of the RGB-config
*/
private String rgbToJsonString() {
StringBuffer strBuf = new StringBuffer();
strBuf.append("\t\t\"red\" :\n");
strBuf.append("\t\t{\n");
strBuf.append(String.format(Locale.ROOT, "\t\t\t\"threshold\" : %.4f,\n", mRedThreshold));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\"gamma\" : %.4f,\n", mRedGamma));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\"blacklevel\" : %.4f,\n", mRedBlacklevel));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\"whitelevel\" : %.4f\n", mRedWhitelevel));
strBuf.append("\t\t},\n");
strBuf.append("\t\t\"green\" :\n");
strBuf.append("\t\t{\n");
strBuf.append(String.format(Locale.ROOT, "\t\t\t\"threshold\" : %.4f,\n", mGreenThreshold));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\"gamma\" : %.4f,\n", mGreenGamma));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\"blacklevel\" : %.4f,\n", mGreenBlacklevel));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\"whitelevel\" : %.4f\n", mGreenWhitelevel));
strBuf.append("\t\t},\n");
strBuf.append("\t\t\"blue\" :\n");
strBuf.append("\t\t{\n");
strBuf.append(String.format(Locale.ROOT, "\t\t\t\"threshold\" : %.4f,\n", mBlueThreshold));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\"gamma\" : %.4f,\n", mBlueGamma));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\"blacklevel\" : %.4f,\n", mBlueBlacklevel));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\"whitelevel\" : %.4f\n", mBlueWhitelevel));
strBuf.append("\t\t}");
return strBuf.toString();
}
/**
* Creates the JSON string of the smoothing subconfiguration as used in the Hyperion deamon configfile
*

View File

@ -0,0 +1,101 @@
package org.hyperion.hypercon.spec;
import java.util.Locale;
public class TransformConfig {
/** The identifier of this ColorTransform configuration */
public String mId = "";
/** The saturation gain (in HSV space) */
public double mSaturationGain = 1.0;
/** The value gain (in HSV space) */
public double mValueGain = 1.5;
/** The minimum required RED-value (in RGB space) */
public double mRedThreshold = 0.1;
/** The gamma-curve correct for the RED-value (in RGB space) */
public double mRedGamma = 2.0;
/** The black-level of the RED-value (in RGB space) */
public double mRedBlacklevel = 0.0;
/** The white-level of the RED-value (in RGB space) */
public double mRedWhitelevel = 0.8;
/** The minimum required GREEN-value (in RGB space) */
public double mGreenThreshold = 0.1;
/** The gamma-curve correct for the GREEN-value (in RGB space) */
public double mGreenGamma = 2.0;
/** The black-level of the GREEN-value (in RGB space) */
public double mGreenBlacklevel = 0.0;
/** The white-level of the GREEN-value (in RGB space) */
public double mGreenWhitelevel = 1.0;
/** The minimum required BLUE-value (in RGB space) */
public double mBlueThreshold = 0.1;
/** The gamma-curve correct for the BLUE-value (in RGB space) */
public double mBlueGamma = 2.0;
/** The black-level of the BLUE-value (in RGB space) */
public double mBlueBlacklevel = 0.0;
/** The white-level of the BLUE-value (in RGB space) */
public double mBlueWhitelevel = 1.0;
public String toJsonString() {
StringBuffer strBuf = new StringBuffer();
strBuf.append("\t\t{\n");
strBuf.append(hsvToJsonString() + ",\n");
strBuf.append(rgbToJsonString() + ",\n");
strBuf.append("\t\t}");
return strBuf.toString();
}
/**
* Creates the JSON string of the HSV-subconfiguration as used in the Hyperion deamon configfile
*
* @return The JSON string of the HSV-config
*/
private String hsvToJsonString() {
StringBuffer strBuf = new StringBuffer();
strBuf.append("\t\t\t\"hsv\" :\n");
strBuf.append("\t\t\t{\n");
strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"saturationGain\" : %.4f,\n", mSaturationGain));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"valueGain\" : %.4f\n", mValueGain));
strBuf.append("\t\t\t}");
return strBuf.toString();
}
/**
* Creates the JSON string of the RGB-subconfiguration as used in the Hyperion deamon configfile
*
* @return The JSON string of the RGB-config
*/
private String rgbToJsonString() {
StringBuffer strBuf = new StringBuffer();
strBuf.append("\t\t\t\"red\" :\n");
strBuf.append("\t\t\t{\n");
strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"threshold\" : %.4f,\n", mRedThreshold));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"gamma\" : %.4f,\n", mRedGamma));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"blacklevel\" : %.4f,\n", mRedBlacklevel));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"whitelevel\" : %.4f\n", mRedWhitelevel));
strBuf.append("\t\t},\n");
strBuf.append("\t\t\t\"green\" :\n");
strBuf.append("\t\t\t{\n");
strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"threshold\" : %.4f,\n", mGreenThreshold));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"gamma\" : %.4f,\n", mGreenGamma));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"blacklevel\" : %.4f,\n", mGreenBlacklevel));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"whitelevel\" : %.4f\n", mGreenWhitelevel));
strBuf.append("\t\t\t},\n");
strBuf.append("\t\t\t\"blue\" :\n");
strBuf.append("\t\t\t{\n");
strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"threshold\" : %.4f,\n", mBlueThreshold));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"gamma\" : %.4f,\n", mBlueGamma));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"blacklevel\" : %.4f,\n", mBlueBlacklevel));
strBuf.append(String.format(Locale.ROOT, "\t\t\t\t\"whitelevel\" : %.4f\n", mBlueWhitelevel));
strBuf.append("\t\t\t}");
return strBuf.toString();
}
}