scale.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Copyright (c) 2018 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 './color'
  7. import { ColorBrewer, ColorMatplotlib, ColorOther } from './tables'
  8. import { ScaleLegend } from 'mol-theme/color';
  9. import { defaults } from 'mol-util';
  10. export type ColorListName = (
  11. keyof typeof ColorBrewer | keyof typeof ColorMatplotlib | keyof typeof ColorOther
  12. )
  13. export const ColorListNames = [
  14. ...Object.keys(ColorBrewer), ...Object.keys(ColorMatplotlib), ...Object.keys(ColorOther)
  15. ]
  16. export const ColorListOptions = ColorListNames.map(n => [n, n] as [ColorListName, string])
  17. export function getColorListFromName(name: ColorListName) {
  18. if (name in ColorBrewer) {
  19. return ColorBrewer[name as keyof typeof ColorBrewer]
  20. } else if (name in ColorMatplotlib) {
  21. return ColorMatplotlib[name as keyof typeof ColorMatplotlib]
  22. } else if (name in ColorOther) {
  23. return ColorOther[name as keyof typeof ColorOther]
  24. }
  25. console.warn(`unknown color list named '${name}'`)
  26. return ColorBrewer.RdYlBu
  27. }
  28. //
  29. export interface ColorScale {
  30. /** Returns hex color for given value */
  31. color: (value: number) => Color
  32. /** Copies color to rgb int8 array */
  33. colorToArray: (value: number, array: Helpers.NumberArray, offset: number) => void
  34. /** Copies normalized (0 to 1) hex color to rgb array */
  35. normalizedColorToArray: (value: number, array: Helpers.NumberArray, offset: number) => void
  36. /** */
  37. setDomain: (min: number, max: number) => void
  38. /** Legend */
  39. readonly legend: ScaleLegend
  40. }
  41. export const DefaultColorScaleProps = {
  42. domain: [0, 1],
  43. reverse: false,
  44. listOrName: ColorBrewer.RdYlBu as Color[] | ColorListName,
  45. minLabel: '' as string | undefined,
  46. maxLabel: '' as string | undefined,
  47. }
  48. export type ColorScaleProps = Partial<typeof DefaultColorScaleProps>
  49. export namespace ColorScale {
  50. export function create(props: ColorScaleProps): ColorScale {
  51. const { domain, reverse, listOrName } = { ...DefaultColorScaleProps, ...props }
  52. const list = typeof listOrName === 'string' ? getColorListFromName(listOrName) : listOrName
  53. const colors = reverse ? list.slice().reverse() : list
  54. const count1 = colors.length - 1
  55. let diff = 0, min = 0, max = 0
  56. function setDomain(_min: number, _max: number) {
  57. min = _min
  58. max = _max
  59. diff = (max - min) || 1
  60. }
  61. setDomain(domain[0], domain[1])
  62. const minLabel = defaults(props.minLabel, min.toString())
  63. const maxLabel = defaults(props.maxLabel, max.toString())
  64. function color(value: number) {
  65. const t = Math.min(colors.length - 1, Math.max(0, ((value - min) / diff) * count1))
  66. const tf = Math.floor(t)
  67. const c1 = colors[tf]
  68. const c2 = colors[Math.ceil(t)]
  69. return Color.interpolate(c1, c2, t - tf)
  70. }
  71. return {
  72. color,
  73. colorToArray: (value: number, array: Helpers.NumberArray, offset: number) => {
  74. Color.toArray(color(value), array, offset)
  75. },
  76. normalizedColorToArray: (value: number, array: Helpers.NumberArray, offset: number) => {
  77. Color.toArrayNormalized(color(value), array, offset)
  78. },
  79. setDomain,
  80. get legend() { return ScaleLegend(minLabel, maxLabel, colors) }
  81. }
  82. }
  83. }