@@ -1290,58 +1290,65 @@ export const ProteinViewer = forwardRef<ProteinViewerRef, ProteinViewerProps>(({
12901290 // KEY FIX: DELEGATE base coloring to NGL's own standard scheme instances.
12911291 console . log ( "Applying Unified Coloring via Delegation" ) ;
12921292
1293- const atomColormap = new Map < number , number > ( ) ; // Index -> Hex
1294-
1295- // Instantiate the Base Scheme (e.g. 'chainid', 'sstruc')
1296- let BaseSchemeClass ;
12971293 try {
1298- BaseSchemeClass = NGL . ColormakerRegistry . getScheme ( currentColoring ) ;
1299- } catch ( e ) {
1300- // Fallback if scheme name is weird
1301- BaseSchemeClass = NGL . ColormakerRegistry . getScheme ( 'chainid' ) ;
1302- }
1303-
1304- // Initialize it with the component's structure statistics (important for bfactor/hydrophobicity scales)
1305- const baseScheme = new BaseSchemeClass ( {
1306- structure : component . structure ,
1307- scheme : currentColoring ,
1308- // Pass palette if needed (custom handling might be needed for 'hydrophobicity' props,
1309- // but standard 'chainid'/'sstruc' work out of box)
1310- } ) ;
1294+ const atomColormap = new Map < number , number > ( ) ; // Index -> Hex
13111295
1312- // A. Base Colors for ALL atoms (via NGL)
1313- component . structure . eachAtom ( ( atom : any ) => {
1314- let color = 0xCCCCCC ;
1296+ // Instantiate the Base Scheme (e.g. 'chainid', 'sstruc')
1297+ let BaseSchemeClass ;
13151298 try {
1316- color = baseScheme . atomColor ( atom ) ;
1299+ BaseSchemeClass = NGL . ColormakerRegistry . getScheme ( currentColoring ) ;
13171300 } catch ( e ) {
1318- // fallback
1301+ // Fallback if scheme name is weird
1302+ BaseSchemeClass = NGL . ColormakerRegistry . getScheme ( 'chainid' ) ;
13191303 }
1320- atomColormap . set ( atom . index , color ) ;
1321- } ) ;
13221304
1323- // B. Apply Custom Overrides
1324- customColors . forEach ( rule => {
1325- if ( rule . color && rule . target ) {
1305+ // Initialize it with the component's structure statistics
1306+ const baseScheme = new BaseSchemeClass ( {
1307+ structure : component . structure ,
1308+ scheme : currentColoring ,
1309+ params : {
1310+ // Pass palette params just in case functionality depends on it
1311+ scale : currentColoring === 'bfactor' ? 'rwb' : undefined
1312+ }
1313+ } ) ;
1314+
1315+ // A. Base Colors for ALL atoms (via NGL)
1316+ component . structure . eachAtom ( ( atom : any ) => {
1317+ let color = 0xCCCCCC ;
13261318 try {
1327- const sel = new NGL . Selection ( rule . target ) ;
1328- const colorHex = new NGL . Color ( rule . color ) . getHex ( ) ;
1329- component . structure . eachAtom ( ( atom : any ) => {
1330- atomColormap . set ( atom . index , colorHex ) ;
1331- } , sel ) ;
1319+ color = baseScheme . atomColor ( atom ) ;
13321320 } catch ( e ) { }
1333- }
1334- } ) ;
1321+ atomColormap . set ( atom . index , color ) ;
1322+ } ) ;
13351323
1336- // C. Register & Apply
1337- const unifiedSchemeId = `unified_${ Date . now ( ) } _${ Math . random ( ) } ` ;
1338- NGL . ColormakerRegistry . addScheme ( function ( this : any ) {
1339- this . atomColor = function ( atom : any ) {
1340- return atomColormap . get ( atom . index ) || 0xCCCCCC ;
1341- } ;
1342- } , unifiedSchemeId ) ;
1324+ // B. Apply Custom Overrides
1325+ customColors . forEach ( rule => {
1326+ if ( rule . color && rule . target ) {
1327+ try {
1328+ const sel = new NGL . Selection ( rule . target ) ;
1329+ const colorHex = new NGL . Color ( rule . color ) . getHex ( ) ;
1330+ component . structure . eachAtom ( ( atom : any ) => {
1331+ atomColormap . set ( atom . index , colorHex ) ;
1332+ } , sel ) ;
1333+ } catch ( e ) { }
1334+ }
1335+ } ) ;
1336+
1337+ // C. Register & Apply
1338+ const unifiedSchemeId = `unified_${ Date . now ( ) } _${ Math . random ( ) } ` ;
1339+ NGL . ColormakerRegistry . addScheme ( function ( this : any ) {
1340+ this . atomColor = function ( atom : any ) {
1341+ return atomColormap . get ( atom . index ) || 0xCCCCCC ;
1342+ } ;
1343+ } , unifiedSchemeId ) ;
13431344
1344- component . addRepresentation ( repType , { color : unifiedSchemeId } ) ;
1345+ component . addRepresentation ( repType , { color : unifiedSchemeId } ) ;
1346+
1347+ } catch ( delegationError ) {
1348+ console . error ( "Custom Scheme Failed, reverting to standard:" , delegationError ) ;
1349+ // FALLBACK: If custom scheme crashes, just show standard structure so it doesn't vanish
1350+ component . addRepresentation ( repType , { color : currentColoring } ) ;
1351+ }
13451352 }
13461353
13471354 // --- OVERLAYS ---
0 commit comments