color.ts 3.1 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 { ColorTheme, LocationColor } from '../../../mol-theme/color';
  11. import { CustomProperty } from '../../common/custom-property';
  12. import { CrossLinkRestraintProvider, CrossLinkRestraint } from './property';
  13. const DefaultColor = Color(0xCCCCCC)
  14. 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`).'
  15. export const CrossLinkColorThemeParams = {
  16. domain: PD.Interval([0.5, 1.5], { step: 0.01 }),
  17. list: PD.ColorList('red-grey', { presetKind: 'scale' }),
  18. }
  19. export type CrossLinkColorThemeParams = typeof CrossLinkColorThemeParams
  20. export function getCrossLinkColorThemeParams(ctx: ThemeDataContext) {
  21. return CrossLinkColorThemeParams // TODO return copy
  22. }
  23. export function CrossLinkColorTheme(ctx: ThemeDataContext, props: PD.Values<CrossLinkColorThemeParams>): ColorTheme<CrossLinkColorThemeParams> {
  24. let color: LocationColor
  25. let scale: ColorScale | undefined = undefined
  26. const crossLinkRestraints = ctx.structure && CrossLinkRestraintProvider.get(ctx.structure).value
  27. if (crossLinkRestraints) {
  28. scale = ColorScale.create({
  29. domain: props.domain,
  30. listOrName: props.list.colors
  31. })
  32. const scaleColor = scale.color
  33. color = (location: Location): Color => {
  34. if (CrossLinkRestraint.isLocation(location)) {
  35. const pair = crossLinkRestraints.pairs[location.element]
  36. if (pair) {
  37. return scaleColor(CrossLinkRestraint.distance(pair) / pair.distanceThreshold)
  38. }
  39. }
  40. return DefaultColor
  41. }
  42. } else {
  43. color = () => DefaultColor
  44. }
  45. return {
  46. factory: CrossLinkColorTheme,
  47. granularity: 'group',
  48. color,
  49. props,
  50. description: Description,
  51. legend: scale ? scale.legend : undefined
  52. }
  53. }
  54. export const CrossLinkColorThemeProvider: ColorTheme.Provider<CrossLinkColorThemeParams, 'cross-link'> = {
  55. name: 'cross-link',
  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. }