unit-index.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  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 { Unit, StructureElement, Link } from 'mol-model/structure';
  9. import { ColorTheme, ColorThemeProps, LocationColor } from '../color';
  10. const DefaultColor = Color(0xCCCCCC)
  11. export function UnitIndexColorTheme(props: ColorThemeProps): ColorTheme {
  12. let color: LocationColor
  13. if (props.structure) {
  14. const { units } = props.structure
  15. const scale = ColorScale.create({ domain: [ 0, units.length - 1 ] })
  16. color = (location: Location): Color => {
  17. if (StructureElement.isLocation(location)) {
  18. return scale.color(Unit.findUnitById(location.unit.id, units))
  19. } else if (Link.isLocation(location)) {
  20. return scale.color(Unit.findUnitById(location.aUnit.id, units))
  21. }
  22. return DefaultColor
  23. }
  24. } else {
  25. color = () => DefaultColor
  26. }
  27. return { kind: 'instance', color }
  28. }