color.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * Copyright (c) 2018-2021 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 { ColorType } from '../mol-geo/geometry/color-data';
  9. import { CarbohydrateSymbolColorThemeProvider } from './color/carbohydrate-symbol';
  10. import { UniformColorThemeProvider } from './color/uniform';
  11. import { deepEqual } from '../mol-util';
  12. import { ParamDefinition as PD } from '../mol-util/param-definition';
  13. import { ThemeDataContext, ThemeRegistry, ThemeProvider } from './theme';
  14. import { ChainIdColorThemeProvider } from './color/chain-id';
  15. import { ElementIndexColorThemeProvider } from './color/element-index';
  16. import { ElementSymbolColorThemeProvider } from './color/element-symbol';
  17. import { MoleculeTypeColorThemeProvider } from './color/molecule-type';
  18. import { PolymerIdColorThemeProvider } from './color/polymer-id';
  19. import { PolymerIndexColorThemeProvider } from './color/polymer-index';
  20. import { ResidueNameColorThemeProvider } from './color/residue-name';
  21. import { SecondaryStructureColorThemeProvider } from './color/secondary-structure';
  22. import { SequenceIdColorThemeProvider } from './color/sequence-id';
  23. import { ShapeGroupColorThemeProvider } from './color/shape-group';
  24. import { UnitIndexColorThemeProvider } from './color/unit-index';
  25. import { ScaleLegend, TableLegend } from '../mol-util/legend';
  26. import { UncertaintyColorThemeProvider } from './color/uncertainty';
  27. import { EntitySourceColorThemeProvider } from './color/entity-source';
  28. import { IllustrativeColorThemeProvider } from './color/illustrative';
  29. import { HydrophobicityColorThemeProvider } from './color/hydrophobicity';
  30. import { ModelIndexColorThemeProvider } from './color/model-index';
  31. import { OccupancyColorThemeProvider } from './color/occupancy';
  32. import { OperatorNameColorThemeProvider } from './color/operator-name';
  33. import { OperatorHklColorThemeProvider } from './color/operator-hkl';
  34. import { PartialChargeColorThemeProvider } from './color/partial-charge';
  35. import { AtomIdColorThemeProvider } from './color/atom-id';
  36. import { EntityIdColorThemeProvider } from './color/entity-id';
  37. export type LocationColor = (location: Location, isSecondary: boolean) => Color
  38. export { ColorTheme };
  39. interface ColorTheme<P extends PD.Params> {
  40. readonly factory: ColorTheme.Factory<P>
  41. readonly granularity: ColorType
  42. readonly color: LocationColor
  43. readonly props: Readonly<PD.Values<P>>
  44. // if palette is defined, 24bit RGB color value normalized to interval [0, 1]
  45. // is used as index to the colors
  46. readonly palette?: Readonly<ColorTheme.Palette>
  47. readonly contextHash?: number
  48. readonly description?: string
  49. readonly legend?: Readonly<ScaleLegend | TableLegend>
  50. }
  51. namespace ColorTheme {
  52. export const enum Category {
  53. Atom = 'Atom Property',
  54. Chain = 'Chain Property',
  55. Residue = 'Residue Property',
  56. Symmetry = 'Symmetry',
  57. Validation = 'Validation',
  58. Misc = 'Miscellaneous',
  59. }
  60. export interface Palette {
  61. colors: Color[]
  62. }
  63. export const PaletteScale = (1 << 24) - 1;
  64. export type Props = { [k: string]: any }
  65. export type Factory<P extends PD.Params> = (ctx: ThemeDataContext, props: PD.Values<P>) => ColorTheme<P>
  66. export const EmptyFactory = () => Empty;
  67. const EmptyColor = Color(0xCCCCCC);
  68. export const Empty: ColorTheme<{}> = {
  69. factory: EmptyFactory,
  70. granularity: 'uniform',
  71. color: () => EmptyColor,
  72. props: {}
  73. };
  74. export function areEqual(themeA: ColorTheme<any>, themeB: ColorTheme<any>) {
  75. return themeA.contextHash === themeB.contextHash && themeA.factory === themeB.factory && deepEqual(themeA.props, themeB.props);
  76. }
  77. export interface Provider<P extends PD.Params = any, Id extends string = string> extends ThemeProvider<ColorTheme<P>, P, Id> { }
  78. export const EmptyProvider: Provider<{}> = { name: '', label: '', category: '', factory: EmptyFactory, getParams: () => ({}), defaultValues: {}, isApplicable: () => true };
  79. export type Registry = ThemeRegistry<ColorTheme<any>>
  80. export function createRegistry() {
  81. return new ThemeRegistry(BuiltIn as { [k: string]: Provider<any> }, EmptyProvider);
  82. }
  83. export const BuiltIn = {
  84. 'atom-id': AtomIdColorThemeProvider,
  85. 'carbohydrate-symbol': CarbohydrateSymbolColorThemeProvider,
  86. 'chain-id': ChainIdColorThemeProvider,
  87. 'element-index': ElementIndexColorThemeProvider,
  88. 'element-symbol': ElementSymbolColorThemeProvider,
  89. 'entity-id': EntityIdColorThemeProvider,
  90. 'entity-source': EntitySourceColorThemeProvider,
  91. 'hydrophobicity': HydrophobicityColorThemeProvider,
  92. 'illustrative': IllustrativeColorThemeProvider,
  93. 'model-index': ModelIndexColorThemeProvider,
  94. 'molecule-type': MoleculeTypeColorThemeProvider,
  95. 'occupancy': OccupancyColorThemeProvider,
  96. 'operator-hkl': OperatorHklColorThemeProvider,
  97. 'operator-name': OperatorNameColorThemeProvider,
  98. 'partial-charge': PartialChargeColorThemeProvider,
  99. 'polymer-id': PolymerIdColorThemeProvider,
  100. 'polymer-index': PolymerIndexColorThemeProvider,
  101. 'residue-name': ResidueNameColorThemeProvider,
  102. 'secondary-structure': SecondaryStructureColorThemeProvider,
  103. 'sequence-id': SequenceIdColorThemeProvider,
  104. 'shape-group': ShapeGroupColorThemeProvider,
  105. 'uncertainty': UncertaintyColorThemeProvider,
  106. 'unit-index': UnitIndexColorThemeProvider,
  107. 'uniform': UniformColorThemeProvider,
  108. };
  109. type _BuiltIn = typeof BuiltIn
  110. export type BuiltIn = keyof _BuiltIn
  111. export type ParamValues<C extends ColorTheme.Provider<any>> = C extends ColorTheme.Provider<infer P> ? PD.Values<P> : never
  112. export type BuiltInParams<T extends BuiltIn> = Partial<ParamValues<_BuiltIn[T]>>
  113. }
  114. export function ColorThemeProvider<P extends PD.Params, Id extends string>(p: ColorTheme.Provider<P, Id>): ColorTheme.Provider<P, Id> { return p; }