Created
August 11, 2021 13:48
-
-
Save trischbeck/3e2b656f97968eb6afb12cf3560c6fe7 to your computer and use it in GitHub Desktop.
#jarchi Color all assessment elements in the model according to a given property.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
Script Name: colorAssessments | |
Purpose: Color all assessment elements in the model according to a given property. | |
Dependening on this property's value the color is set on a red-yellow-green scale. | |
default values: property_name = "bewertung"; min_value = -10 (weak); max_value = 10 (strong) | |
Thomas Rischbeck, rischbeck@itmc.ch, (C) ITMC 2021 | |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
var verbose = false; | |
var property_name = "bewertung"; | |
var min_Value = -10; | |
var max_Value = 10; | |
colorAssessments(); | |
function colorAssessments() { | |
// Iterate through all assessment elements in the model | |
$("element").filter("assessment").forEach(function(element) { | |
var bewertung = element.prop(property_name); | |
if (isNaN(bewertung) ) { | |
// do nothing | |
} else { | |
// map min_Value - max_Value to 1 - 100 | |
try { | |
var bewInt = parseInt(bewertung); | |
var colScale = 100 - (bewertung - min_Value)/(max_Value-min_Value)*100; | |
//var colScale = 100-((parseInt(bewertung) + 10) * 5); | |
//console.log("> found " + element.name + " - bewertung: " + bewertung + " colscale: " + colScale); | |
var fillColor = getColorOnRYGScale (colScale); | |
$(element).objectRefs().attr("fillColor", fillColor); | |
$(element).objectRefs().attr("fontColor", 0); | |
} catch (error) {} | |
} | |
}); | |
} | |
// get a color between red - yellow - green | |
// depending on the number (1-100) | |
function getColorOnRYGScale(n) { | |
var red, green, blue; | |
if (n <= 50) green = 255; else green = Math.round(510 - (n*5.1)); | |
if (n <= 50) red = Math.round((n*5.1)); else red = 255; | |
blue = 0; | |
if (verbose) console.log(n, red, green, blue); | |
return convertRGBToHexString(red, green, blue); | |
} | |
// Helper function - convert rgb values to a hex color string. Format is #rrggbb | |
function convertRGBToHexString(red, green, blue) { | |
red = red.toString(16); | |
if(red.length == 1) red = "0" + red; | |
green = green.toString(16); | |
if(green.length == 1) green = "0" + green; | |
blue = blue.toString(16); | |
if(blue.length == 1) blue = "0" + blue; | |
return '#' + red + green + blue; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment