element-index.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.Select<ColorListName>('Color Scale', '', 'RdYlBu', ColorListOptions),
  18. }
  19. export function getElementIndexColorThemeParams(ctx: ThemeDataContext) {
  20. return ElementIndexColorThemeParams // TODO return copy
  21. }
  22. export type ElementIndexColorThemeProps = PD.DefaultValues<typeof ElementIndexColorThemeParams>
  23. export function ElementIndexColorTheme(ctx: ThemeDataContext, props: ElementIndexColorThemeProps): ColorTheme<ElementIndexColorThemeProps> {
  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. granularity: 'groupInstance',
  55. color,
  56. props,
  57. description: Description,
  58. legend: scale ? scale.legend : undefined
  59. }
  60. }
  61. export const ElementIndexColorThemeProvider: ColorTheme.Provider<typeof ElementIndexColorThemeParams> = {
  62. factory: ElementIndexColorTheme, params: getElementIndexColorThemeParams
  63. }