custom-theme.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { isPositionLocation } from '../../mol-geo/util/location-iterator';
  2. import { Vec3 } from '../../mol-math/linear-algebra';
  3. import { ColorTheme } from '../../mol-theme/color';
  4. import { ThemeDataContext } from '../../mol-theme/theme';
  5. import { Color } from '../../mol-util/color';
  6. import { ColorNames } from '../../mol-util/color/names';
  7. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  8. export function CustomColorTheme(
  9. ctx: ThemeDataContext,
  10. props: PD.Values<{}>
  11. ): ColorTheme<{}> {
  12. const { radius, center } = ctx.structure?.boundary.sphere!;
  13. const radiusSq = Math.max(radius * radius, 0.001);
  14. const scale = ColorTheme.PaletteScale;
  15. return {
  16. factory: CustomColorTheme,
  17. granularity: 'vertex',
  18. color: location => {
  19. if (!isPositionLocation(location)) return ColorNames.black;
  20. const dist = Vec3.squaredDistance(location.position, center);
  21. const t = Math.min(dist / radiusSq, 1);
  22. return ((t * scale) | 0) as Color;
  23. },
  24. palette: {
  25. filter: 'nearest',
  26. colors: [
  27. ColorNames.red,
  28. ColorNames.pink,
  29. ColorNames.violet,
  30. ColorNames.orange,
  31. ColorNames.yellow,
  32. ColorNames.green,
  33. ColorNames.blue
  34. ]
  35. },
  36. props: props,
  37. description: '',
  38. };
  39. }
  40. export const CustomColorThemeProvider: ColorTheme.Provider<{}, 'basic-wrapper-custom-color-theme'> = {
  41. name: 'basic-wrapper-custom-color-theme',
  42. label: 'Custom Color Theme',
  43. category: ColorTheme.Category.Misc,
  44. factory: CustomColorTheme,
  45. getParams: () => ({}),
  46. defaultValues: { },
  47. isApplicable: (ctx: ThemeDataContext) => true,
  48. };