trajectory-index.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Copyright (c) 2019-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 } from '../../mol-util/color';
  7. import { Location } from '../../mol-model/location';
  8. import { StructureElement, Bond, Model } from '../../mol-model/structure';
  9. import { ColorTheme, LocationColor } from '../color';
  10. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  11. import { ThemeDataContext } from '../theme';
  12. import { getPaletteParams, getPalette } from '../../mol-util/color/palette';
  13. import { TableLegend, ScaleLegend } from '../../mol-util/legend';
  14. const DefaultColor = Color(0xCCCCCC);
  15. const Description = 'Gives every model (frame) a unique color based on the index in its trajectory.';
  16. export const TrajectoryIndexColorThemeParams = {
  17. ...getPaletteParams({ type: 'colors', colorList: 'purples' }),
  18. };
  19. export type TrajectoryIndexColorThemeParams = typeof TrajectoryIndexColorThemeParams
  20. export function getTrajectoryIndexColorThemeParams(ctx: ThemeDataContext) {
  21. return PD.clone(TrajectoryIndexColorThemeParams);
  22. }
  23. export function TrajectoryIndexColorTheme(ctx: ThemeDataContext, props: PD.Values<TrajectoryIndexColorThemeParams>): ColorTheme<TrajectoryIndexColorThemeParams> {
  24. let color: LocationColor;
  25. let legend: ScaleLegend | TableLegend | undefined;
  26. if (ctx.structure) {
  27. const { models } = ctx.structure.root;
  28. let size = 0;
  29. for (const m of models) size = Math.max(size, Model.TrajectoryInfo.get(m)?.size || 0);
  30. const palette = getPalette(size, props);
  31. legend = palette.legend;
  32. const modelColor = new Map<number, Color>();
  33. for (let i = 0, il = models.length; i < il; ++i) {
  34. const idx = Model.TrajectoryInfo.get(models[i])?.index || 0;
  35. modelColor.set(idx, palette.color(idx));
  36. }
  37. color = (location: Location): Color => {
  38. if (StructureElement.Location.is(location)) {
  39. return modelColor.get(Model.TrajectoryInfo.get(location.unit.model).index)!;
  40. } else if (Bond.isLocation(location)) {
  41. return modelColor.get(Model.TrajectoryInfo.get(location.aUnit.model).index)!;
  42. }
  43. return DefaultColor;
  44. };
  45. } else {
  46. color = () => DefaultColor;
  47. }
  48. return {
  49. factory: TrajectoryIndexColorTheme,
  50. granularity: 'instance',
  51. color,
  52. props,
  53. description: Description,
  54. legend
  55. };
  56. }
  57. export const TrajectoryIndexColorThemeProvider: ColorTheme.Provider<TrajectoryIndexColorThemeParams, 'trajectory-index'> = {
  58. name: 'trajectory-index',
  59. label: 'Trajectory Index',
  60. category: ColorTheme.Category.Chain,
  61. factory: TrajectoryIndexColorTheme,
  62. getParams: getTrajectoryIndexColorThemeParams,
  63. defaultValues: PD.getDefaultValues(TrajectoryIndexColorThemeParams),
  64. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure && ctx.structure.elementCount > 0 && Model.TrajectoryInfo.get(ctx.structure.models[0]).size > 1
  65. };