unit-index.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 unit (single chain or collection of single elements) a unique color based on the position (index) of the unit in the list of units in the structure.'
  12. export function UnitIndexColorTheme(props: ColorThemeProps): ColorTheme {
  13. let color: LocationColor
  14. let scale: ColorScale | undefined = undefined
  15. if (props.structure) {
  16. const { units } = props.structure
  17. scale = ColorScale.create({ domain: [ 0, units.length - 1 ] })
  18. const unitIdColor = new Map<number, Color>()
  19. for (let i = 0, il = units.length; i <il; ++i) {
  20. unitIdColor.set(units[i].id, scale.color(i))
  21. }
  22. color = (location: Location): Color => {
  23. if (StructureElement.isLocation(location)) {
  24. return unitIdColor.get(location.unit.id)!
  25. } else if (Link.isLocation(location)) {
  26. return unitIdColor.get(location.aUnit.id)!
  27. }
  28. return DefaultColor
  29. }
  30. } else {
  31. color = () => DefaultColor
  32. }
  33. return {
  34. granularity: 'instance',
  35. color,
  36. description: Description,
  37. legend: scale ? scale.legend : undefined
  38. }
  39. }