scale.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Color, ColorListEntry } from './color';
  7. import { getColorListFromName, ColorListName } from './lists';
  8. import { defaults } from '../../mol-util';
  9. import { NumberArray } from '../../mol-util/type-helpers';
  10. import { ScaleLegend } from '../legend';
  11. import { SortedArray } from '../../mol-data/int';
  12. import { clamp } from '../../mol-math/interpolate';
  13. export interface ColorScale {
  14. /** Returns hex color for given value */
  15. color: (value: number) => Color
  16. /** Copies color to rgb int8 array */
  17. colorToArray: (value: number, array: NumberArray, offset: number) => void
  18. /** Copies normalized (0 to 1) hex color to rgb array */
  19. normalizedColorToArray: (value: number, array: NumberArray, offset: number) => void
  20. /** */
  21. setDomain: (min: number, max: number) => void
  22. /** Legend */
  23. readonly legend: ScaleLegend
  24. }
  25. export const DefaultColorScaleProps = {
  26. domain: [0, 1] as [number, number],
  27. reverse: false,
  28. listOrName: 'red-yellow-blue' as ColorListEntry[] | ColorListName,
  29. minLabel: '' as string | undefined,
  30. maxLabel: '' as string | undefined,
  31. };
  32. export type ColorScaleProps = Partial<typeof DefaultColorScaleProps>
  33. export namespace ColorScale {
  34. export function create(props: ColorScaleProps): ColorScale {
  35. const { domain, reverse, listOrName } = { ...DefaultColorScaleProps, ...props };
  36. const list = typeof listOrName === 'string' ? getColorListFromName(listOrName).list : listOrName;
  37. const colors = reverse ? list.slice().reverse() : list;
  38. const count1 = colors.length - 1;
  39. let diff = 0, min = 0, max = 0;
  40. function setDomain(_min: number, _max: number) {
  41. min = _min;
  42. max = _max;
  43. diff = (max - min) || 1;
  44. }
  45. setDomain(domain[0], domain[1]);
  46. const minLabel = defaults(props.minLabel, min.toString());
  47. const maxLabel = defaults(props.maxLabel, max.toString());
  48. let color: (v: number) => Color;
  49. const hasOffsets = colors.every(c => Array.isArray(c));
  50. if (hasOffsets) {
  51. const sorted = [...colors] as [Color, number][];
  52. sorted.sort((a, b) => a[1] - b[1]);
  53. const src = sorted.map(c => c[0]);
  54. const off = SortedArray.ofSortedArray(sorted.map(c => c[1]));
  55. const max = src.length - 1;
  56. color = (v: number) => {
  57. const t = clamp((v - min) / diff, 0, 1);
  58. const i = SortedArray.findPredecessorIndex(off, t);
  59. if (i === 0) {
  60. return src[min];
  61. } else if (i > max) {
  62. return src[max];
  63. }
  64. const o1 = off[i - 1], o2 = off[i];
  65. const t1 = clamp((t - o1) / (o2 - o1), 0, 1); // TODO: cache the deltas?
  66. return Color.interpolate(src[i - 1], src[i], t1);
  67. };
  68. } else {
  69. color = (value: number) => {
  70. const t = Math.min(colors.length - 1, Math.max(0, ((value - min) / diff) * count1));
  71. const tf = Math.floor(t);
  72. const c1 = colors[tf] as Color;
  73. const c2 = colors[Math.ceil(t)] as Color;
  74. return Color.interpolate(c1, c2, t - tf);
  75. };
  76. }
  77. return {
  78. color,
  79. colorToArray: (value: number, array: NumberArray, offset: number) => {
  80. Color.toArray(color(value), array, offset);
  81. },
  82. normalizedColorToArray: (value: number, array: NumberArray, offset: number) => {
  83. Color.toArrayNormalized(color(value), array, offset);
  84. },
  85. setDomain,
  86. get legend() { return ScaleLegend(minLabel, maxLabel, colors); }
  87. };
  88. }
  89. }