model-index.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Color } from '../../mol-util/color';
  7. import { Location } from '../../mol-model/location';
  8. import { StructureElement, Bond } from '../../mol-model/structure';
  9. import { ColorTheme, LocationColor } from '../color';
  10. import { ParamDefinition as PD } from '../../mol-util/param-definition'
  11. import { ThemeDataContext } from '../../mol-theme/theme';
  12. import { getPaletteParams, getPalette } from '../../mol-util/color/palette';
  13. import { TableLegend, ScaleLegend } from '../../mol-util/legend';
  14. const DefaultColor = Color(0xCCCCCC)
  15. const Description = 'Gives every model a unique color based on the position (index) of the model in the list of models in the structure.'
  16. export const ModelIndexColorThemeParams = {
  17. ...getPaletteParams({ type: 'colors', colorList: 'purples' }),
  18. }
  19. export type ModelIndexColorThemeParams = typeof ModelIndexColorThemeParams
  20. export function getModelIndexColorThemeParams(ctx: ThemeDataContext) {
  21. return ModelIndexColorThemeParams // TODO return copy
  22. }
  23. export function ModelIndexColorTheme(ctx: ThemeDataContext, props: PD.Values<ModelIndexColorThemeParams>): ColorTheme<ModelIndexColorThemeParams> {
  24. let color: LocationColor
  25. let legend: ScaleLegend | TableLegend | undefined
  26. if (ctx.structure) {
  27. const { models } = ctx.structure.root
  28. let size = 0;
  29. for (const m of models) size = Math.max(size, m.trajectoryInfo.size);
  30. const palette = getPalette(size, props)
  31. legend = palette.legend
  32. const modelColor = new Map<number, Color>()
  33. for (let i = 0, il = models.length; i <il; ++i) {
  34. const idx = models[i].trajectoryInfo.index;
  35. modelColor.set(models[i].trajectoryInfo.index, palette.color(idx))
  36. }
  37. color = (location: Location): Color => {
  38. if (StructureElement.Location.is(location)) {
  39. return modelColor.get(location.unit.model.trajectoryInfo.index)!
  40. } else if (Bond.isLocation(location)) {
  41. return modelColor.get(location.aUnit.model.trajectoryInfo.index)!
  42. }
  43. return DefaultColor
  44. }
  45. } else {
  46. color = () => DefaultColor
  47. }
  48. return {
  49. factory: ModelIndexColorTheme,
  50. granularity: 'instance',
  51. color,
  52. props,
  53. description: Description,
  54. legend
  55. }
  56. }
  57. export const ModelIndexColorThemeProvider: ColorTheme.Provider<ModelIndexColorThemeParams, 'model-index'> = {
  58. name: 'model-index',
  59. label: 'Model Index',
  60. category: ColorTheme.Category.Chain,
  61. factory: ModelIndexColorTheme,
  62. getParams: getModelIndexColorThemeParams,
  63. defaultValues: PD.getDefaultValues(ModelIndexColorThemeParams),
  64. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure && ctx.structure.elementCount > 0 && ctx.structure.models[0].trajectoryInfo.size > 1
  65. }