volume-value.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * Copyright (c) 2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ColorTheme } from '../color';
  7. import { Color, ColorScale } from '../../mol-util/color';
  8. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  9. import { ThemeDataContext } from '../theme';
  10. import { ColorNames } from '../../mol-util/color/names';
  11. const DefaultColor = Color(0xCCCCCC);
  12. const Description = 'Assign color based on the given value of a volume cell.';
  13. export const VolumeValueColorThemeParams = {
  14. colorList: PD.ColorList({
  15. kind: 'interpolate',
  16. colors: [
  17. [ColorNames.white, 0],
  18. [ColorNames.red, 0.25],
  19. [ColorNames.white, 0.5],
  20. [ColorNames.blue, 0.75],
  21. [ColorNames.white, 1]
  22. ]
  23. }, { offsets: true, isEssential: true }),
  24. };
  25. export type VolumeValueColorThemeParams = typeof VolumeValueColorThemeParams
  26. export function getVolumeValueColorThemeParams(ctx: ThemeDataContext) {
  27. return VolumeValueColorThemeParams; // TODO return copy
  28. }
  29. export function VolumeValueColorTheme(ctx: ThemeDataContext, props: PD.Values<VolumeValueColorThemeParams>): ColorTheme<VolumeValueColorThemeParams> {
  30. const scale = ColorScale.create({ domain: [0, 1], listOrName: props.colorList.colors });
  31. const colors: Color[] = [];
  32. for (let i = 0; i < 256; ++i) {
  33. colors[i] = scale.color(i / 255);
  34. }
  35. const palette: ColorTheme.Palette = { colors, filter: 'linear' };
  36. return {
  37. factory: VolumeValueColorTheme,
  38. granularity: 'direct',
  39. color: () => DefaultColor,
  40. props: props,
  41. description: Description,
  42. legend: scale.legend,
  43. palette,
  44. };
  45. }
  46. export const VolumeValueColorThemeProvider: ColorTheme.Provider<VolumeValueColorThemeParams, 'volume-value'> = {
  47. name: 'volume-value',
  48. label: 'Volume Value',
  49. category: ColorTheme.Category.Misc,
  50. factory: VolumeValueColorTheme,
  51. getParams: getVolumeValueColorThemeParams,
  52. defaultValues: PD.getDefaultValues(VolumeValueColorThemeParams),
  53. isApplicable: (ctx: ThemeDataContext) => !!ctx.volume,
  54. };