[DEV] correct some parsing error and add Dynamic tool

This commit is contained in:
Edouard DUPIN 2021-04-06 01:11:14 +02:00
parent ab18170a8c
commit bd9c867207
7 changed files with 250 additions and 10 deletions

View File

@ -321,7 +321,7 @@ public record Color(
public static Color valueOf(final String colorBase) throws Exception { public static Color valueOf(final String colorBase) throws Exception {
// remove all white space... // remove all white space...
String color = colorBase.replace(" \r\n\t\\(\\)", ""); String color = colorBase.replaceAll("[ \r\n\t\\(\\)]", "");
if (color.isEmpty()) { if (color.isEmpty()) {
return new Color(0, 0, 0, 1.0f); return new Color(0, 0, 0, 1.0f);
} }
@ -403,10 +403,98 @@ public record Color(
} }
} }
public static Color valueOf256(final String colorBase) throws Exception {
// remove all white space...
String color = colorBase.replaceAll("[ \r\n\t\\(\\)]", "");
if (color.isEmpty()) {
return new Color(0, 0, 0, 1.0f);
}
final Color named = get(colorBase);
if (named != null) {
return named;
} else if (color.charAt(0) == '#') {
// MODEL: #RGB
// #RGBA
// #RRGGBB
// #RRGGBBAA
switch (color.length()) {
case 4 -> {
final float r = Integer.parseInt(color.substring(1, 2), 16) * 255.0f * 16.0f;
final float g = Integer.parseInt(color.substring(2, 3), 16) * 255.0f * 16.0f;
final float b = Integer.parseInt(color.substring(3, 4), 16) * 255.0f * 16.0f;
return new Color(r, g, b);
}
case 5 -> {
final float r = Integer.parseInt(color.substring(1, 2), 16) * 255.0f * 16.0f;
final float g = Integer.parseInt(color.substring(2, 3), 16) * 255.0f * 16.0f;
final float b = Integer.parseInt(color.substring(3, 4), 16) * 255.0f * 16.0f;
final float a = Integer.parseInt(color.substring(4, 5), 16) * 255.0f * 16.0f;
return new Color(r, g, b, a);
}
case 7 -> {
final float r = Integer.parseInt(color.substring(1, 3), 16) * 255.0f;
final float g = Integer.parseInt(color.substring(3, 5), 16) * 255.0f;
final float b = Integer.parseInt(color.substring(5, 7), 16) * 255.0f;
return new Color(r, g, b);
}
case 9 -> {
final float r = Integer.parseInt(color.substring(1, 3), 16) * 255.0f;
final float g = Integer.parseInt(color.substring(3, 5), 16) * 255.0f;
final float b = Integer.parseInt(color.substring(5, 7), 16) * 255.0f;
final float a = Integer.parseInt(color.substring(7, 9), 16) * 255.0f;
return new Color(r, g, b, a);
}
default -> throw new Exception("Can not parse color ... '" + colorBase + "'");
}
} else {
// Model: r.r,g.g,b.b
// r.r,g.g,b.b,a.a
// (r.r,g.g,b.b)
// (r.r,g.g,b.b,a.a)
// rgb(r.r,g.g,b.b)
// rgba(r.r,g.g,b.b,a.a)
// argb(a.a,r.r,g.g,b.b)
if (color.startsWith("argb")) {
color = color.replace("argb", "");
final String[] vals = color.split(",");
if (vals.length == 4) {
final float a = FMath.avg(0.0f, Float.parseFloat(vals[0]) / 256.0f, 1.0f);
final float r = FMath.avg(0.0f, Float.parseFloat(vals[1]) / 256.0f, 1.0f);
final float g = FMath.avg(0.0f, Float.parseFloat(vals[2]) / 256.0f, 1.0f);
final float b = FMath.avg(0.0f, Float.parseFloat(vals[3]) / 256.0f, 1.0f);
return new Color(r, g, b, a);
} else {
throw new Exception("Can not parse color ... '" + colorBase + "'");
}
}
color = color.replace("rgb", "");
color = color.replace("rgba", "");
final String[] vals = color.split(",");
if (vals.length == 3) {
final float r = FMath.avg(0.0f, Float.parseFloat(vals[0]) / 256.0f, 1.0f);
final float g = FMath.avg(0.0f, Float.parseFloat(vals[1]) / 256.0f, 1.0f);
final float b = FMath.avg(0.0f, Float.parseFloat(vals[2]) / 256.0f, 1.0f);
return new Color(r, g, b);
} else if (vals.length == 4) {
final float r = FMath.avg(0.0f, Float.parseFloat(vals[0]) / 256.0f, 1.0f);
final float g = FMath.avg(0.0f, Float.parseFloat(vals[1]) / 256.0f, 1.0f);
final float b = FMath.avg(0.0f, Float.parseFloat(vals[2]) / 256.0f, 1.0f);
final float a = FMath.avg(0.0f, Float.parseFloat(vals[3]) / 256.0f, 1.0f);
return new Color(r, g, b, a);
} else {
throw new Exception("Can not parse color ... '" + colorBase + "'");
}
}
}
public Color(final float r, final float g, final float b) { public Color(final float r, final float g, final float b) {
this(r, g, b, 1.0f); this(r, g, b, 1.0f);
} }
public Color(final int r, final int g, final int b) {
this(r, g, b, 0xFF);
}
public Color(final float r, final float g, final float b, final float a) { public Color(final float r, final float g, final float b, final float a) {
this.r = r; this.r = r;
this.g = g; this.g = g;
@ -414,6 +502,10 @@ public record Color(
this.a = a; this.a = a;
} }
public Color(final int r, final int g, final int b, final int a) {
this(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
}
public Color(final double r, final double g, final double b, final double a) { public Color(final double r, final double g, final double b, final double a) {
this((float) r, (float) g, (float) b, (float) a); this((float) r, (float) g, (float) b, (float) a);
} }

View File

@ -184,6 +184,9 @@ public record Dimension(
* @return dimension in Pixel * @return dimension in Pixel
*/ */
public Vector2f getPixel() { public Vector2f getPixel() {
if (this.type != Distance.POURCENT) {
return this.size;
}
return getPixel(windowsSize.getPixel()); return getPixel(windowsSize.getPixel());
} }
@ -191,7 +194,7 @@ public record Dimension(
if (this.type != Distance.POURCENT) { if (this.type != Distance.POURCENT) {
return this.size; return this.size;
} }
final Vector2f res = new Vector2f(uppersize.x() * this.size.x(), uppersize.y() * this.size.y()); final Vector2f res = new Vector2f(uppersize.x() * this.size.x() * 0.01f, uppersize.y() * this.size.y() * 0.01f);
//GALE_DEBUG("Get % : " + m_data + " / " + windDim + " == > " + res); //GALE_DEBUG("Get % : " + m_data + " / " + windDim + " == > " + res);
return res; return res;
} }
@ -205,7 +208,7 @@ public record Dimension(
if (this.type != Distance.POURCENT) { if (this.type != Distance.POURCENT) {
return new Vector2i((int) this.size.x(), (int) this.size.y()); return new Vector2i((int) this.size.x(), (int) this.size.y());
} }
final Vector2i res = new Vector2i((int) (uppersize.x() * this.size.x()), (int) (uppersize.y() * this.size.y())); final Vector2i res = new Vector2i((int) (uppersize.x() * this.size.x() * 0.01f), (int) (uppersize.y() * this.size.y() * 0.01f));
//GALE_DEBUG("Get % : " + m_data + " / " + windDim + " == > " + res); //GALE_DEBUG("Get % : " + m_data + " / " + windDim + " == > " + res);
return res; return res;
} }
@ -238,7 +241,6 @@ public record Dimension(
* @param config dimension configuration. * @param config dimension configuration.
*/ */
public static Dimension valueOf(String config) { public static Dimension valueOf(String config) {
final Vector2f size = Vector2f.ZERO;
Distance type = Distance.PIXEL; Distance type = Distance.PIXEL;
if (config.endsWith("%")) { if (config.endsWith("%")) {
type = Distance.POURCENT; type = Distance.POURCENT;
@ -310,4 +312,93 @@ public record Dimension(
return str; return str;
} }
public static Dimension valueOf(String contentX, String contentY) {
Distance typeX = Distance.UNKNOW;
if (contentX.endsWith("%")) {
typeX = Distance.POURCENT;
contentX = contentX.substring(0, contentX.length() - 1);
} else if (contentX.endsWith("px")) {
typeX = Distance.PIXEL;
contentX = contentX.substring(0, contentX.length() - 2);
} else if (contentX.endsWith("ft")) {
typeX = Distance.FOOT;
contentX = contentX.substring(0, contentX.length() - 2);
} else if (contentX.endsWith("in")) {
typeX = Distance.INCH;
contentX = contentX.substring(0, contentX.length() - 2);
} else if (contentX.endsWith("km")) {
typeX = Distance.KILOMETER;
contentX = contentX.substring(0, contentX.length() - 2);
} else if (contentX.endsWith("mm")) {
typeX = Distance.MILLIMETER;
contentX = contentX.substring(0, contentX.length() - 2);
} else if (contentX.endsWith("cm")) {
typeX = Distance.CENTIMETER;
contentX = contentX.substring(0, contentX.length() - 2);
} else if (contentX.endsWith("m")) {
typeX = Distance.METER;
contentX = contentX.substring(0, contentX.length() - 1);
} else if (contentX.endsWith("em")) {
typeX = Distance.ELEMENT;
contentX = contentX.substring(0, contentX.length() - 2);
} else if (contentX.endsWith("ex")) {
typeX = Distance.EX;
contentX = contentX.substring(0, contentX.length() - 2);
} else if (contentX.endsWith("pt")) {
typeX = Distance.POINT;
contentX = contentX.substring(0, contentX.length() - 2);
} else if (contentX.endsWith("pc")) {
typeX = Distance.PC;
contentX = contentX.substring(0, contentX.length() - 2);
}
float tmpX = Float.valueOf(contentX);
Distance typeY = Distance.UNKNOW;
if (contentY.endsWith("%")) {
typeY = Distance.POURCENT;
contentY = contentY.substring(0, contentY.length() - 1);
} else if (contentY.endsWith("px")) {
typeY = Distance.PIXEL;
contentY = contentY.substring(0, contentY.length() - 2);
} else if (contentY.endsWith("ft")) {
typeY = Distance.FOOT;
contentY = contentY.substring(0, contentY.length() - 2);
} else if (contentY.endsWith("in")) {
typeY = Distance.INCH;
contentY = contentY.substring(0, contentY.length() - 2);
} else if (contentY.endsWith("km")) {
typeY = Distance.KILOMETER;
contentY = contentY.substring(0, contentY.length() - 2);
} else if (contentY.endsWith("mm")) {
typeY = Distance.MILLIMETER;
contentY = contentY.substring(0, contentY.length() - 2);
} else if (contentY.endsWith("cm")) {
typeY = Distance.CENTIMETER;
contentY = contentY.substring(0, contentY.length() - 2);
} else if (contentY.endsWith("m")) {
typeY = Distance.METER;
contentY = contentY.substring(0, contentY.length() - 1);
} else if (contentY.endsWith("em")) {
typeY = Distance.ELEMENT;
contentY = contentY.substring(0, contentY.length() - 2);
} else if (contentY.endsWith("ex")) {
typeY = Distance.EX;
contentY = contentY.substring(0, contentY.length() - 2);
} else if (contentY.endsWith("pt")) {
typeY = Distance.POINT;
contentY = contentY.substring(0, contentY.length() - 2);
} else if (contentY.endsWith("pc")) {
typeY = Distance.PC;
contentY = contentY.substring(0, contentY.length() - 2);
}
float tmpY = Float.valueOf(contentY);
if (typeX == Distance.UNKNOW && typeY == Distance.UNKNOW) {
return new Dimension(new Vector2f(tmpX, tmpY), Distance.PIXEL);
} else if (typeX == Distance.UNKNOW) {
return new Dimension(new Vector2f(tmpX, tmpY), typeY);
}
return new Dimension(new Vector2f(tmpX, tmpY), typeX);
}
} }

View File

@ -6,7 +6,6 @@
package org.atriasoft.etk; package org.atriasoft.etk;
import org.atriasoft.etk.internal.Log; import org.atriasoft.etk.internal.Log;
import org.atriasoft.etk.math.Vector2f;
/** /**
* in the dimension class we store the data as the more usefull unit (pixel) * in the dimension class we store the data as the more usefull unit (pixel)
@ -89,7 +88,6 @@ public record Dimension1D(
* @param config dimension configuration. * @param config dimension configuration.
*/ */
public static Dimension1D valueOf(String config) { public static Dimension1D valueOf(String config) {
final Vector2f size = Vector2f.ZERO;
Distance type = Distance.PIXEL; Distance type = Distance.PIXEL;
if (config.endsWith("%")) { if (config.endsWith("%")) {
type = Distance.POURCENT; type = Distance.POURCENT;
@ -127,9 +125,6 @@ public record Dimension1D(
} else if (config.endsWith("pc")) { } else if (config.endsWith("pc")) {
type = Distance.PC; type = Distance.PC;
config = config.substring(0, config.length() - 2); config = config.substring(0, config.length() - 2);
} else {
Log.critical("Can not parse dimension : '" + config + "'");
return null;
} }
final float tmp = Float.valueOf(config); final float tmp = Float.valueOf(config);
final Dimension1D ret = new Dimension1D(tmp, type); final Dimension1D ret = new Dimension1D(tmp, type);

View File

@ -1,6 +1,7 @@
package org.atriasoft.etk; package org.atriasoft.etk;
public enum Distance { public enum Distance {
UNKNOW, //!< "%"
POURCENT, //!< "%" POURCENT, //!< "%"
PIXEL, //!< "px" PIXEL, //!< "px"
METER, //!< "m" METER, //!< "m"

View File

@ -188,6 +188,26 @@ public class Uri {
} }
public static void writeAll(final Uri uri, final String data) { public static void writeAll(final Uri uri, final String data) {
BufferedWriter out = null;
try {
FileWriter fstream = new FileWriter(uri.getPath(), false); //true tells to append data.
out = new BufferedWriter(fstream);
out.write(data);
} catch (IOException e) {
Log.error("Error: " + e.getMessage());
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.error("Error: ", e);
}
}
}
}
public static void writeAllAppend(final Uri uri, final String data) {
BufferedWriter out = null; BufferedWriter out = null;
try { try {
FileWriter fstream = new FileWriter(uri.getPath(), true); //true tells to append data. FileWriter fstream = new FileWriter(uri.getPath(), true); //true tells to append data.
@ -240,7 +260,7 @@ public class Uri {
} }
public String getExtention() { public String getExtention() {
final String[] ret = this.path.split("."); final String[] ret = this.path.split("\\.");
return ret[ret.length - 1]; return ret[ret.length - 1];
} }

View File

@ -0,0 +1,36 @@
package org.atriasoft.etk.util;
public class ArraysTools {
public static <T> void fill(final T[] buffer, final T value) {
if (buffer == null) {
return;
}
for (int iii = 0; iii < buffer.length; iii++) {
buffer[iii] = value;
}
}
public static <T> void fill2(final float[][] buffer, final float value) {
if (buffer == null) {
return;
}
for (int iii = 0; iii < buffer.length; iii++) {
for (int jjj = 0; jjj < buffer[iii].length; jjj++) {
buffer[iii][jjj] = value;
}
}
}
public static <T> void fill2(final T[][] buffer, final T value) {
if (buffer == null) {
return;
}
for (int iii = 0; iii < buffer.length; iii++) {
for (int jjj = 0; jjj < buffer[iii].length; jjj++) {
buffer[iii][jjj] = value;
}
}
}
private ArraysTools() {}
}

View File

@ -6,4 +6,9 @@ public class Dynamic<T> {
public Dynamic(final T value) { public Dynamic(final T value) {
this.value = value; this.value = value;
} }
@Override
public String toString() {
return "Dynamic<" + super.toString() + ">";
}
} }