UNPKG

molstar

Version:

A comprehensive macromolecular library.

84 lines (83 loc) 3.48 kB
/** * Copyright (c) 2019-2021 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> * @author David Sehnal <david.sehnal@gmail.com> */ import { __assign } from "tslib"; import { TableLegend } from '../legend'; import { ParamDefinition as PD } from '../param-definition'; import { distinctColors, DistinctColorsParams } from './distinct'; import { getColorListFromName } from './lists'; import { ColorScale } from './scale'; var DefaultGetPaletteProps = { type: 'generate', colorList: 'red-yellow-blue' }; export function getPaletteParams(props) { if (props === void 0) { props = {}; } var p = __assign(__assign({}, DefaultGetPaletteProps), props); return { palette: PD.MappedStatic(p.type, { colors: PD.Group({ list: PD.ColorList(p.colorList), }, { isFlat: true }), generate: PD.Group(__assign(__assign({}, DistinctColorsParams), { maxCount: PD.Numeric(75, { min: 1, max: 250, step: 1 }) }), { isFlat: true }) }, { options: [ ['colors', 'Color List'], ['generate', 'Generate Distinct'] ] }) }; } var DefaultPaletteProps = PD.getDefaultValues(getPaletteParams()); var DefaultLabelOptions = { valueLabel: function (i) { return "".concat(i + 1); }, minLabel: 'Start', maxLabel: 'End' }; export function getPalette(count, props, labelOptions) { var _a; if (labelOptions === void 0) { labelOptions = {}; } var color; var legend; if (props.palette.name === 'colors' && props.palette.params.list.kind === 'interpolate') { var list = props.palette.params.list; var domain = [0, count - 1]; var _b = __assign(__assign({}, DefaultLabelOptions), labelOptions), minLabel = _b.minLabel, maxLabel = _b.maxLabel; var colors = list.colors; if (colors.length === 0) colors = getColorListFromName(DefaultGetPaletteProps.colorList).list; var scale = ColorScale.create({ listOrName: colors, domain: domain, minLabel: minLabel, maxLabel: maxLabel }); legend = scale.legend; color = scale.color; } else { var colors_1; if (props.palette.name === 'colors') { colors_1 = props.palette.params.list.colors.map(function (c) { return Array.isArray(c) ? c[0] : c; }); if (colors_1.length === 0) colors_1 = getColorListFromName('dark-2').list.map(function (c) { return Array.isArray(c) ? c[0] : c; }); } else { count = Math.min(count, props.palette.params.maxCount); colors_1 = distinctColors(count, props.palette.params); } var valueLabel = (_a = labelOptions.valueLabel) !== null && _a !== void 0 ? _a : DefaultLabelOptions.valueLabel; var colorsLength_1 = colors_1.length; var table = []; for (var i = 0; i < count; ++i) { var j = i % colorsLength_1; if (table[j] === undefined) { table[j] = [valueLabel(i), colors_1[j]]; } else { table[j][0] += ", ".concat(valueLabel(i)); } } legend = TableLegend(table); color = function (i) { return colors_1[i % colorsLength_1]; }; } return { color: color, legend: legend }; }