import * as React from "react"; import { Color4, Color3 } from 'babylonjs/Maths/math.color'; import { SketchPicker } from 'react-color'; import { GlobalState } from '../globalState'; export interface IColorPickerComponentProps { value: Color4 | Color3; onColorChanged: (newOne: string) => void; globalState: GlobalState; disableAlpha?: boolean; } interface IColorPickerComponentState { pickerEnabled: boolean; color: { r: number, g: number, b: number, a?: number }, hex: string } export class ColorPickerLineComponent extends React.Component { private _floatRef: React.RefObject private _floatHostRef: React.RefObject constructor(props: IColorPickerComponentProps) { super(props); this.state = {pickerEnabled: false, color: { r: this.props.value.r * 255, g: this.props.value.g * 255, b: this.props.value.b * 255, a: this.props.value instanceof Color4 ? this.props.value.a * 100 : 100, }, hex: this.props.value.toHexString()}; this._floatRef = React.createRef(); this._floatHostRef = React.createRef(); } syncPositions() { const div = this._floatRef.current as HTMLDivElement; const host = this._floatHostRef.current as HTMLDivElement; if (!div || !host) { return; } let top = host.getBoundingClientRect().top; let height = div.getBoundingClientRect().height; if (top + height + 10 > window.innerHeight) { top = window.innerHeight - height - 10; } div.style.top = top + "px"; div.style.left = host.getBoundingClientRect().left - div.getBoundingClientRect().width + "px"; } shouldComponentUpdate(nextProps: IColorPickerComponentProps, nextState: IColorPickerComponentState) { let result = nextProps.value.toHexString() !== this.props.value.toHexString() || nextProps.disableAlpha !== this.props.disableAlpha || nextState.hex !== this.state.hex || nextState.pickerEnabled !== this.state.pickerEnabled; if(result) { nextState.color = { r: nextProps.value.r * 255, g: nextProps.value.g * 255, b: nextProps.value.b * 255, a: nextProps.value instanceof Color4 ? nextProps.value.a : 1, }; nextState.hex = nextProps.value.toHexString(); } return result; } componentDidUpdate() { this.syncPositions(); } componentDidMount() { this.syncPositions(); } setPickerState(enabled: boolean) { this.setState({ pickerEnabled: enabled }); this.props.globalState.blockKeyboardEvents = enabled; } render() { var color = this.state.color; this.props.globalState.blockKeyboardEvents = this.state.pickerEnabled; return (
this.setPickerState(true)}>
{ this.state.pickerEnabled && <>
this.setPickerState(false)}>
{ let hex: string; if (this.props.disableAlpha) { let newColor3 = Color3.FromInts(color.rgb.r, color.rgb.g, color.rgb.b); hex = newColor3.toHexString(); } else { let newColor4 = Color4.FromInts(color.rgb.r, color.rgb.g, color.rgb.b, 255 * (color.rgb.a || 0)); hex = newColor4.toHexString(); } this.setState({hex: hex, color: color.rgb}); this.props.onColorChanged(hex); }} />
}
); } }