generate.ts 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Copyright (c) 2020-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 { getPalette } from '../../../mol-util/color/palette';
  10. import { ColorTheme, LocationColor } from '../../../mol-theme/color';
  11. import { ScaleLegend, TableLegend } from '../../../mol-util/legend';
  12. import { StructureElement, Bond, Model } from '../../../mol-model/structure';
  13. import { Location } from '../../../mol-model/location';
  14. import { CellPackInfoProvider } from '../property';
  15. import { distinctColors } from '../../../mol-util/color/distinct';
  16. import { Hcl } from '../../../mol-util/color/spaces/hcl';
  17. const DefaultColor = Color(0xCCCCCC);
  18. const Description = 'Gives every model in a CellPack packing a unique generated color similar to other models in the packing.';
  19. export const CellPackGenerateColorThemeParams = {};
  20. export type CellPackGenerateColorThemeParams = typeof CellPackGenerateColorThemeParams
  21. export function getCellPackGenerateColorThemeParams(ctx: ThemeDataContext) {
  22. return CellPackGenerateColorThemeParams; // TODO return copy
  23. }
  24. export function CellPackGenerateColorTheme(ctx: ThemeDataContext, props: PD.Values<CellPackGenerateColorThemeParams>): ColorTheme<CellPackGenerateColorThemeParams> {
  25. let color: LocationColor;
  26. let legend: ScaleLegend | TableLegend | undefined;
  27. const info = ctx.structure && CellPackInfoProvider.get(ctx.structure).value;
  28. if (ctx.structure && info) {
  29. const colors = distinctColors(info.packingsCount);
  30. let hcl = Hcl.fromColor(Hcl(), colors[info.packingIndex]);
  31. const hue = [Math.max(0, hcl[0] - 35), Math.min(360, hcl[0] + 35)] as [number, number];
  32. const { models } = ctx.structure.root;
  33. let size = 0;
  34. for (const m of models) size = Math.max(size, Model.TrajectoryInfo.get(m).size);
  35. const palette = getPalette(size, { palette: {
  36. name: 'generate',
  37. params: {
  38. hue, chroma: [30, 80], luminance: [15, 85],
  39. clusteringStepCount: 50, minSampleCount: 800, maxCount: 75
  40. }
  41. }}, { minLabel: 'Min', maxLabel: 'Max' });
  42. legend = palette.legend;
  43. const modelColor = new Map<number, Color>();
  44. for (let i = 0, il = models.length; i < il; ++i) {
  45. const idx = Model.TrajectoryInfo.get(models[i]).index;
  46. modelColor.set(Model.TrajectoryInfo.get(models[i]).index, palette.color(idx));
  47. }
  48. color = (location: Location): Color => {
  49. if (StructureElement.Location.is(location)) {
  50. return modelColor.get(Model.TrajectoryInfo.get(location.unit.model).index)!;
  51. } else if (Bond.isLocation(location)) {
  52. return modelColor.get(Model.TrajectoryInfo.get(location.aUnit.model).index)!;
  53. }
  54. return DefaultColor;
  55. };
  56. } else {
  57. color = () => DefaultColor;
  58. }
  59. return {
  60. factory: CellPackGenerateColorTheme,
  61. granularity: 'instance',
  62. color,
  63. props,
  64. description: Description,
  65. legend
  66. };
  67. }
  68. export const CellPackGenerateColorThemeProvider: ColorTheme.Provider<CellPackGenerateColorThemeParams, 'cellpack-generate'> = {
  69. name: 'cellpack-generate',
  70. label: 'CellPack Generate',
  71. category: ColorTheme.Category.Chain,
  72. factory: CellPackGenerateColorTheme,
  73. getParams: getCellPackGenerateColorThemeParams,
  74. defaultValues: PD.getDefaultValues(CellPackGenerateColorThemeParams),
  75. isApplicable: (ctx: ThemeDataContext) => {
  76. return (
  77. !!ctx.structure && ctx.structure.elementCount > 0 &&
  78. !!CellPackInfoProvider.get(ctx.structure).value
  79. );
  80. }
  81. };