color.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Color, ColorScale } from '../../../mol-util/color';
  7. import { Location } from '../../../mol-model/location';
  8. import { ParamDefinition as PD } from '../../../mol-util/param-definition'
  9. import { ThemeDataContext } from '../../../mol-theme/theme';
  10. import { ColorListName, ColorListOptionsScale } from '../../../mol-util/color/lists';
  11. import { ColorTheme, LocationColor } from '../../../mol-theme/color';
  12. import { CustomProperty } from '../../common/custom-property';
  13. import { CrossLinkRestraintProvider, CrossLinkRestraint } from './property';
  14. const DefaultColor = Color(0xCCCCCC)
  15. const Description = 'Colors cross-links by the deviation of the observed distance versus the modeled distance (e.g. modeled / `ihm_cross_link_restraint.distance_threshold`).'
  16. export const CrossLinkColorThemeParams = {
  17. domain: PD.Interval([0.5, 1.5], { step: 0.01 }),
  18. list: PD.ColorList<ColorListName>('red-grey', ColorListOptionsScale),
  19. }
  20. export type CrossLinkColorThemeParams = typeof CrossLinkColorThemeParams
  21. export function getCrossLinkColorThemeParams(ctx: ThemeDataContext) {
  22. return CrossLinkColorThemeParams // TODO return copy
  23. }
  24. export function CrossLinkColorTheme(ctx: ThemeDataContext, props: PD.Values<CrossLinkColorThemeParams>): ColorTheme<CrossLinkColorThemeParams> {
  25. let color: LocationColor
  26. let scale: ColorScale | undefined = undefined
  27. const crossLinkRestraints = ctx.structure && CrossLinkRestraintProvider.get(ctx.structure).value
  28. if (crossLinkRestraints) {
  29. scale = ColorScale.create({
  30. domain: props.domain,
  31. listOrName: props.list
  32. })
  33. const scaleColor = scale.color
  34. color = (location: Location): Color => {
  35. if (CrossLinkRestraint.isLocation(location)) {
  36. const pair = crossLinkRestraints.pairs[location.element]
  37. if (pair) {
  38. return scaleColor(CrossLinkRestraint.distance(pair) / pair.distanceThreshold)
  39. }
  40. }
  41. return DefaultColor
  42. }
  43. } else {
  44. color = () => DefaultColor
  45. }
  46. return {
  47. factory: CrossLinkColorTheme,
  48. granularity: 'group',
  49. color,
  50. props,
  51. description: Description,
  52. legend: scale ? scale.legend : undefined
  53. }
  54. }
  55. export const CrossLinkColorThemeProvider: ColorTheme.Provider<CrossLinkColorThemeParams> = {
  56. label: 'Cross Link',
  57. category: ColorTheme.Category.Misc,
  58. factory: CrossLinkColorTheme,
  59. getParams: getCrossLinkColorThemeParams,
  60. defaultValues: PD.getDefaultValues(CrossLinkColorThemeParams),
  61. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure && CrossLinkRestraint.isApplicable(ctx.structure),
  62. ensureCustomProperties: {
  63. attach: (ctx: CustomProperty.Context, data: ThemeDataContext) => data.structure ? CrossLinkRestraintProvider.attach(ctx, data.structure, void 0, true) : Promise.resolve(),
  64. detach: (_, data) => data.structure && data.structure.customPropertyDescriptors.reference(CrossLinkRestraintProvider.descriptor, false)
  65. }
  66. }