@@ -145,19 +145,31 @@ import { computeInvlerp } from "#src/util/lerp.js";
145145
146146const MAX_LAYER_BAR_UI_INDICATOR_COLORS = 6 ;
147147
148- export interface SegmentPropertyColor {
148+ export interface SegmentPropertyColorBase {
149149 type : "tag" | "numeric" ;
150150 active : boolean ;
151- property ?: string ;
152- map ?: Map < string , string > ; // TODO is there a color type?
153- options ?: {
151+ }
152+
153+ export interface SegmentPropertyColorNumeric extends SegmentPropertyColorBase {
154+ type : "numeric" ;
155+ active : boolean ;
156+ property : string ;
157+ options : {
154158 min ?: number ;
155159 max ?: number ;
156160 minColor : string ;
157161 maxColor : string ;
158162 } ;
159163}
160164
165+ export interface SegmentPropertyColorTag extends SegmentPropertyColorBase {
166+ type : "tag" ;
167+ active : boolean ;
168+ map : Map < string , string > ;
169+ }
170+
171+ export type SegmentPropertyColor = SegmentPropertyColorNumeric | SegmentPropertyColorTag ;
172+
161173export class SegmentationUserLayerGroupState
162174 extends RefCounted
163175 implements SegmentationGroupState
@@ -325,42 +337,29 @@ export class SegmentationUserLayerColorGroupState
325337 this . segmentPropertyColorsMap = this . registerDisposer (
326338 makeCachedDerivedWatchableValue (
327339 ( segmentPropertyMap , segmentPropertyColors ) => {
328- console . log ( "update segmentPropertyColorsMap" ) ;
329- segmentPropertyColors ;
330340 const map = new Uint64Map ( ) ;
331341 if ( ! segmentPropertyMap ) {
332342 return map ;
333343 }
334-
335344 const { tags : tagsProperty } = segmentPropertyMap ;
336-
337- console . log ( "tags" , segmentPropertyMap . tags ) ;
338-
339345 for ( const propertyColor of segmentPropertyColors ) {
340346 if ( ! propertyColor . active ) continue ;
341-
342347 if ( propertyColor . type === "tag" && tagsProperty ) {
343348 const { tags, values } = tagsProperty ;
344-
345- // const colors = todo performance, cache colors as bigints
346-
347349 const bigIntColors = new Map < string , bigint > ( ) ;
348350 for ( const [ tag , colorString ] of propertyColor . map ! . entries ( ) ) {
349351 const color = parseRGBColorSpecification ( colorString ) ;
350352 bigIntColors . set ( tag , BigInt ( packColor ( color ) ) ) ;
351- console . log ( "tag color" , tag , colorString , color ) ;
352353 }
353-
354354 for ( const [ index , id ] of (
355355 segmentPropertyMap . segmentPropertyMap . inlineProperties ?. ids ||
356356 [ ]
357357 ) . entries ( ) ) {
358- if ( map . has ( id ) ) continue ; // priority first color match
358+ if ( map . has ( id ) ) continue ; // prioritize first color match
359359 const tagIndices = values [ index ] ;
360360 const segmentTags = new Set (
361361 tagIndices . split ( "" ) . map ( ( x ) => tags [ x . charCodeAt ( 0 ) ] ) ,
362362 ) ;
363-
364363 for ( const [ tag , color ] of bigIntColors . entries ( ) ) {
365364 if ( segmentTags . has ( tag ) ) {
366365 map . set ( id , color ) ;
@@ -369,7 +368,7 @@ export class SegmentationUserLayerColorGroupState
369368 }
370369 }
371370 } else if ( propertyColor . type === "numeric" ) {
372- const options = propertyColor . options ! ;
371+ const options = propertyColor . options ;
373372 const { numericalProperties } = segmentPropertyMap ;
374373 const numericalProperty = numericalProperties . find (
375374 ( x ) => x . id === propertyColor . property ,
@@ -381,11 +380,11 @@ export class SegmentationUserLayerColorGroupState
381380 const { values, bounds } = numericalProperty ;
382381 const min = Number ( options . min ?? bounds [ 0 ] ) ;
383382 const max = Number ( options . max ?? bounds [ 1 ] ) ;
384-
385383 for ( const [ index , id ] of (
386384 segmentPropertyMap . segmentPropertyMap . inlineProperties ?. ids ||
387385 [ ]
388386 ) . entries ( ) ) {
387+ if ( map . has ( id ) ) continue ; // prioritize first color match
389388 const value = values [ index ] ;
390389 if ( Number . isNaN ( value ) ) continue ;
391390 const normalized = Math . min (
@@ -398,7 +397,6 @@ export class SegmentationUserLayerColorGroupState
398397 }
399398 }
400399 }
401- console . log ( "map" , map . size ) ;
402400 return map ;
403401 } ,
404402 [
@@ -465,7 +463,7 @@ export class SegmentationUserLayerColorGroupState
465463 if ( type === "tag" ) {
466464 const map = verifyObjectProperty (
467465 propertyColor ,
468- json_keys . MAP_JSON_KEY ,
466+ "map" ,
469467 ( x ) => {
470468 verifyObject ( x ) ;
471469 const res = new Map < string , string > ( ) ;
@@ -543,22 +541,25 @@ export class SegmentationUserLayerColorGroupState
543541 }
544542 }
545543 if ( segmentPropertyColors . length > 0 ) {
546- const j : any [ ] = ( x [ json_keys . SEGMENT_PROPERTY_COLORS_JSON_KEY ] = [ ] ) ;
547- for ( const propertyColor of segmentPropertyColors ) {
548- const p : any = { } ;
549- p [ "type" ] = propertyColor . type ;
550- p [ "property" ] = propertyColor . property ;
551- p [ "active" ] = propertyColor . active ? undefined : false ;
552- if ( propertyColor . type === "tag" ) {
553- const map : any = ( p [ json_keys . MAP_JSON_KEY ] = { } ) ;
554- for ( const [ key , value ] of propertyColor . map ! . entries ( ) ) {
555- map [ key . toString ( ) ] = value ;
556- }
544+ x [ json_keys . SEGMENT_PROPERTY_COLORS_JSON_KEY ] = segmentPropertyColors . map ( ( propertyColor ) => {
545+ const { type, active} = propertyColor ;
546+ if ( type === "numeric" ) {
547+ const { property, options } = propertyColor ;
548+ return {
549+ type,
550+ active,
551+ property,
552+ options
553+ } ;
557554 } else {
558- p [ "options" ] = propertyColor . options ;
555+ const { map } = propertyColor ;
556+ return {
557+ type,
558+ active,
559+ map : Object . fromEntries ( map . entries ( ) . map ( ( [ k , v ] ) => [ k . toString ( ) , v ] ) ) ,
560+ } ;
559561 }
560- j . push ( p ) ;
561- }
562+ } ) ;
562563 }
563564 return x ;
564565 }
0 commit comments