element-index.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * Copyright (c) 2018-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 { OrderedSet } from '../../mol-data/int';
  10. import { ColorTheme, LocationColor } from '../color';
  11. import { ParamDefinition as PD } from '../../mol-util/param-definition'
  12. import { ThemeDataContext } from '../../mol-theme/theme';
  13. import { getPaletteParams, getPalette } from '../../mol-util/color/palette';
  14. import { TableLegend, ScaleLegend } from '../../mol-util/legend';
  15. const DefaultColor = Color(0xCCCCCC)
  16. const Description = 'Gives every element (atom or coarse sphere/gaussian) a unique color based on the position (index) of the element in the list of elements in the structure.'
  17. export const ElementIndexColorThemeParams = {
  18. ...getPaletteParams({ type: 'scale', scaleList: 'red-yellow-blue' }),
  19. }
  20. export type ElementIndexColorThemeParams = typeof ElementIndexColorThemeParams
  21. export function getElementIndexColorThemeParams(ctx: ThemeDataContext) {
  22. return ElementIndexColorThemeParams // TODO return copy
  23. }
  24. export function ElementIndexColorTheme(ctx: ThemeDataContext, props: PD.Values<ElementIndexColorThemeParams>): ColorTheme<ElementIndexColorThemeParams> {
  25. let color: LocationColor
  26. let legend: ScaleLegend | TableLegend | undefined
  27. if (ctx.structure) {
  28. const { units } = ctx.structure.root
  29. const unitCount = units.length
  30. const cummulativeElementCount = new Map<number, number>()
  31. const unitIdIndex = new Map<number, number>()
  32. let elementCount = 0
  33. for (let i = 0; i < unitCount; ++i) {
  34. cummulativeElementCount.set(i, elementCount)
  35. elementCount += units[i].elements.length
  36. unitIdIndex.set(units[i].id, i)
  37. }
  38. const palette = getPalette(elementCount, props)
  39. legend = palette.legend
  40. color = (location: Location): Color => {
  41. if (StructureElement.Location.is(location)) {
  42. const unitIndex = unitIdIndex.get(location.unit.id)!
  43. const unitElementIndex = OrderedSet.findPredecessorIndex(units[unitIndex].elements, location.element)
  44. return palette.color(cummulativeElementCount.get(unitIndex)! + unitElementIndex)
  45. } else if (Bond.isLocation(location)) {
  46. const unitIndex = unitIdIndex.get(location.aUnit.id)!
  47. const unitElementIndex = OrderedSet.findPredecessorIndex(units[unitIndex].elements, location.aUnit.elements[location.aIndex])
  48. return palette.color(cummulativeElementCount.get(unitIndex)! + unitElementIndex)
  49. }
  50. return DefaultColor
  51. }
  52. } else {
  53. color = () => DefaultColor
  54. }
  55. return {
  56. factory: ElementIndexColorTheme,
  57. granularity: 'groupInstance',
  58. color,
  59. props,
  60. description: Description,
  61. legend
  62. }
  63. }
  64. export const ElementIndexColorThemeProvider: ColorTheme.Provider<ElementIndexColorThemeParams> = {
  65. label: 'Element Index',
  66. category: ColorTheme.Category.Atom,
  67. factory: ElementIndexColorTheme,
  68. getParams: getElementIndexColorThemeParams,
  69. defaultValues: PD.getDefaultValues(ElementIndexColorThemeParams),
  70. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure
  71. }