provided.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Copyright (c) 2019-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ThemeDataContext } from '../../../mol-theme/theme';
  7. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  8. import { Color } from '../../../mol-util/color';
  9. import { ColorTheme, LocationColor } from '../../../mol-theme/color';
  10. import { ScaleLegend, TableLegend } from '../../../mol-util/legend';
  11. import { StructureElement, Model, Bond } from '../../../mol-model/structure';
  12. import { Location } from '../../../mol-model/location';
  13. import { CellPackInfoProvider } from '../property';
  14. const DefaultColor = Color(0xCCCCCC);
  15. const Description = 'Gives every model in a CellPack the color provied in the packing data.';
  16. export const CellPackProvidedColorThemeParams = {};
  17. export type CellPackProvidedColorThemeParams = typeof CellPackProvidedColorThemeParams
  18. export function getCellPackProvidedColorThemeParams(ctx: ThemeDataContext) {
  19. return CellPackProvidedColorThemeParams; // TODO return copy
  20. }
  21. export function CellPackProvidedColorTheme(ctx: ThemeDataContext, props: PD.Values<CellPackProvidedColorThemeParams>): ColorTheme<CellPackProvidedColorThemeParams> {
  22. let color: LocationColor;
  23. let legend: ScaleLegend | TableLegend | undefined;
  24. const info = ctx.structure && CellPackInfoProvider.get(ctx.structure).value;
  25. if (ctx.structure && info?.colors) {
  26. const { models } = ctx.structure.root;
  27. const modelColor = new Map<number, Color>();
  28. for (let i = 0, il = models.length; i < il; ++i) {
  29. const idx = Model.TrajectoryInfo.get(models[i]).index;
  30. modelColor.set(Model.TrajectoryInfo.get(models[i]).index, info.colors[idx]);
  31. }
  32. color = (location: Location): Color => {
  33. if (StructureElement.Location.is(location)) {
  34. return modelColor.get(Model.TrajectoryInfo.get(location.unit.model).index)!;
  35. } else if (Bond.isLocation(location)) {
  36. return modelColor.get(Model.TrajectoryInfo.get(location.aUnit.model).index)!;
  37. }
  38. return DefaultColor;
  39. };
  40. } else {
  41. color = () => DefaultColor;
  42. }
  43. return {
  44. factory: CellPackProvidedColorTheme,
  45. granularity: 'instance',
  46. color,
  47. props,
  48. description: Description,
  49. legend
  50. };
  51. }
  52. export const CellPackProvidedColorThemeProvider: ColorTheme.Provider<CellPackProvidedColorThemeParams, 'cellpack-provided'> = {
  53. name: 'cellpack-provided',
  54. label: 'CellPack Provided',
  55. category: ColorTheme.Category.Chain,
  56. factory: CellPackProvidedColorTheme,
  57. getParams: getCellPackProvidedColorThemeParams,
  58. defaultValues: PD.getDefaultValues(CellPackProvidedColorThemeParams),
  59. isApplicable: (ctx: ThemeDataContext) => {
  60. return (
  61. !!ctx.structure && ctx.structure.elementCount > 0 &&
  62. Model.TrajectoryInfo.get(ctx.structure.models[0]).size > 1 &&
  63. !!CellPackInfoProvider.get(ctx.structure).value?.colors
  64. );
  65. }
  66. };