polymer-index.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { 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 { ScaleLegend } from '../../mol-util/color/scale';
  13. import { TableLegend } from '../../mol-util/color/lists';
  14. import { getPaletteParams, getPalette } from '../../mol-util/color/palette';
  15. const DefaultColor = Color(0xCCCCCC)
  16. const Description = 'Gives every polymer a unique color based on the position (index) of the polymer in the list of polymers in the structure.'
  17. export const PolymerIndexColorThemeParams = {
  18. ...getPaletteParams({ scaleList: 'red-yellow-blue' }),
  19. }
  20. export type PolymerIndexColorThemeParams = typeof PolymerIndexColorThemeParams
  21. export function getPolymerIndexColorThemeParams(ctx: ThemeDataContext) {
  22. return PolymerIndexColorThemeParams // TODO return copy
  23. }
  24. export type PolymerIndexColorThemeProps = PD.Values<typeof PolymerIndexColorThemeParams>
  25. export function PolymerIndexColorTheme(ctx: ThemeDataContext, props: PD.Values<PolymerIndexColorThemeParams>): ColorTheme<PolymerIndexColorThemeParams> {
  26. let color: LocationColor
  27. let legend: ScaleLegend | TableLegend | undefined
  28. if (ctx.structure) {
  29. const { units } = ctx.structure.root
  30. let polymerCount = 0
  31. for (let i = 0, il = units.length; i <il; ++i) {
  32. if (units[i].polymerElements.length > 0) ++polymerCount
  33. }
  34. const palette = getPalette(polymerCount, props)
  35. legend = palette.legend
  36. const unitIdColor = new Map<number, Color>()
  37. for (let i = 0, j = 0, il = units.length; i <il; ++i) {
  38. if (units[i].polymerElements.length > 0) {
  39. unitIdColor.set(units[i].id, palette.color(j))
  40. ++j
  41. }
  42. }
  43. color = (location: Location): Color => {
  44. let color: Color | undefined
  45. if (StructureElement.isLocation(location)) {
  46. color = unitIdColor.get(location.unit.id)
  47. } else if (Link.isLocation(location)) {
  48. color = unitIdColor.get(location.aUnit.id)
  49. }
  50. return color !== undefined ? color : DefaultColor
  51. }
  52. } else {
  53. color = () => DefaultColor
  54. }
  55. return {
  56. factory: PolymerIndexColorTheme,
  57. granularity: 'instance',
  58. color,
  59. props,
  60. description: Description,
  61. legend
  62. }
  63. }
  64. export const PolymerIndexColorThemeProvider: ColorTheme.Provider<PolymerIndexColorThemeParams> = {
  65. label: 'Polymer Index',
  66. factory: PolymerIndexColorTheme,
  67. getParams: getPolymerIndexColorThemeParams,
  68. defaultValues: PD.getDefaultValues(PolymerIndexColorThemeParams),
  69. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure
  70. }