diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GPar.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GPar.java index 48e5ca2d58fcd3da897908e0acc74003041ee96d..adba03004cfa945ccba56231ed7b4f4140995caa 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GPar.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GPar.java @@ -18,7 +18,6 @@ import static com.oracle.truffle.r.library.fastrGrid.GridUtils.getDataAtMod; import java.util.Arrays; import java.util.function.Function; -import com.oracle.truffle.r.library.fastrGrid.GridState.GridPalette; import com.oracle.truffle.r.library.fastrGrid.device.DrawingContext; import com.oracle.truffle.r.library.fastrGrid.device.DrawingContextDefaults; import com.oracle.truffle.r.library.fastrGrid.device.GridColor; @@ -26,7 +25,6 @@ import com.oracle.truffle.r.library.fastrGrid.device.GridDevice; import com.oracle.truffle.r.runtime.RError; import com.oracle.truffle.r.runtime.RError.Message; import com.oracle.truffle.r.runtime.RRuntime; -import com.oracle.truffle.r.runtime.Utils; import com.oracle.truffle.r.runtime.data.RDataFactory; import com.oracle.truffle.r.runtime.data.RList; import com.oracle.truffle.r.runtime.data.RNull; @@ -267,21 +265,7 @@ public final class GPar { private GridColor getGridColor(int listIndex) { Object value = data[listIndex]; - GridColor color = getPaletteColor(value); - if (color != null) { - return color; - } - - String strValue = null; - if (value instanceof String) { - strValue = (String) value; - } else if (value instanceof RAbstractStringVector && ((RAbstractStringVector) value).getLength() > 0) { - strValue = ((RAbstractStringVector) value).getDataAt(index % ((RAbstractStringVector) value).getLength()); - } else { - return GridColor.TRANSPARENT; - } - - color = GridColorUtils.gridColorFromString(strValue); + GridColor color = GridColorUtils.getColor(value, index); double alpha = asDouble(data[GP_ALPHA], index); if (alpha != 1.) { int newAlpha = Math.min(255, (int) (alpha * ((color.getAlpha() / 255.0) * 255))); @@ -291,44 +275,6 @@ public final class GPar { } } - private GridColor getPaletteColor(Object colorIdIn) { - Object colorId = colorIdIn; - if (colorId instanceof RAbstractVector) { - RAbstractVector vec = (RAbstractVector) colorId; - colorId = vec.getDataAtAsObject(index % vec.getLength()); - } - int paletteIdx = RRuntime.INT_NA; - if (colorId instanceof Integer) { - paletteIdx = (int) colorId; - } else if (colorId instanceof Double && !RRuntime.isNA((Double) colorId)) { - paletteIdx = (int) (double) colorId; - } else if (colorId instanceof String && !RRuntime.isNA((String) colorId)) { - paletteIdx = paletteIdxFromString((String) colorId); - } else if (colorId instanceof Byte && !RRuntime.isNA((byte) colorId)) { - paletteIdx = (int) (byte) colorId; - } - if (RRuntime.isNA(paletteIdx)) { - return null; - } - if (paletteIdx < 0) { - throw RError.error(RError.NO_CALLER, Message.GENERIC, Utils.stringFormat("numerical color values must be >= 0, found %d", paletteIdx)); - } - if (paletteIdx == 0) { - return GridColor.TRANSPARENT; - } - GridPalette palette = GridContext.getContext().getGridState().getPalette(); - GridColor result = palette.colors[(paletteIdx - 1) % palette.colors.length]; - return result; // one based index - } - - private int paletteIdxFromString(String colorId) { - try { - return Integer.parseInt(colorId, 10); - } catch (NumberFormatException ex) { - return RRuntime.INT_NA; - } - } - private static final byte[] DASHED_LINE = new byte[]{4, 4}; private static final byte[] DOTTED_LINE = new byte[]{1, 3}; private static final byte[] DOTDASH_LINE = new byte[]{1, 3, 4, 3}; diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GridColorUtils.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GridColorUtils.java index 1ec25bf728686609acfa0232c2515f4f09818363..c751e57f43cddaab2e1f1f8cf82918e246fffff6 100644 --- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GridColorUtils.java +++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GridColorUtils.java @@ -18,6 +18,10 @@ import com.oracle.truffle.r.library.fastrGrid.GridState.GridPalette; import com.oracle.truffle.r.library.fastrGrid.device.GridColor; import com.oracle.truffle.r.runtime.RError; import com.oracle.truffle.r.runtime.RError.Message; +import com.oracle.truffle.r.runtime.RRuntime; +import com.oracle.truffle.r.runtime.Utils; +import com.oracle.truffle.r.runtime.data.model.RAbstractStringVector; +import com.oracle.truffle.r.runtime.data.model.RAbstractVector; public final class GridColorUtils { @@ -27,6 +31,28 @@ public final class GridColorUtils { // only static members } + /** + * Converts given object into {@link GridColor}. The object may be a vector, in which case the + * index modulo its size is used to select element of that vector. + */ + public static GridColor getColor(Object value, int index) { + GridColor color = GridColorUtils.getPaletteColor(value, index); + if (color != null) { + return color; + } + + String strValue = null; + if (value instanceof String) { + strValue = (String) value; + } else if (value instanceof RAbstractStringVector && ((RAbstractStringVector) value).getLength() > 0) { + strValue = ((RAbstractStringVector) value).getDataAt(index % ((RAbstractStringVector) value).getLength()); + } else { + return GridColor.TRANSPARENT; + } + + return gridColorFromString(strValue); + } + public static GridPalette getDefaultPalette() { if (defaultPalette == null) { // Note: default palette copied from GNU R @@ -105,6 +131,44 @@ public final class GridColorUtils { return isNormalized ? synonym : synonym.replace(" ", "").toLowerCase(Locale.ROOT); } + private static GridColor getPaletteColor(Object colorIdIn, int index) { + Object colorId = colorIdIn; + if (colorId instanceof RAbstractVector) { + RAbstractVector vec = (RAbstractVector) colorId; + colorId = vec.getDataAtAsObject(index % vec.getLength()); + } + int paletteIdx = RRuntime.INT_NA; + if (colorId instanceof Integer) { + paletteIdx = (int) colorId; + } else if (colorId instanceof Double && !RRuntime.isNA((Double) colorId)) { + paletteIdx = (int) (double) colorId; + } else if (colorId instanceof String && !RRuntime.isNA((String) colorId)) { + paletteIdx = paletteIdxFromString((String) colorId); + } else if (colorId instanceof Byte && !RRuntime.isNA((byte) colorId)) { + paletteIdx = (int) (byte) colorId; + } + if (RRuntime.isNA(paletteIdx)) { + return null; + } + if (paletteIdx < 0) { + throw RError.error(RError.NO_CALLER, Message.GENERIC, Utils.stringFormat("numerical color values must be >= 0, found %d", paletteIdx)); + } + if (paletteIdx == 0) { + return GridColor.TRANSPARENT; + } + GridPalette palette = GridContext.getContext().getGridState().getPalette(); + GridColor result = palette.colors[(paletteIdx - 1) % palette.colors.length]; + return result; // one based index + } + + private static int paletteIdxFromString(String colorId) { + try { + return Integer.parseInt(colorId, 10); + } catch (NumberFormatException ex) { + return RRuntime.INT_NA; + } + } + private static final class NamesHolder { private static final HashMap<String, Object> NAMES = new HashMap<>(700);