From: Hannu Niemistö Date: Mon, 19 Sep 2016 09:08:31 +0000 (+0300) Subject: Merged changes from SVN X-Git-Tag: v1.25.0~108^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=7a7c389f2ffddb4e4170098e4f5facca1fa8b633;p=simantics%2Fplatform.git Merged changes from SVN --- diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCL.grammar b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCL.grammar index 6821da83e..500cf3b20 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCL.grammar +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCL.grammar @@ -187,7 +187,7 @@ stringLiteral ; statements - = LBRACE statement (SEMICOLON statement)* RBRACE # Statements + = LBRACE (statement (SEMICOLON statement)*)? RBRACE # Statements ; statement diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLLexer.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLLexer.java index e23a9a17c..e31112790 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLLexer.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLLexer.java @@ -1,12 +1,12 @@ /* The following code was generated by JFlex 1.6.1 */ -package org.simantics.scl.compiler.internal.parsing.parser; - -import org.simantics.scl.compiler.internal.parsing.Token; -import org.simantics.scl.compiler.errors.Locations; -import org.simantics.scl.compiler.internal.parsing.exceptions.SCLSyntaxErrorException; -import gnu.trove.list.array.TIntArrayList; - +package org.simantics.scl.compiler.internal.parsing.parser; + +import org.simantics.scl.compiler.internal.parsing.Token; +import org.simantics.scl.compiler.errors.Locations; +import org.simantics.scl.compiler.internal.parsing.exceptions.SCLSyntaxErrorException; +import gnu.trove.list.array.TIntArrayList; + /** * This class is a scanner generated by @@ -562,20 +562,20 @@ public class SCLLexer { private int zzFinalHighSurrogate = 0; /* user code: */ - public SCLParserOptions options = SCLParserOptions.DEFAULT; - int stringStart; - TIntArrayList parenCountStack = new TIntArrayList(2); - int parenCount = 0; - TIntArrayList stateStack = new TIntArrayList(2); - - StringBuffer string = new StringBuffer(); - - private Token sym(int id) { - return new Token(id, yychar, yychar+yylength(), yytext()); - } - private Token sym(int id, String text) { - return new Token(id, yychar, yychar+yylength(), text); - } + public SCLParserOptions options = SCLParserOptions.DEFAULT; + int stringStart; + TIntArrayList parenCountStack = new TIntArrayList(2); + int parenCount = 0; + TIntArrayList stateStack = new TIntArrayList(2); + + StringBuffer string = new StringBuffer(); + + private Token sym(int id) { + return new Token(id, yychar, yychar+yylength(), yytext()); + } + private Token sym(int id, String text) { + return new Token(id, yychar, yychar+yylength(), text); + } /** @@ -900,7 +900,7 @@ public class SCLLexer { } case 278: break; default: - { return sym(SCLTerminals.EOF); + { return sym(SCLTerminals.EOF); } } } @@ -943,15 +943,15 @@ public class SCLLexer { } case 101: break; case 10: - { --parenCount; - if(parenCount == 0 && !parenCountStack.isEmpty()) { - parenCount = parenCountStack.removeAt(parenCountStack.size()-1); - string.setLength(0); - stringStart=yychar; - yybegin(stateStack.removeAt(stateStack.size()-1)); - return sym(SCLTerminals.CONTINUE_STRING); - } - else + { --parenCount; + if(parenCount == 0 && !parenCountStack.isEmpty()) { + parenCount = parenCountStack.removeAt(parenCountStack.size()-1); + string.setLength(0); + stringStart=yychar; + yybegin(stateStack.removeAt(stateStack.size()-1)); + return sym(SCLTerminals.CONTINUE_STRING); + } + else return sym(SCLTerminals.RPAREN); } case 102: break; @@ -1036,7 +1036,7 @@ public class SCLLexer { } case 122: break; case 31: - { yybegin(YYINITIAL); + { yybegin(YYINITIAL); return new Token(SCLTerminals.END_STRING, stringStart, yychar+1, string.toString()); } case 123: break; @@ -1113,10 +1113,10 @@ public class SCLLexer { } case 141: break; case 50: - { parenCountStack.add(parenCount); - parenCount = 1; - stateStack.add(STRING); - yybegin(YYINITIAL); + { parenCountStack.add(parenCount); + parenCount = 1; + stateStack.add(STRING); + yybegin(YYINITIAL); return new Token(SCLTerminals.SUSPEND_STRING, stringStart, yychar+1, string.toString()); } case 142: break; @@ -1129,10 +1129,10 @@ public class SCLLexer { } case 144: break; case 53: - { parenCountStack.add(parenCount); - parenCount = 1; - stateStack.add(LONG_STRING); - yybegin(YYINITIAL); + { parenCountStack.add(parenCount); + parenCount = 1; + stateStack.add(LONG_STRING); + yybegin(YYINITIAL); return new Token(SCLTerminals.SUSPEND_STRING, stringStart, yychar+1, string.toString()); } case 145: break; @@ -1145,12 +1145,12 @@ public class SCLLexer { } case 147: break; case 56: - { String text = yytext(); + { String text = yytext(); return sym(SCLTerminals.ID, text.substring(1, text.length()-1)); } case 148: break; case 57: - { String text = yytext(); + { String text = yytext(); return sym(SCLTerminals.SYMBOL, text.substring(1, text.length()-1)); } case 149: break; @@ -1171,7 +1171,7 @@ public class SCLLexer { } case 153: break; case 62: - { yybegin(YYINITIAL); + { yybegin(YYINITIAL); return new Token(SCLTerminals.END_STRING, stringStart, yychar+3, string.toString()); } case 154: break; diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParser.dat b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParser.dat index f17807cb5..fe5dac3c4 100644 Binary files a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParser.dat and b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParser.dat differ diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParser.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParser.java index bde7f10b4..ac61c9010 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParser.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParser.java @@ -13,7 +13,7 @@ public abstract class SCLParser { public static final boolean TRACE = false; private static final int INITIAL_CAPACITY = 16; - private static final int STATE_COUNT = 323; + private static final int STATE_COUNT = 324; private static final int TERMINAL_COUNT = 79; private static final int NONTERMINAL_COUNT = 49; private static final int PRODUCT_COUNT = 126; @@ -21,7 +21,7 @@ public abstract class SCLParser { private static final int[] ACTION_ROW_ID = new int[STATE_COUNT]; private static final int[] ACTION_COLUMN_ID = new int[TERMINAL_COUNT]; private static final short[] ACTION_TABLE = new short[5508]; - private static final int[] ERROR_TABLE = new int[798]; + private static final int[] ERROR_TABLE = new int[800]; private static final int[] GOTO_ROW_ID = new int[STATE_COUNT]; private static final int[] GOTO_COLUMN_ID = new int[NONTERMINAL_COUNT]; private static final short[] GOTO_TABLE = new short[1620]; @@ -362,19 +362,19 @@ public abstract class SCLParser { return parse(0); } public Object parseCommands() { - return parse(308); + return parse(309); } public Object parseImport() { - return parse(315); + return parse(316); } public Object parseType() { - return parse(317); + return parse(318); } public Object parseExp() { - return parse(319); + return parse(320); } public Object parseEquationBlock() { - return parse(321); + return parse(322); } @@ -945,7 +945,7 @@ public abstract class SCLParser { */ protected abstract Object reduceFieldDescription(); /** - * statements ::= LBRACE statement (SEMICOLON statement)* RBRACE + * statements ::= LBRACE (statement (SEMICOLON (statement SEMICOLON)* statement)?)? RBRACE */ protected abstract Object reduceStatements(); /** diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParserImpl.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParserImpl.java index 680be5e35..bbf944f1e 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParserImpl.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/parser/SCLParserImpl.java @@ -480,8 +480,9 @@ public class SCLParserImpl extends SCLParser { @Override protected Object reduceStatements() { EBlock block = new EBlock(); - for(int i=1;i 2) + for(int i=1;i alphaValues; + + + public ColorAlphaGradient() { + super(); + this.alphaValues = new ArrayList<>(); + } + + public ColorAlphaGradient(ColorAlphaGradient copyFrom) { + super(copyFrom); + this.alphaValues = copyFrom.alphaValues; + } + + public ColorAlphaGradient(ColorValue array[], int alphaArray[]) { + super(array); + if (array.length != alphaArray.length) + throw new IllegalArgumentException("Array lenghts do not match."); + this.alphaValues = new ArrayList<>(alphaArray.length); + for (int a : alphaArray) { + alphaValues.add(a); + } + } + + public ColorAlphaGradient(ColorValue array[], int alphaArray[], int type) { + super(array, type); + if (array.length != alphaArray.length) + throw new IllegalArgumentException("Array lenghts do not match."); + this.alphaValues = new ArrayList<>(alphaArray.length); + for (int a : alphaArray) { + alphaValues.add(a); + } + } + + public ColorAlphaGradient(Collection values, Collection alphaValues) { + super(values); + if (values.size() != alphaValues.size()) + throw new IllegalArgumentException("Array lenghts do not match."); + this.alphaValues = new ArrayList<>(alphaValues); + } + + public ColorAlphaGradient(Collection values, Collection alphaValues, int type) { + super(values, type); + if (values.size() != alphaValues.size()) + throw new IllegalArgumentException("Array lenghts do not match."); + this.alphaValues = new ArrayList<>(alphaValues); + } + + /** + * Interpolates color in RGB space + * + * @param value + * @return + */ + private byte[] getRGBColor(double value) { + int index = 1; + while (values.get(index).getValue() <= value && index < values.size()-1) + index++; + + value -= values.get(index - 1).getValue(); + value /= (values.get(index).getValue() - values.get(index - 1).getValue()); + double valuei = 1.0 - value; + byte color[] = new byte[] { + (byte) Math.min(255.0, Math.floor(value * values.get(index).getColor().getR() + valuei * values.get(index - 1).getColor().getR())), + (byte) Math.min(255.0, Math.floor(value * values.get(index).getColor().getG() + valuei * values.get(index - 1).getColor().getG())), + (byte) Math.min(255.0, Math.floor(value * values.get(index).getColor().getB() + valuei * values.get(index - 1).getColor().getB())), + (byte) Math.min(255.0, Math.floor(value * alphaValues.get(index) + valuei * alphaValues.get(index - 1)))}; + return color; + + } + + /** + * Interpolates color in HSV space + * + * @param value + * @return + */ + private byte[] getHSVColor(double value) { + int index = 1; + while (values.get(index).getValue() <= value && index < values.size()-1) + index++; + + value -= values.get(index - 1).getValue(); + value /= (values.get(index).getValue() - values.get(index - 1).getValue()); + double valuei = 1.0 - value; + double h; + if (Float.isNaN(values.get(index).getColor().getH())) { + h = values.get(index-1).getColor().getH(); + } else if (Float.isNaN(values.get(index-1).getColor().getH())) { + h = values.get(index).getColor().getH(); + } else { + // selecting shortest direction between hues + float angle = values.get(index).getColor().getH() - values.get(index - 1).getColor().getH(); + if (angle > 180.f) + angle -= 360.f; + else if (angle < -180.f) + angle += 360.f; + h = values.get(index - 1).getColor().getH() + value * angle; + if (h > 360.f) + h -= 360.f; + else if (h < 0.f) + h+= 360.f; + } + org.simantics.utils.ui.color.Color interpolated = new org.simantics.utils.ui.color.Color(h, value * values.get(index).getColor().getS() + valuei * values.get(index - 1).getColor().getS(), + value * values.get(index).getColor().getV() + valuei * values.get(index - 1).getColor().getV()); + byte color[] = new byte[] { (byte) interpolated.getR(), (byte) interpolated.getG(), (byte) interpolated.getB(), (byte) Math.min(255.0, Math.floor(value * alphaValues.get(index) + valuei * alphaValues.get(index - 1)))}; + + return color; + + } + + /** + *

+ * Returns gradient in array of bytes. Array is RGB order and int contains 3 * requested size of bytes. + *

+ *

+ * If gradient contains only one color array is filled with that color + *

+ *

+ * if gradient has no colors array is filled with white + *

+ * @param size number of pixels + * @return gradient in array of bytes + */ + public byte[] getGradientArray(int size) { + byte array[] = new byte[size * 4]; + if (values.size() > 1) { + for (int i = 0; i < size; i++) { + int index = i * 4; + double value = values.get(0).getValue() + (values.get(values.size() - 1).getValue() - values.get(0).getValue()) * (double) i / (double) size; + byte color[]; + if (type == RGB) + color = getRGBColor(value); + else + color = getHSVColor(value); + array[index] = color[0]; + array[index + 1] = color[1]; + array[index + 2] = color[2]; + array[index + 3] = color[3]; + } + } else if (values.size() == 1) { + byte color[] = new byte[3]; + color[0] = (byte)values.get(0).getColor().getR(); + color[1] = (byte)values.get(0).getColor().getG(); + color[2] = (byte)values.get(0).getColor().getB(); + color[3] = (byte)(int)alphaValues.get(0); + for (int i = 0; i < size; i++) { + int index = i * 3; + array[index] = color[0]; + array[index + 1] = color[1]; + array[index + 2] = color[2]; + array[index + 3] = color[3]; + } + } else { + for (int i = 0; i < size; i++) { + int index = i * 4; + array[index] = (byte)255; + array[index + 1] = (byte)255; + array[index + 2] = (byte)255; + array[index + 3] = (byte)255; + } + } + return array; + } + + /** + *

+ * Returns gradient in image. + *

+ *

+ * If gradient contains only one color image is filled with that color + *

+ *

+ * if gradient has no colors image is filled with white + *

+ *

+ * Style must be set to SWT.HORIZONTAL or SWT.VERTICAL + *

+ * @param size number of pixels + * @return gradient in array of bytes + */ + + public Image getGradientImage(int width, int height, int style) { + Image image = new Image(Display.getCurrent(), width, height); + GC gc = new GC(image); + gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE)); + gc.fillRectangle(0, 0, width, height); + if (values.size() > 1) { + if (SWT.HORIZONTAL == (style | SWT.HORIZONTAL)) { + for (int x = 0; x < width; x++) { + double value = values.get(0).getValue() + (values.get(values.size() - 1).getValue() - values.get(0).getValue()) * (double) x / (double) (width - 1); + byte byteColor[]; + if (type == RGB) + byteColor = getRGBColor(value); + else + byteColor = getHSVColor(value); + Color color = new Color(Display.getCurrent(), byteColor[0] & 0xff, byteColor[1] & 0xff, byteColor[2] & 0xff, byteColor[3] & 0xff); + gc.setForeground(color); + gc.drawLine(x, 0, x, height); + color.dispose(); + } + } else if (SWT.VERTICAL == (style | SWT.VERTICAL)){ + for (int y = 0; y < height; y++) { + double value = values.get(0).getValue() + (values.get(values.size() - 1).getValue() - values.get(0).getValue()) * (double) y + / (double) (height - 1); + byte byteColor[]; + if (type == RGB) + byteColor = getRGBColor(value); + else + byteColor = getHSVColor(value); + Color color = new Color(Display.getCurrent(), byteColor[0] & 0xff, byteColor[1] & 0xff, byteColor[2] & 0xff, byteColor[3] & 0xff); + gc.setForeground(color); + gc.drawLine(0, y, width, y); + color.dispose(); + } + } else { + gc.dispose(); + image.dispose(); + SWT.error(SWT.ERROR_INVALID_ARGUMENT); + } + } else if (values.size() == 1) { + Color color = new Color(Display.getCurrent(), values.get(0).getColor().getR(), values.get(0).getColor().getG(), values.get(0).getColor().getB(), alphaValues.get(0)); + gc.setBackground(color); + gc.fillRectangle(0, 0, width, height); + color.dispose(); + } else { + gc.fillRectangle(0, 0, width, height); + } + gc.dispose(); + return image; + } + + @Override + public boolean equals(Object obj) { + if (super.equals(obj)) { + ColorAlphaGradient cg = (ColorAlphaGradient)obj; + return alphaValues.containsAll(cg.alphaValues); + } else { + return false; + } + } + +} diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/color/ColorGradient.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/color/ColorGradient.java index a08010f20..56195ce0c 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/color/ColorGradient.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/color/ColorGradient.java @@ -38,9 +38,9 @@ public class ColorGradient { public static final int HSV = 1; - private ArrayList values; + protected ArrayList values; - private int type; + protected int type; public ColorGradient() { this.values = new ArrayList(); @@ -59,7 +59,7 @@ public class ColorGradient { } public ColorGradient(ColorValue array[]) { - this.values = new ArrayList(); + this.values = new ArrayList(array.length); for (ColorValue c : array) { values.add(c); } @@ -295,7 +295,10 @@ public class ColorGradient { @Override public boolean equals(Object obj) { - if (!(obj instanceof ColorGradient)) return false; + if (obj == null) + return false; + if (obj.getClass() != getClass()) + return false; ColorGradient cg = (ColorGradient) obj; if (cg.type != type) return false; if (values.size()!=cg.values.size()) return false;