model-index.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * Copyright (c) 2019 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, Link } 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 { ScaleLegend } from '../../mol-util/color/scale';
  13. import { getPaletteParams, getPalette } from '../../mol-util/color/palette';
  14. import { TableLegend } from '../../mol-util/color/lists';
  15. const DefaultColor = Color(0xCCCCCC)
  16. const Description = 'Gives every model a unique color based on the position (index) of the model in the list of models in the structure.'
  17. export const ModelIndexColorThemeParams = {
  18. ...getPaletteParams({ type: 'scale', scaleList: 'red-yellow-blue' }),
  19. }
  20. export type ModelIndexColorThemeParams = typeof ModelIndexColorThemeParams
  21. export function getModelIndexColorThemeParams(ctx: ThemeDataContext) {
  22. return ModelIndexColorThemeParams // TODO return copy
  23. }
  24. export function ModelIndexColorTheme(ctx: ThemeDataContext, props: PD.Values<ModelIndexColorThemeParams>): ColorTheme<ModelIndexColorThemeParams> {
  25. let color: LocationColor
  26. let legend: ScaleLegend | TableLegend | undefined
  27. if (ctx.structure) {
  28. const { models } = ctx.structure.root
  29. const palette = getPalette(models.length, props)
  30. legend = palette.legend
  31. const modelColor = new Map<string, Color>()
  32. for (let i = 0, il = models.length; i <il; ++i) {
  33. modelColor.set(models[i].id, palette.color(i))
  34. }
  35. color = (location: Location): Color => {
  36. if (StructureElement.isLocation(location)) {
  37. return modelColor.get(location.unit.model.id)!
  38. } else if (Link.isLocation(location)) {
  39. return modelColor.get(location.aUnit.model.id)!
  40. }
  41. return DefaultColor
  42. }
  43. } else {
  44. color = () => DefaultColor
  45. }
  46. return {
  47. factory: ModelIndexColorTheme,
  48. granularity: 'instance',
  49. color,
  50. props,
  51. description: Description,
  52. legend
  53. }
  54. }
  55. export const ModelIndexColorThemeProvider: ColorTheme.Provider<ModelIndexColorThemeParams> = {
  56. label: 'Model Index',
  57. factory: ModelIndexColorTheme,
  58. getParams: getModelIndexColorThemeParams,
  59. defaultValues: PD.getDefaultValues(ModelIndexColorThemeParams),
  60. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure
  61. }