element-index.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ColorScale, Color } from 'mol-util/color';
  7. import { Location } from 'mol-model/location';
  8. import { StructureElement, Link } 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 { ColorListOptions, ColorListName } from 'mol-util/color/scale';
  14. const DefaultColor = Color(0xCCCCCC)
  15. 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.'
  16. export const ElementIndexColorThemeParams = {
  17. list: PD.ColorScale<ColorListName>('RedYellowBlue', ColorListOptions),
  18. }
  19. export type ElementIndexColorThemeParams = typeof ElementIndexColorThemeParams
  20. export function getElementIndexColorThemeParams(ctx: ThemeDataContext) {
  21. return ElementIndexColorThemeParams // TODO return copy
  22. }
  23. export function ElementIndexColorTheme(ctx: ThemeDataContext, props: PD.Values<ElementIndexColorThemeParams>): ColorTheme<ElementIndexColorThemeParams> {
  24. let color: LocationColor
  25. let scale = ColorScale.create({ listOrName: props.list, minLabel: 'Start', maxLabel: 'End' })
  26. if (ctx.structure) {
  27. const { units } = ctx.structure
  28. const unitCount = units.length
  29. const cummulativeElementCount = new Map<number, number>()
  30. const unitIdIndex = new Map<number, number>()
  31. let elementCount = 0
  32. for (let i = 0; i < unitCount; ++i) {
  33. cummulativeElementCount.set(i, elementCount)
  34. elementCount += units[i].elements.length
  35. unitIdIndex.set(units[i].id, i)
  36. }
  37. scale.setDomain(0, elementCount - 1)
  38. const scaleColor = scale.color
  39. color = (location: Location): Color => {
  40. if (StructureElement.isLocation(location)) {
  41. const unitIndex = unitIdIndex.get(location.unit.id)!
  42. const unitElementIndex = OrderedSet.findPredecessorIndex(location.unit.elements, location.element)
  43. return scaleColor(cummulativeElementCount.get(unitIndex)! + unitElementIndex)
  44. } else if (Link.isLocation(location)) {
  45. const unitIndex = unitIdIndex.get(location.aUnit.id)!
  46. return scaleColor(cummulativeElementCount.get(unitIndex)! + location.aIndex)
  47. }
  48. return DefaultColor
  49. }
  50. } else {
  51. color = () => DefaultColor
  52. }
  53. return {
  54. factory: ElementIndexColorTheme,
  55. granularity: 'groupInstance',
  56. color,
  57. props,
  58. description: Description,
  59. legend: scale ? scale.legend : undefined
  60. }
  61. }
  62. export const ElementIndexColorThemeProvider: ColorTheme.Provider<ElementIndexColorThemeParams> = {
  63. label: 'Element Index',
  64. factory: ElementIndexColorTheme,
  65. getParams: getElementIndexColorThemeParams,
  66. defaultValues: PD.getDefaultValues(ElementIndexColorThemeParams),
  67. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure
  68. }