unit-index.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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, LocationColor } from '../color';
  10. import { ParamDefinition as PD } from 'mol-util/param-definition'
  11. import { ThemeDataContext } from 'mol-theme/theme';
  12. import { ColorListOptions, ColorListName } from 'mol-util/color/scale';
  13. const DefaultColor = Color(0xCCCCCC)
  14. 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.'
  15. export const UnitIndexColorThemeParams = {
  16. list: PD.Select<ColorListName>('RdYlBu', ColorListOptions),
  17. }
  18. export type UnitIndexColorThemeParams = typeof UnitIndexColorThemeParams
  19. export function getUnitIndexColorThemeParams(ctx: ThemeDataContext) {
  20. return UnitIndexColorThemeParams // TODO return copy
  21. }
  22. export function UnitIndexColorTheme(ctx: ThemeDataContext, props: PD.Values<UnitIndexColorThemeParams>): ColorTheme<UnitIndexColorThemeParams> {
  23. let color: LocationColor
  24. const scale = ColorScale.create({ listOrName: props.list, minLabel: 'Start', maxLabel: 'End' })
  25. if (ctx.structure) {
  26. const { units } = ctx.structure
  27. scale.setDomain(0, units.length - 1)
  28. const unitIdColor = new Map<number, Color>()
  29. for (let i = 0, il = units.length; i <il; ++i) {
  30. unitIdColor.set(units[i].id, scale.color(i))
  31. }
  32. color = (location: Location): Color => {
  33. if (StructureElement.isLocation(location)) {
  34. return unitIdColor.get(location.unit.id)!
  35. } else if (Link.isLocation(location)) {
  36. return unitIdColor.get(location.aUnit.id)!
  37. }
  38. return DefaultColor
  39. }
  40. } else {
  41. color = () => DefaultColor
  42. }
  43. return {
  44. factory: UnitIndexColorTheme,
  45. granularity: 'instance',
  46. color,
  47. props,
  48. description: Description,
  49. legend: scale ? scale.legend : undefined
  50. }
  51. }
  52. export const UnitIndexColorThemeProvider: ColorTheme.Provider<UnitIndexColorThemeParams> = {
  53. label: 'Unit Index',
  54. factory: UnitIndexColorTheme,
  55. getParams: getUnitIndexColorThemeParams
  56. }