/** * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal * @author Alexander Rose */ import { Color } from '../../mol-util/color'; import { ParamDefinition as PD } from '../../mol-util/param-definition'; import { camelCaseToWords, stringToWords } from '../../mol-util/string'; import * as React from 'react'; import { _Props, _State } from '../base'; import { ParamProps } from './parameters'; import { TextInput, Button, ControlRow } from './common'; import { DefaultColorSwatch } from '../../mol-util/color/swatches'; export class CombinedColorControl extends React.PureComponent, { isExpanded: boolean, lightness: number }> { state = { isExpanded: !!this.props.param.isExpanded, lightness: 0 }; protected update(value: Color) { this.props.onChange({ param: this.props.param, name: this.props.name, value }); } toggleExpanded = (e: React.MouseEvent) => { this.setState({ isExpanded: !this.state.isExpanded }); e.currentTarget.blur(); }; onClickSwatch = (e: React.MouseEvent) => { const value = Color(+(e.currentTarget.getAttribute('data-color') || '0')); if (value !== this.props.value) { if (!this.props.param.isExpanded) this.setState({ isExpanded: false }); this.update(value); } }; onR = (v: number) => { const [, g, b] = Color.toRgb(this.props.value); const value = Color.fromRgb(v, g, b); if (value !== this.props.value) this.update(value); }; onG = (v: number) => { const [r, , b] = Color.toRgb(this.props.value); const value = Color.fromRgb(r, v, b); if (value !== this.props.value) this.update(value); }; onB = (v: number) => { const [r, g] = Color.toRgb(this.props.value); const value = Color.fromRgb(r, g, v); if (value !== this.props.value) this.update(value); }; onLighten = () => { this.update(Color.lighten(this.props.value, 0.1)); }; onDarken = () => { this.update(Color.darken(this.props.value, 0.1)); }; swatch() { return
{DefaultColorSwatch.map(c =>
; } render() { const label = this.props.param.label || camelCaseToWords(this.props.name); const [r, g, b] = Color.toRgb(this.props.value); return <> } /> {this.state.isExpanded &&
{this.swatch()}
}/>
} ; } } let _colors: any = void 0; export function ColorOptions() { if (_colors) return _colors; _colors = <>{DefaultColorSwatch.map(v => )}; return _colors; } const DefaultColorSwatchMap = (function () { const map = new Map(); for (const v of DefaultColorSwatch) map.set(v[1], v[0]); return map; })(); export function ColorValueOption(color: Color) { return !DefaultColorSwatchMap.has(color) ? : null; }