1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
package com.metaweb.lessen.tokens;
import com.metaweb.lessen.expr.OperatorCall;
public class ComponentColor extends Color {
protected NumericToken _r, _g, _b, _a;
public ComponentColor(
int start, int end, String text,
NumericToken r,
NumericToken g,
NumericToken b,
NumericToken a
) {
super(start, end, text);
_r = r; _g = g; _b = b; _a = a;
}
@Override
public int getA() {
return tokenToInt(_a);
}
@Override
public int getB() {
return tokenToInt(_b);
}
@Override
public int getG() {
return tokenToInt(_g);
}
@Override
public int getR() {
return tokenToInt(_r);
}
protected int tokenToInt(NumericToken t) {
if (t == null) {
return -1;
} else if (t.type == Type.Percentage) {
return t.n.intValue() * 255 / 100;
} else if (t.n.doubleValue() > 1) {
return t.n.intValue();
} else {
return (int) (255 * t.n.doubleValue());
}
}
public Color performOperation(String op, Color color) {
if (color instanceof ComponentColor && (op.endsWith("+") || op.equals("-"))) {
ComponentColor color2 = (ComponentColor) color;
NumericToken r = limit(performNumericOperation(op, _r, color2._r));
NumericToken g = limit(performNumericOperation(op, _g, color2._g));
NumericToken b = limit(performNumericOperation(op, _b, color2._b));
NumericToken a = limit(performNumericOperation(op, _a, color2._a));
return createColor(r, g, b, a);
}
return super.performOperation(op, color);
}
@Override
public Color performOperation(String op, Number n) {
if (op.equals("*") || op.equals("/")) {
double d = n.doubleValue();
NumericToken r = limit(performNumericOperation(op, _r, d));
NumericToken g = limit(performNumericOperation(op, _g, d));
NumericToken b = limit(performNumericOperation(op, _b, d));
NumericToken a = limit(performNumericOperation(op, _a, d));
return createColor(r, g, b, a);
}
return super.performOperation(op, n);
}
protected NumericToken performNumericOperation(String op, NumericToken tLeft, NumericToken tRight) {
if (tLeft == null) {
return tRight;
} else if (tRight == null) {
return tLeft;
} else {
return OperatorCall.performNumericOperation(op, tLeft, tRight);
}
}
protected NumericToken performNumericOperation(String op, NumericToken leftT, double d) {
if (leftT != null) {
return OperatorCall.performNumericOperation(op, leftT, d);
}
return null;
}
protected ComponentColor createColor(NumericToken r, NumericToken g, NumericToken b, NumericToken a) {
StringBuffer sb = new StringBuffer();
sb.append(a != null ? "rgba(" : "rgb(");
sb.append(r != null ? r.getCleanText() : "ERROR"); sb.append(", ");
sb.append(g != null ? g.getCleanText() : "ERROR"); sb.append(", ");
sb.append(b != null ? b.getCleanText() : "ERROR");
if (a != null) {
sb.append(", ");
sb.append(a.getCleanText());
}
sb.append(")");
return new ComponentColor(
start,
end,
sb.toString(),
r,
g,
b,
a
);
}
protected NumericToken limit(NumericToken t) {
if (t != null) {
double d = t.n.doubleValue();
if (t.type == Type.Percentage) {
if (d < 0 || d > 100) {
return OperatorCall.makePercentageToken(t, Math.max(0, Math.min(100, d)));
}
} else {
if (d < 0 || d > 255) {
return OperatorCall.makeNumberToken(t, Math.max(0, Math.min(255, d)));
}
}
}
return t;
}
}
|