Skip to content

Instantly share code, notes, and snippets.

@timolaine
Last active November 5, 2025 14:02
Show Gist options
  • Select an option

  • Save timolaine/0337e187c54f6bbf7a3919a1501f5054 to your computer and use it in GitHub Desktop.

Select an option

Save timolaine/0337e187c54f6bbf7a3919a1501f5054 to your computer and use it in GitHub Desktop.
Colorize all relationships in a view based on property values in #jArchi
/*
* Color relations by property value
*
* Requires jArchi - https://www.archimatetool.com/blog/2018/07/02/jarchi/
*
* Original script (c) 2022 Steven Mileham
* Modifications to support relationship colorization (c) 2025 Timo Laine
*
*/
var debug = false;
var buttonLimit=5;
console.show();
console.clear();
console.log("> Starting Color relations by property value");
function generatePalette(n) {
var colors = [];
for (var i = 0; i < n; i++) {
var hue = (i * 360 / n); // Evenly spaced hues
var sat = 100; // Saturation (0-100)
var light = 40; // Lightness (0-100)
colors.push(hslToHex(hue, sat, light));
}
return colors;
}
// Convert HSL to HEX (#RRGGBB)
function hslToHex(h, s, l) {
s /= 100;
l /= 100;
var c = (1 - Math.abs(2 * l - 1)) * s;
var x = c * (1 - Math.abs((h / 60) % 2 - 1));
var m = l - c / 2;
var r = 0, g = 0, b = 0;
if (h < 60) { r = c; g = x; b = 0; }
else if (h < 120) { r = x; g = c; b = 0; }
else if (h < 180) { r = 0; g = c; b = x; }
else if (h < 240) { r = 0; g = x; b = c; }
else if (h < 300) { r = x; g = 0; b = c; }
else { r = c; g = 0; b = x; }
r = Math.round((r + m) * 255).toString(16).padStart(2, '0');
g = Math.round((g + m) * 255).toString(16).padStart(2, '0');
b = Math.round((b + m) * 255).toString(16).padStart(2, '0');
return "#" + r + g + b;
}
function buttonDialog(message,options) {
var dialog = new org.eclipse.jface.dialogs.MessageDialog(shell,"Archi",null,message,3,options.concat(["Cancel"]),0);
var result=dialog.open();
return result==options.length?null:(result+1).toString();
}
var theView = $(selection).filter("archimate-diagram-model").first();
var propertyHash = [];
debug ? console.log(theView) : true;
if (theView) {
$(theView).find("relationship").each(function(e) {
theProps = e.prop();
for (var i=0; i<theProps.length; i++) {
if (propertyHash[theProps[i]]) {
propertyHash[theProps[i]][[e.prop(theProps[i])]]=1;
}
else
{
newArray = [];
newArray[e.prop(theProps[i])]=1;
propertyHash[theProps[i]]=newArray;
}
}
})
}
debug ? console.log(propertyHash):true;
var propertyList ="";
var i=1;
var menuArray=[];
for (var property in propertyHash){
propertyList +="("+i+") "+property+"\n";
debug ? console.log(propertyHash[property]):true;
menuArray.push(property);
i++;
}
debug ? console.log(propertyList):true;
if (theView) {
var theProperty = "-1";
while (true) {
if (menuArray.length<=buttonLimit) {
theProperty = buttonDialog("Please select a Property",menuArray);
}
else {
theProperty = window.prompt("Please select a Property by number:\n"+propertyList, "");
}
if (theProperty===null || theProperty.trim()=="" || (parseInt(theProperty.trim())>0 && parseInt(theProperty.trim())<=menuArray.length)) {
break;
}
}
if (theProperty!=null && theProperty!="") {
var valueList ="";
var valueArray = [];
var i=1;
for (var value in propertyHash[menuArray[theProperty-1]]) {
valueList += "("+i+") "+value+"\n";
valueArray.push(value);
i++;
}
var colourArray = [];
while (true) {
numberOfColours = window.prompt("Please enter number of colors required:\n(The following values are present):\n"+valueList, valueArray.length);
if (numberOfColours===null || numberOfColours.trim()=="" || (parseInt(numberOfColours.trim())>0 && parseInt(numberOfColours.trim())<=valueArray.length)) {
break;
}
}
colourPalette=generatePalette(numberOfColours);
for (i=0; i<numberOfColours;i++){
while (true) {
var colourPair = {};
if (valueArray.length<=buttonLimit) {
colourPair.value = buttonDialog("Please select a Property value for Color number "+(i+1), valueArray);
}
else {
colourPair.value = window.prompt("Please select a Property value for Color number "+(i+1)+":\n"+valueList, "");
}
colourPair.colour = colourPalette[i];
if (colourPair.value===null || colourPair.value.trim()=="" || (parseInt(colourPair.value.trim())>0 && parseInt(colourPair.value.trim())<=valueArray.length)) {
break;
}
}
colourArray[i]=colourPair;
}
var defaultColours = window.confirm("Do you wish to reset non-matching components to default colors?");
$(theView).find("relationship").each(function(e) {
var found = false;
debug ? console.log(e.name) : true;
var isProperty = e.prop(menuArray[theProperty-1]);
debug ? console.log(isProperty) : true;
for (var pair in colourArray) {
debug ? console.log(colourArray[pair]) : true;
if (isProperty == valueArray[colourArray[pair].value-1]) {
debug ? console.log("Found") : true;
e.lineColor = colourArray[pair].colour;
found = true;
}
}
if (!found && defaultColours) {
e.lineColor = null;
}
})
var oldKey = $(theView).find(".Key").filter("diagram-model-group").delete();
var theKey = theView.createObject("diagram-model-group", 12,-36-(36*colourArray.length), 240, 24+(36*colourArray.length));
theKey.borderType="1";
theKey.name = menuArray[theProperty-1];
for (var pair in colourArray) {
var aKey = theKey.createObject("diagram-model-note", 12, 24+(pair*36), 216, 24);
debug ? console.log(pair) : true;
debug ? console.log(colourArray[pair].colour) : true;
aKey.fillColor = colourArray[pair].colour;
aKey.fontColor = "#ffffff";
aKey.borderType="1";
aKey.text = /* menuArray[theProperty-1] + "=" + */ valueArray[colourArray[pair].value-1];
}
}
} else {
console.error("> Please select a view.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment