UNPKG

molstar

Version:

A comprehensive macromolecular library.

130 lines (129 loc) 7.32 kB
"use strict"; /** * Copyright (c) 2023-2024 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Adam Midlik <midlik@gmail.com> */ Object.defineProperty(exports, "__esModule", { value: true }); exports.MultilayerColorThemeName = exports.DefaultMultilayerColorThemeProps = exports.NoColor = void 0; exports.makeMultilayerColorThemeParams = makeMultilayerColorThemeParams; exports.makeMultilayerColorThemeProvider = makeMultilayerColorThemeProvider; const structure_1 = require("../../../mol-model/structure"); const categories_1 = require("../../../mol-theme/color/categories"); const color_1 = require("../../../mol-util/color"); const names_1 = require("../../../mol-util/color/names"); const param_definition_1 = require("../../../mol-util/param-definition"); const string_1 = require("../../../mol-util/string"); const is_mvs_model_prop_1 = require("./is-mvs-model-prop"); const selector_1 = require("./selector"); /** Special value that can be used as color with null-like semantic (i.e. "no color provided"). * By some lucky coincidence, Mol* treats -1 as white. */ exports.NoColor = (0, color_1.Color)(-1); /** Return true if `color` is a real color, false if it is `NoColor`. */ function isValidColor(color) { return color >= 0; } const DefaultBackgroundColor = names_1.ColorNames.white; /** Parameter definition for color theme "Multilayer" */ function makeMultilayerColorThemeParams(colorThemeRegistry, ctx) { const colorThemeInfo = { help: (value) => { const { name, params } = value; const p = colorThemeRegistry.get(name); const ct = p.factory({}, params); return { description: ct.description, legend: ct.legend }; } }; const nestedThemeTypes = colorThemeRegistry.types.filter(([name, label, category]) => name !== exports.MultilayerColorThemeName && colorThemeRegistry.get(name).isApplicable(ctx)); // Adding 'multilayer' theme itself would cause infinite recursion return { layers: param_definition_1.ParamDefinition.ObjectList({ theme: param_definition_1.ParamDefinition.Mapped('uniform', nestedThemeTypes, name => param_definition_1.ParamDefinition.Group(colorThemeRegistry.get(name).getParams({ structure: structure_1.Structure.Empty })), colorThemeInfo), selection: selector_1.SelectorParams, }, obj => (0, string_1.stringToWords)(obj.theme.name), { description: 'A list of layers, each defining a color theme. The last listed layer is the top layer (applies first). If the top layer does not provide color for a location or its selection does not cover the location, the underneath layers will apply.' }), background: param_definition_1.ParamDefinition.Color(DefaultBackgroundColor, { description: 'Color for elements where no layer applies' }), }; } /** Default values for `MultilayerColorThemeProps` */ exports.DefaultMultilayerColorThemeProps = { layers: [], background: DefaultBackgroundColor }; /** Return color theme that assigns colors based on a list of nested color themes (layers). * The last layer in the list whose selection covers the given location * and which provides a valid (non-negative) color value will be used. * If a nested theme provider has `ensureCustomProperties` methods, these will not be called automatically * (the caller must ensure that any required custom properties be attached). */ function makeMultilayerColorTheme(ctx, props, colorThemeRegistry) { var _a; const colorLayers = []; // undefined elementSet means 'all' for (let i = props.layers.length - 1; i >= 0; i--) { // iterate from end to get top layer first, bottom layer last const layer = props.layers[i]; const themeProvider = colorThemeRegistry.get(layer.theme.name); if (!themeProvider) { console.warn(`Skipping color theme '${layer.theme.name}', cannot find it in registry.`); continue; } if ((_a = themeProvider.ensureCustomProperties) === null || _a === void 0 ? void 0 : _a.attach) { console.warn(`Multilayer color theme: layer "${themeProvider.name}" has ensureCustomProperties.attach method, but Multilayer color theme does not call it. If the layer does not work, make sure you call ensureCustomProperties.attach somewhere.`); } const theme = themeProvider.factory(ctx, layer.theme.params); switch (theme.granularity) { case 'uniform': case 'instance': case 'group': case 'groupInstance': case 'vertex': case 'vertexInstance': const elementSet = (0, selector_1.isSelectorAll)(layer.selection) ? undefined : selector_1.ElementSet.fromSelector(ctx.structure, layer.selection); // treating 'all' specially for performance reasons (it's expected to be used most often) colorLayers.push({ color: theme.color, elementSet }); break; default: console.warn(`Skipping color theme '${layer.theme.name}', cannot process granularity '${theme.granularity}'`); } } ; function structureElementColor(loc, isSecondary) { for (const layer of colorLayers) { const matches = !layer.elementSet || selector_1.ElementSet.has(layer.elementSet, loc); if (!matches) continue; const color = layer.color(loc, isSecondary); if (!isValidColor(color)) continue; return color; } return props.background; } const auxLocation = structure_1.StructureElement.Location.create(ctx.structure); const color = (location, isSecondary) => { if (structure_1.StructureElement.Location.is(location)) { return structureElementColor(location, isSecondary); } else if (structure_1.Bond.isLocation(location)) { // this will be applied for each bond twice, to get color of each half (a* refers to the adjacent atom, b* to the opposite atom) auxLocation.unit = location.aUnit; auxLocation.element = location.aUnit.elements[location.aIndex]; return structureElementColor(auxLocation, isSecondary); } return props.background; }; return { factory: (ctx_, props_) => makeMultilayerColorTheme(ctx_, props_, colorThemeRegistry), granularity: 'group', preferSmoothing: true, color: color, props: props, description: 'Combines colors from multiple color themes.', }; } /** Unique name for "Multilayer" color theme */ exports.MultilayerColorThemeName = 'mvs-multilayer'; /** A thingy that is needed to register color theme "Multilayer" */ function makeMultilayerColorThemeProvider(colorThemeRegistry) { return { name: exports.MultilayerColorThemeName, label: 'MVS Multi-layer', category: categories_1.ColorThemeCategory.Misc, factory: (ctx, props) => makeMultilayerColorTheme(ctx, props, colorThemeRegistry), getParams: (ctx) => makeMultilayerColorThemeParams(colorThemeRegistry, ctx), defaultValues: exports.DefaultMultilayerColorThemeProps, isApplicable: (ctx) => !!ctx.structure && (0, is_mvs_model_prop_1.isMVSStructure)(ctx.structure), }; }