model-index.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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, 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: 'scale', scaleList: 'red-yellow-blue' }),
  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. const palette = getPalette(models.length, props)
  29. legend = palette.legend
  30. const modelColor = new Map<string, Color>()
  31. for (let i = 0, il = models.length; i <il; ++i) {
  32. modelColor.set(models[i].id, palette.color(i))
  33. }
  34. color = (location: Location): Color => {
  35. if (StructureElement.Location.is(location)) {
  36. return modelColor.get(location.unit.model.id)!
  37. } else if (Bond.isLocation(location)) {
  38. return modelColor.get(location.aUnit.model.id)!
  39. }
  40. return DefaultColor
  41. }
  42. } else {
  43. color = () => DefaultColor
  44. }
  45. return {
  46. factory: ModelIndexColorTheme,
  47. granularity: 'instance',
  48. color,
  49. props,
  50. description: Description,
  51. legend
  52. }
  53. }
  54. export const ModelIndexColorThemeProvider: ColorTheme.Provider<ModelIndexColorThemeParams> = {
  55. label: 'Model Index',
  56. factory: ModelIndexColorTheme,
  57. getParams: getModelIndexColorThemeParams,
  58. defaultValues: PD.getDefaultValues(ModelIndexColorThemeParams),
  59. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure && ctx.structure.models.length > 1
  60. }