diff --git a/src/mol-theme/color.ts b/src/mol-theme/color.ts index 45fb46faf10f4d65cb2bd87739eae4b796a20dde..bc8104c95c6d216ce72f6192a8d32765ae46ca1d 100644 --- a/src/mol-theme/color.ts +++ b/src/mol-theme/color.ts @@ -30,6 +30,7 @@ import { UncertaintyColorThemeProvider } from './color/uncertainty'; import { EntitySourceColorThemeProvider } from './color/entity-source'; import { IllustrativeColorThemeProvider } from './color/illustrative'; import { HydrophobicityColorThemeProvider } from './color/hydrophobicity'; +import { ModelIndexColorThemeProvider } from './color/model-index'; export type LocationColor = (location: Location, isSecondary: boolean) => Color @@ -79,6 +80,7 @@ export const BuiltInColorThemes = { 'entity-source': EntitySourceColorThemeProvider, 'hydrophobicity': HydrophobicityColorThemeProvider, 'illustrative': IllustrativeColorThemeProvider, + 'model-index': ModelIndexColorThemeProvider, 'molecule-type': MoleculeTypeColorThemeProvider, 'polymer-id': PolymerIdColorThemeProvider, 'polymer-index': PolymerIndexColorThemeProvider, diff --git a/src/mol-theme/color/model-index.ts b/src/mol-theme/color/model-index.ts new file mode 100644 index 0000000000000000000000000000000000000000..1478125cdaafe35338727786b150b1957ee01eb8 --- /dev/null +++ b/src/mol-theme/color/model-index.ts @@ -0,0 +1,69 @@ +/** + * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info. + * + * @author Alexander Rose <alexander.rose@weirdbyte.de> + */ + +import { Color } from '../../mol-util/color'; +import { Location } from '../../mol-model/location'; +import { StructureElement, Link } from '../../mol-model/structure'; +import { ColorTheme, LocationColor } from '../color'; +import { ParamDefinition as PD } from '../../mol-util/param-definition' +import { ThemeDataContext } from '../../mol-theme/theme'; +import { ScaleLegend } from '../../mol-util/color/scale'; +import { getPaletteParams, getPalette } from './util'; +import { TableLegend } from '../../mol-util/color/tables'; + +const DefaultColor = Color(0xCCCCCC) +const Description = 'Gives every model a unique color based on the position (index) of the model in the list of models in the structure.' + +export const ModelIndexColorThemeParams = { + ...getPaletteParams({ scaleList: 'RedYellowBlue' }), +} +export type ModelIndexColorThemeParams = typeof ModelIndexColorThemeParams +export function getModelIndexColorThemeParams(ctx: ThemeDataContext) { + return ModelIndexColorThemeParams // TODO return copy +} + +export function ModelIndexColorTheme(ctx: ThemeDataContext, props: PD.Values<ModelIndexColorThemeParams>): ColorTheme<ModelIndexColorThemeParams> { + let color: LocationColor + let legend: ScaleLegend | TableLegend | undefined + + if (ctx.structure) { + const { models } = ctx.structure + const palette = getPalette(models.length, props) + legend = palette.legend + const modelColor = new Map<string, Color>() + for (let i = 0, il = models.length; i <il; ++i) { + modelColor.set(models[i].id, palette.color(i)) + } + + color = (location: Location): Color => { + if (StructureElement.isLocation(location)) { + return modelColor.get(location.unit.model.id)! + } else if (Link.isLocation(location)) { + return modelColor.get(location.aUnit.model.id)! + } + return DefaultColor + } + } else { + color = () => DefaultColor + } + + return { + factory: ModelIndexColorTheme, + granularity: 'instance', + color, + props, + description: Description, + legend + } +} + +export const ModelIndexColorThemeProvider: ColorTheme.Provider<ModelIndexColorThemeParams> = { + label: 'Model Index', + factory: ModelIndexColorTheme, + getParams: getModelIndexColorThemeParams, + defaultValues: PD.getDefaultValues(ModelIndexColorThemeParams), + isApplicable: (ctx: ThemeDataContext) => !!ctx.structure +} \ No newline at end of file