@@ -35,13 +35,67 @@ var aperioMap = {
3535 '4' : 'Polyline' ,
3636} ;
3737
38+ // apollo color things...
39+
40+ // Define the known color categories and their hex values
41+ const colorCategories = {
42+ "Tumor" : "#00FF00" ,
43+ "Normal_Benign" : "#FF00FF" ,
44+ "Stroma_fibrosis_inflammation" : "#FFFF00" ,
45+ "Necrosis" : "#000000" ,
46+ "Blood" : "#FF0000"
47+ } ;
48+
49+ // Convert hex to RGB
50+ function hexToRgb ( hex ) {
51+ // Remove the "#" if it's present
52+ hex = hex . replace ( "#" , "" ) ;
53+
54+ // Parse hex color to RGB components
55+ let r = parseInt ( hex . substring ( 0 , 2 ) , 16 ) ;
56+ let g = parseInt ( hex . substring ( 2 , 4 ) , 16 ) ;
57+ let b = parseInt ( hex . substring ( 4 , 6 ) , 16 ) ;
58+
59+ return { r, g, b } ;
60+ }
61+
62+ // Calculate Euclidean distance between two RGB colors
63+ function calculateColorDistance ( rgb1 , rgb2 ) {
64+ const rDiff = rgb1 . r - rgb2 . r ;
65+ const gDiff = rgb1 . g - rgb2 . g ;
66+ const bDiff = rgb1 . b - rgb2 . b ;
67+ return Math . sqrt ( rDiff * rDiff + gDiff * gDiff + bDiff * bDiff ) ;
68+ }
69+
70+ // Classify the input hex color into one of the categories
71+ function classifyColor ( hex ) {
72+ const inputRgb = hexToRgb ( hex ) ;
73+ let closestCategory = null ;
74+ let minDistance = Infinity ;
75+
76+ // Loop through all categories and find the closest match
77+ for ( const [ category , hexColor ] of Object . entries ( colorCategories ) ) {
78+ const categoryRgb = hexToRgb ( hexColor ) ;
79+ const distance = calculateColorDistance ( inputRgb , categoryRgb ) ;
80+
81+ if ( distance < minDistance ) {
82+ minDistance = distance ;
83+ closestCategory = category ;
84+ }
85+ }
86+ return closestCategory ;
87+ }
88+
89+
3890function xml2geo ( ) {
3991 let input = document . getElementById ( 'xml_in' ) . value ;
4092 xmlDoc = parser . parseFromString ( input , 'text/xml' ) ;
4193 let annotations = xmlDoc . getElementsByTagName ( 'Annotation' ) ;
4294 let slideId = document . getElementById ( 'slide_id' ) . value ;
4395 let annotName = document . getElementById ( 'annot_name' ) . value ;
4496
97+ let apolloColors = document . getElementById ( 'apollo_colors' ) . checked ;
98+
4599 // Store objects per unique color
46100 let outputMap = { } ;
47101
@@ -57,7 +111,11 @@ function xml2geo() {
57111 outputMap [ hexColor ] [ 'provenance' ] [ 'image' ] [ 'slide' ] = slideId ;
58112 outputMap [ hexColor ] [ 'provenance' ] [ 'analysis' ] [ 'execution_id' ] = randomId ;
59113 outputMap [ hexColor ] [ 'provenance' ] [ 'analysis' ] [ 'name' ] = `${ annotName } _${ hexColor } ` ;
60- outputMap [ hexColor ] [ 'properties' ] [ 'annotations' ] [ 'name' ] = `${ annotName } _${ hexColor } ` ;
114+ let colorname = hexColor ;
115+ if ( apolloColors ) {
116+ colorname = classifyColor ( hexColor )
117+ }
118+ outputMap [ hexColor ] [ 'properties' ] [ 'annotations' ] [ 'name' ] = `${ annotName } _${ colorname } ` ;
61119 }
62120
63121 let regions = annotation . getElementsByTagName ( 'Region' ) ;
0 commit comments