polymer-index.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 { ColorTheme, ColorThemeProps, LocationColor } from '../color';
  10. const DefaultColor = Color(0xCCCCCC)
  11. const Description = 'Gives every polymer a unique color based on the position (index) of the polymer in the list of polymers in the structure.'
  12. export function PolymerIndexColorTheme(props: ColorThemeProps): ColorTheme {
  13. let color: LocationColor
  14. let scale: ColorScale | undefined = undefined
  15. if (props.structure) {
  16. const { units } = props.structure
  17. let polymerCount = 0
  18. for (let i = 0, il = units.length; i <il; ++i) {
  19. if (units[i].polymerElements.length > 0) ++polymerCount
  20. }
  21. scale = ColorScale.create({ list: props.list, domain: [ 0, polymerCount - 1 ] })
  22. const unitIdColor = new Map<number, Color>()
  23. for (let i = 0, j = 0, il = units.length; i <il; ++i) {
  24. if (units[i].polymerElements.length > 0) {
  25. unitIdColor.set(units[i].id, scale.color(j))
  26. ++j
  27. }
  28. }
  29. color = (location: Location): Color => {
  30. let color: Color | undefined
  31. if (StructureElement.isLocation(location)) {
  32. color = unitIdColor.get(location.unit.id)
  33. } else if (Link.isLocation(location)) {
  34. color = unitIdColor.get(location.aUnit.id)
  35. }
  36. return color !== undefined ? color : DefaultColor
  37. }
  38. } else {
  39. color = () => DefaultColor
  40. }
  41. return {
  42. features: { structure: true, list: true },
  43. granularity: 'instance',
  44. color,
  45. description: Description,
  46. legend: scale ? scale.legend : undefined
  47. }
  48. }