illustrative.ts 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ElementSymbol, isNucleic, isProtein, MoleculeType } from '../../mol-model/structure/model/types';
  7. import { Color } from '../../mol-util/color';
  8. import { StructureElement, Unit, Bond } from '../../mol-model/structure';
  9. import { Location } from '../../mol-model/location';
  10. import { ColorTheme } from '../color';
  11. import { ParamDefinition as PD } from '../../mol-util/param-definition'
  12. import { ThemeDataContext } from '../theme';
  13. import { elementSymbolColor, ElementSymbolColors } from './element-symbol';
  14. import { getAdjustedColorMap } from '../../mol-util/color/color';
  15. const DefaultIllustrativeColor = Color(0xFFFFFF)
  16. const Description = `Assigns an illustrative color similar to David Goodsell's Molecule of the Month style.`
  17. export const IllustrativeColorThemeParams = {}
  18. export type IllustrativeColorThemeParams = typeof IllustrativeColorThemeParams
  19. export function getIllustrativeColorThemeParams(ctx: ThemeDataContext) {
  20. return IllustrativeColorThemeParams // TODO return copy
  21. }
  22. export function illustrativeColor(colorMap: ElementSymbolColors, typeSymbol: ElementSymbol, moleculeType: MoleculeType) {
  23. if (isNucleic(moleculeType)) {
  24. if (typeSymbol === 'O') {
  25. return Color(0xFF8C8C)
  26. } else if (typeSymbol === 'P') {
  27. return Color(0xFF7D7D)
  28. } else {
  29. return Color(0xFFA6A6)
  30. }
  31. } else if (isProtein(moleculeType)) {
  32. if (typeSymbol === 'C') {
  33. return Color(0x7FB2FF)
  34. } else {
  35. return Color(0x6699FF)
  36. }
  37. } else {
  38. return elementSymbolColor(colorMap, typeSymbol)
  39. }
  40. }
  41. export function IllustrativeColorTheme(ctx: ThemeDataContext, props: PD.Values<IllustrativeColorThemeParams>): ColorTheme<IllustrativeColorThemeParams> {
  42. const colorMap = getAdjustedColorMap(ElementSymbolColors, 0, 0.7)
  43. function color(location: Location): Color {
  44. if (StructureElement.Location.is(location)) {
  45. if (Unit.isAtomic(location.unit)) {
  46. const moleculeType = location.unit.model.atomicHierarchy.derived.residue.moleculeType[location.unit.residueIndex[location.element]]
  47. const typeSymbol = location.unit.model.atomicHierarchy.atoms.type_symbol.value(location.element)
  48. return illustrativeColor(colorMap, typeSymbol, moleculeType)
  49. }
  50. } else if (Bond.isLocation(location)) {
  51. if (Unit.isAtomic(location.aUnit)) {
  52. const elementIndex = location.aUnit.elements[location.aIndex]
  53. const moleculeType = location.aUnit.model.atomicHierarchy.derived.residue.moleculeType[location.aUnit.residueIndex[elementIndex]]
  54. const typeSymbol = location.aUnit.model.atomicHierarchy.atoms.type_symbol.value(elementIndex)
  55. return illustrativeColor(colorMap, typeSymbol, moleculeType)
  56. }
  57. }
  58. return DefaultIllustrativeColor
  59. }
  60. return {
  61. factory: IllustrativeColorTheme,
  62. granularity: 'group',
  63. color,
  64. props,
  65. description: Description,
  66. // TODO add legend
  67. }
  68. }
  69. export const IllustrativeColorThemeProvider: ColorTheme.Provider<IllustrativeColorThemeParams, 'illustrative'> = {
  70. name: 'illustrative',
  71. label: 'Illustrative',
  72. category: ColorTheme.Category.Misc,
  73. factory: IllustrativeColorTheme,
  74. getParams: getIllustrativeColorThemeParams,
  75. defaultValues: PD.getDefaultValues(IllustrativeColorThemeParams),
  76. isApplicable: (ctx: ThemeDataContext) => !!ctx.structure
  77. }