use of au.gov.asd.tac.constellation.utilities.color.ConstellationColor in project constellation by constellation-app.
the class ImageGraphBuilderPlugin method edit.
@Override
protected void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
interaction.setProgress(0, 0, "Building...", true);
// imageFiles will be a list of files which extends from object type
@SuppressWarnings("unchecked") final List<File> imageFiles = (List<File>) parameters.getObjectValue(IMAGE_FILE_PARAMETER_ID);
for (final File imageFile : imageFiles) {
final ArrayList<BufferedImage> images = new ArrayList<>();
if (StringUtils.endsWithIgnoreCase(imageFile.getName(), FileExtensionConstants.GIF)) {
final ThreeTuple<List<BufferedImage>, List<Integer>, List<Integer>> loadedImageData;
try {
loadedImageData = loadImagesFromStream(imageFile);
} catch (final IOException ex) {
throw new PluginException(PluginNotificationLevel.ERROR, ex);
}
final BufferedImage firstImage = loadedImageData.getFirst().get(0);
images.add(firstImage);
final AffineTransform identity = new AffineTransform();
identity.setToIdentity();
for (int i = 1; i < loadedImageData.getFirst().size(); i++) {
final BufferedImage currentImage = loadedImageData.getFirst().get(i);
final BufferedImage image = new BufferedImage(firstImage.getWidth(), firstImage.getHeight(), firstImage.getType());
final Graphics2D g2d = image.createGraphics();
g2d.drawImage(images.get(i - 1), identity, null);
g2d.drawImage(currentImage, new AffineTransform(1, 0, 0, 1, loadedImageData.getSecond().get(i), loadedImageData.getThird().get(i)), null);
g2d.dispose();
images.add(image);
}
} else {
final BufferedImage loadedImageData;
try {
loadedImageData = loadImage(imageFile);
images.add(loadedImageData);
} catch (final IOException ex) {
LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
}
}
final int vertexVisibilityAttributeId;
final boolean multipleFrames = images.size() > 1;
if (multipleFrames) {
final int attrLayers = graph.addAttribute(GraphElementType.GRAPH, IntegerAttributeDescription.ATTRIBUTE_NAME, "layers", "layers", 0, null);
graph.setIntValue(attrLayers, 0, images.size());
vertexVisibilityAttributeId = VisualConcept.VertexAttribute.VISIBILITY.get(graph);
} else {
vertexVisibilityAttributeId = Graph.NOT_FOUND;
}
final int vertexIdentifierAttributeId = VisualConcept.VertexAttribute.IDENTIFIER.get(graph);
final int vertexColorAttributeId = VisualConcept.VertexAttribute.COLOR.get(graph);
final int vertexBackgroundIconAttributeId = VisualConcept.VertexAttribute.BACKGROUND_ICON.get(graph);
final int vertexXAttributeId = VisualConcept.VertexAttribute.X.get(graph);
final int vertexYAttributeId = VisualConcept.VertexAttribute.Y.get(graph);
final int vertexZAttributeId = VisualConcept.VertexAttribute.Z.get(graph);
final int vertexX2AttributeId = VisualConcept.VertexAttribute.X2.get(graph);
final int vertexY2AttributeId = VisualConcept.VertexAttribute.Y2.get(graph);
final int vertexZ2AttributeId = VisualConcept.VertexAttribute.Z2.get(graph);
final int pixelXAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "pixelX", "pixelX", "", null);
final int pixelYAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "pixelY", "pixelY", "", null);
final int redAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "red", "red", "", null);
final int greenAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "green", "green", "", null);
final int blueAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "blue", "blue", "", null);
final int alphaAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "alpha", "alpha", "", null);
final int diffSouthAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "diffSouth", "diffSouth", "", null);
final int diffNorthAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "diffNorth", "diffNorth", "", null);
final int diffEastAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "diffEast", "diffEast", "", null);
final int diffWestAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "diffWest", "diffWest", "", null);
final int transactionWeightAttributeId = AnalyticConcept.TransactionAttribute.WEIGHT.get(graph);
final boolean useVertexAttributes = pixelXAttributeId != Graph.NOT_FOUND && pixelYAttributeId != Graph.NOT_FOUND && greenAttributeId != Graph.NOT_FOUND && redAttributeId != Graph.NOT_FOUND && blueAttributeId != Graph.NOT_FOUND && alphaAttributeId != Graph.NOT_FOUND && diffSouthAttributeId != Graph.NOT_FOUND && diffNorthAttributeId != Graph.NOT_FOUND && diffEastAttributeId != Graph.NOT_FOUND && diffWestAttributeId != Graph.NOT_FOUND;
final boolean useTransAttributes = transactionWeightAttributeId != Graph.NOT_FOUND;
int frame = 0;
for (BufferedImage image : images) {
final int w = image.getWidth();
final int h = image.getHeight();
int[][] vertexIds = new int[w][h];
final float zlen = multipleFrames ? 0 : Math.min(w, h) / 4F;
final float vis = frame / (float) (images.size() - 1);
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
final int rgb = image.getRGB(x, y);
final int a = (rgb >> 24) & 0xff;
final int r = (rgb >> 16) & 0xff;
final int g = (rgb >> 8) & 0xff;
final int b = rgb & 0xff;
// Generate Z using grayscale.
final float gray = 0;
final int vxId = vertexIds[x][y] = graph.addVertex();
graph.setStringValue(vertexIdentifierAttributeId, vxId, String.format("%d,%d", x, y));
final int yinv = h - y;
ConstructionUtilities.setxyz(graph, vxId, vertexXAttributeId, vertexYAttributeId, vertexZAttributeId, x * 2, yinv * 2, -gray * zlen / 255F);
ConstructionUtilities.setxyz(graph, vxId, vertexX2AttributeId, vertexY2AttributeId, vertexZ2AttributeId, x * 2, yinv * 2, 0);
graph.setStringValue(vertexBackgroundIconAttributeId, vxId, "Background.Flat Square");
ConstellationColor color = ConstellationColor.getColorValue(r / 255F, g / 255F, b / 255F, a / 255F);
graph.setObjectValue(vertexColorAttributeId, vxId, color);
if (multipleFrames) {
graph.setFloatValue(vertexVisibilityAttributeId, vxId, vis);
}
if (useVertexAttributes) {
graph.setFloatValue(pixelXAttributeId, vxId, x);
graph.setFloatValue(pixelYAttributeId, vxId, y);
graph.setFloatValue(redAttributeId, vxId, r);
graph.setFloatValue(greenAttributeId, vxId, g);
graph.setFloatValue(blueAttributeId, vxId, b);
graph.setFloatValue(alphaAttributeId, vxId, a);
if (x > 0 && x < w - 1 && y > 0 && y < h - 1) {
final int southRGB = image.getRGB(x, y + 1);
final int northRGB = image.getRGB(x, y - 1);
final int eastRGB = image.getRGB(x + 1, y);
final int westRGB = image.getRGB(x - 1, y);
graph.setFloatValue(diffSouthAttributeId, vxId, calculateDifference(r, g, b, southRGB));
graph.setFloatValue(diffNorthAttributeId, vxId, calculateDifference(r, g, b, northRGB));
graph.setFloatValue(diffEastAttributeId, vxId, calculateDifference(r, g, b, eastRGB));
graph.setFloatValue(diffWestAttributeId, vxId, calculateDifference(r, g, b, westRGB));
}
}
if (useTransAttributes) {
if (x > 0) {
final int transactionId = graph.addTransaction(vxId, vertexIds[x - 1][y], false);
ConstellationColor otherColor = (ConstellationColor) graph.getObjectValue(vertexColorAttributeId, vertexIds[x - 1][y]);
graph.setFloatValue(transactionWeightAttributeId, transactionId, calculateWeight(color, otherColor));
}
if (y > 0) {
final int transactionId = graph.addTransaction(vxId, vertexIds[x][y - 1], false);
ConstellationColor otherColor = (ConstellationColor) graph.getObjectValue(vertexColorAttributeId, vertexIds[x][y - 1]);
graph.setFloatValue(transactionWeightAttributeId, transactionId, calculateWeight(color, otherColor));
}
}
}
}
frame++;
}
}
PluginExecution.withPlugin(InteractiveGraphPluginRegistry.RESET_VIEW).executeNow(graph);
interaction.setProgress(1, 0, "Completed successfully", true);
}
use of au.gov.asd.tac.constellation.utilities.color.ConstellationColor in project constellation by constellation-app.
the class TimelinePanel method updateTimeline.
public void updateTimeline(final GraphReadMethods graph, final boolean selectedOnly, final ZoneId zoneId) {
final XYChart.Series<Number, Number> series = new XYChart.Series<>();
// Graph attribute ids:
final String colorAttrDesc = new ColorAttributeDescription().getName();
final int colorTransAttr = graph.getAttribute(GraphElementType.TRANSACTION, colorAttrDesc);
final int colorVertAttr = graph.getAttribute(GraphElementType.VERTEX, colorAttrDesc);
final int selectedTransAttr = VisualConcept.TransactionAttribute.SELECTED.get(graph);
final int selectedVertAttr = VisualConcept.VertexAttribute.SELECTED.get(graph);
final int labelVertAttr = graph.getAttribute(GraphElementType.VERTEX, nodeLabelAttr);
long lowestObservedY = Long.MAX_VALUE;
long highestObservedY = Long.MIN_VALUE;
for (final TreeElement element : clusteringManager.getElementsToDraw()) {
XYChart.Data<Number, Number> nodeItem = element.getNodeItem();
if (nodeItem == null) {
if (element instanceof TreeLeaf) {
final TreeLeaf leaf = (TreeLeaf) element;
final int transactionID = leaf.getId();
// Get the color for this transaction:
ConstellationColor col = ConstellationColor.getColorValue(graph.getStringValue(colorTransAttr, transactionID));
final Color transColor = col != null ? new Color(col.getRed(), col.getGreen(), col.getBlue(), col.getAlpha()) : FALLBACK_COLOUR;
// Get the selection status for the transaction:
final boolean transSelected = graph.getBooleanValue(selectedTransAttr, transactionID);
// Get the source and destination vertices, and their respective colors:
final int sourceA = graph.getTransactionSourceVertex(transactionID);
final int sourceB = graph.getTransactionDestinationVertex(transactionID);
// Get the color for each vertex:
col = ConstellationColor.getColorValue(graph.getStringValue(colorVertAttr, sourceA));
final Color sourceAColor = col != null ? new Color(col.getRed(), col.getGreen(), col.getBlue(), col.getAlpha()) : FALLBACK_COLOUR;
col = ConstellationColor.getColorValue(graph.getStringValue(colorVertAttr, sourceB));
final Color sourceBColor = col != null ? new Color(col.getRed(), col.getGreen(), col.getBlue(), col.getAlpha()) : FALLBACK_COLOUR;
// Get the selection state for each vertex:
final boolean sourceASelected = graph.getBooleanValue(selectedVertAttr, sourceA);
final boolean sourceBSelected = graph.getBooleanValue(selectedVertAttr, sourceB);
// Get the label for each vertex:
String sourceALabel = null;
String sourceBLabel = null;
if (labelVertAttr != Graph.NOT_FOUND) {
sourceALabel = graph.getStringValue(labelVertAttr, sourceA);
sourceBLabel = graph.getStringValue(labelVertAttr, sourceB);
}
boolean isElementSelected = GraphManager.getDefault().isElementSelected();
isElementSelected |= sourceASelected || sourceBSelected || transSelected;
GraphManager.getDefault().setElementSelected(isElementSelected);
// Get the directionality of the transaction:
final int directionality = graph.getTransactionDirection(transactionID);
final Vertex vertexA;
final Vertex vertexB;
final Transaction transaction;
if (sourceA > sourceB) {
vertexA = new Vertex(sourceA, sourceA, sourceALabel, sourceAColor, sourceASelected, transSelected, btnShowLabels.isSelected());
vertexB = new Vertex(sourceB, sourceB, sourceBLabel, sourceBColor, sourceBSelected, transSelected, btnShowLabels.isSelected());
if (directionality == Graph.DOWNHILL) {
final String label = labelMaker(sourceALabel, ARROW_CHAR, sourceBLabel);
transaction = new Transaction(transactionID, transColor, label, Transaction.DIRECTED_UP, transSelected);
} else if (directionality == Graph.UPHILL) {
throw new IllegalArgumentException("source > dest is always downhill");
} else {
// Undirected / Bi-directional
final String label = labelMaker(sourceALabel, '-', sourceBLabel);
transaction = new Transaction(transactionID, transColor, label, transSelected);
}
} else if (sourceA < sourceB) {
vertexA = new Vertex(sourceB, sourceB, sourceBLabel, sourceBColor, sourceBSelected, transSelected, btnShowLabels.isSelected());
vertexB = new Vertex(sourceA, sourceA, sourceALabel, sourceAColor, sourceASelected, transSelected, btnShowLabels.isSelected());
if (directionality == Graph.DOWNHILL) {
throw new IllegalArgumentException("source < dest is always uphill");
// final String label = labelMaker(sourceBLabel, ARROW_CHAR, sourceALabel);
// transaction = new Transaction(transactionID, transColor, label, Transaction.DIRECTED_UP, transSelected);
} else if (directionality == Graph.UPHILL) {
final String label = labelMaker(sourceALabel, ARROW_CHAR, sourceBLabel);
transaction = new Transaction(transactionID, transColor, label, Transaction.DIRECTED_DOWN, transSelected);
} else {
// Undirected / Bi-directional
final String label = labelMaker(sourceBLabel, '-', sourceALabel);
transaction = new Transaction(transactionID, transColor, label, transSelected);
}
} else {
// Same source and destination: a loop.
vertexA = new Vertex(sourceA, sourceA, sourceALabel, sourceAColor, sourceASelected, transSelected, btnShowLabels.isSelected());
vertexB = new Vertex(sourceA, sourceA, sourceALabel, sourceAColor, sourceASelected, transSelected, btnShowLabels.isSelected());
transaction = new Transaction(transactionID, transColor, sourceALabel, transSelected);
}
nodeItem = new XYChart.Data<>(leaf.getDatetime(), (Math.max(sourceA, sourceB) - Math.min(sourceA, sourceB)), new Interaction(vertexA, vertexB, transaction, btnShowLabels.isSelected()));
element.setNodeItem(nodeItem);
} else {
nodeItem = new XYChart.Data<>(element.getLowerTimeExtent(), element.getLowerDisplayPos(), new Cluster(element.getLowerTimeExtent(), element.getUpperTimeExtent(), element.getLowerDisplayPos(), element.getUpperDisplayPos(), element.getCount(), element.getSelectedCount(), element.anyNodesSelected()));
element.setNodeItem(nodeItem);
}
}
if (nodeItem != null) {
series.getData().add(nodeItem);
lowestObservedY = Math.min(element.getLowerDisplayPos(), lowestObservedY);
highestObservedY = Math.max(element.getUpperDisplayPos(), highestObservedY);
}
}
timeline.populate(series, lowestObservedY, highestObservedY, selectedOnly, zoneId);
}
use of au.gov.asd.tac.constellation.utilities.color.ConstellationColor in project constellation by constellation-app.
the class SchemaElementTypeNGTest method testGetColor.
/**
* Test of getColor method, of class SchemaElementType.
*/
@Test
public void testGetColor() {
SchemaElementType<SchemaVertexType> instance = SchemaVertexType.unknownType();
ConstellationColor expResult = ConstellationColor.GREY;
ConstellationColor result = instance.getColor();
assertEquals(result, expResult);
}
use of au.gov.asd.tac.constellation.utilities.color.ConstellationColor in project constellation by constellation-app.
the class DefaultCustomIconProviderNGTest method testAddIcon.
/**
* Test of addIcon method, of class DefaultCustomIconProvider.
*
* @param iconManager
* @throws URISyntaxException
*/
public void testAddIcon(final MockedStatic<IconManager> iconManager) throws URISyntaxException {
// Create a test icon
final ConstellationColor ICON_COLOR = ConstellationColor.BLUEBERRY;
final ConstellationIcon ICON_BACKGROUND = DefaultIconProvider.FLAT_SQUARE;
final ConstellationIcon ICON_SYMBOL = AnalyticIconProvider.STAR;
ConstellationIcon icon = new ConstellationIcon.Builder("TestIcon", new ImageIconData((BufferedImage) ImageUtilities.mergeImages(ICON_BACKGROUND.buildBufferedImage(16, ICON_COLOR.getJavaColor()), ICON_SYMBOL.buildBufferedImage(16), 0, 0))).build();
iconManager.when(() -> IconManager.iconExists(Mockito.any())).thenReturn(false);
DefaultCustomIconProvider instance = new DefaultCustomIconProvider();
final boolean result = instance.addIcon(icon);
assertEquals(result, true);
// Clean up for after the test
if (DefaultCustomIconProviderNGTest.class.getResource("resources/TestIcon.png") != null) {
File removeFile = new File(DefaultCustomIconProviderNGTest.class.getResource("resources/TestIcon.png").toURI());
removeFile.delete();
}
}
use of au.gov.asd.tac.constellation.utilities.color.ConstellationColor in project constellation by constellation-app.
the class DefaultCustomIconProviderNGTest method runTests.
/**
* Runner for tests
*
* @throws java.net.URISyntaxException
*/
@Test(expectedExceptions = NullPointerException.class)
public void runTests() throws URISyntaxException {
try (MockedStatic<DefaultCustomIconProvider> defaultCustomIconProviderMock = Mockito.mockStatic(DefaultCustomIconProvider.class);
MockedStatic<IconManager> iconManagerMock = Mockito.mockStatic(IconManager.class)) {
// Get a test directory location for the getIconDirectory call
URL exampleIcon = DefaultCustomIconProviderNGTest.class.getResource("resources/");
File testFile = new File(exampleIcon.toURI());
defaultCustomIconProviderMock.when(() -> DefaultCustomIconProvider.getIconDirectory()).thenReturn(testFile);
// Create a test icon to be removed by testRemoveIcon
final ConstellationColor ICON_COLOR = ConstellationColor.BLUEBERRY;
final ConstellationIcon ICON_BACKGROUND = DefaultIconProvider.FLAT_SQUARE;
final ConstellationIcon ICON_SYMBOL = AnalyticIconProvider.STAR;
ConstellationIcon icon = new ConstellationIcon.Builder(TEST_ICON_NAME, new ImageIconData((BufferedImage) ImageUtilities.mergeImages(ICON_BACKGROUND.buildBufferedImage(16, ICON_COLOR.getJavaColor()), ICON_SYMBOL.buildBufferedImage(16), 0, 0))).build();
icon.setEditable(true);
DefaultCustomIconProvider instance = new DefaultCustomIconProvider();
// Add a test icon to be removed later
instance.addIcon(icon);
// Run testAddIcon
testAddIcon(iconManagerMock);
// Run testAddIconFileDoesExist
testAddIconFileDoesExist(icon);
// Run testRemoveIcon
testRemoveIcon(iconManagerMock, icon);
// Run testRemoveIconDoesNotExist
testRemoveIconDoesNotExist();
// Run testLoadIcons
testLoadIcons(testFile);
// Verify defaultCustomIconProvider.getIconDirectory was called the correct number of times
defaultCustomIconProviderMock.verify(() -> DefaultCustomIconProvider.getIconDirectory(), times(6));
}
}
Aggregations