scale.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 } from './color'
  7. import { getColorListFromName, ColorListName } from './lists'
  8. import { defaults } from '../../mol-util';
  9. import { NumberArray } from '../../mol-util/type-helpers';
  10. export interface ScaleLegend {
  11. kind: 'scale-legend'
  12. minLabel: string,
  13. maxLabel: string,
  14. colors: Color[]
  15. }
  16. export function ScaleLegend(minLabel: string, maxLabel: string, colors: Color[]): ScaleLegend {
  17. return { kind: 'scale-legend', minLabel, maxLabel, colors }
  18. }
  19. export interface ColorScale {
  20. /** Returns hex color for given value */
  21. color: (value: number) => Color
  22. /** Copies color to rgb int8 array */
  23. colorToArray: (value: number, array: NumberArray, offset: number) => void
  24. /** Copies normalized (0 to 1) hex color to rgb array */
  25. normalizedColorToArray: (value: number, array: NumberArray, offset: number) => void
  26. /** */
  27. setDomain: (min: number, max: number) => void
  28. /** Legend */
  29. readonly legend: ScaleLegend
  30. }
  31. export const DefaultColorScaleProps = {
  32. domain: [0, 1] as [number, number],
  33. reverse: false,
  34. listOrName: 'red-yellow-blue' as Color[] | ColorListName,
  35. minLabel: '' as string | undefined,
  36. maxLabel: '' as string | undefined,
  37. }
  38. export type ColorScaleProps = Partial<typeof DefaultColorScaleProps>
  39. export namespace ColorScale {
  40. export function create(props: ColorScaleProps): ColorScale {
  41. const { domain, reverse, listOrName } = { ...DefaultColorScaleProps, ...props }
  42. const list = typeof listOrName === 'string' ? getColorListFromName(listOrName) : listOrName
  43. const colors = reverse ? list.slice().reverse() : list
  44. const count1 = colors.length - 1
  45. let diff = 0, min = 0, max = 0
  46. function setDomain(_min: number, _max: number) {
  47. min = _min
  48. max = _max
  49. diff = (max - min) || 1
  50. }
  51. setDomain(domain[0], domain[1])
  52. const minLabel = defaults(props.minLabel, min.toString())
  53. const maxLabel = defaults(props.maxLabel, max.toString())
  54. function color(value: number) {
  55. const t = Math.min(colors.length - 1, Math.max(0, ((value - min) / diff) * count1))
  56. const tf = Math.floor(t)
  57. const c1 = colors[tf]
  58. const c2 = colors[Math.ceil(t)]
  59. return Color.interpolate(c1, c2, t - tf)
  60. }
  61. return {
  62. color,
  63. colorToArray: (value: number, array: NumberArray, offset: number) => {
  64. Color.toArray(color(value), array, offset)
  65. },
  66. normalizedColorToArray: (value: number, array: NumberArray, offset: number) => {
  67. Color.toArrayNormalized(color(value), array, offset)
  68. },
  69. setDomain,
  70. get legend() { return ScaleLegend(minLabel, maxLabel, colors) }
  71. }
  72. }
  73. }