// Copyright 2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import React, { useState } from 'react'; import { Button, ButtonVariant } from './Button'; import { GradientDial, KnobType } from './GradientDial'; import { SampleMessageBubbles } from './SampleMessageBubbles'; import { Slider } from './Slider'; import { Tabs } from './Tabs'; import type { CustomColorType } from '../types/Colors'; import type { LocalizerType } from '../types/Util'; import { getHSL } from '../util/getHSL'; import { getCustomColorStyle } from '../util/getCustomColorStyle'; export type PropsType = { customColor?: CustomColorType; i18n: LocalizerType; onClose: () => unknown; onSave: (color: CustomColorType) => unknown; }; enum TabViews { Solid = 'Solid', Gradient = 'Gradient', } function getPercentage(value: number, max: number): number { return (100 * value) / max; } function getValue(percentage: number, max: number): number { return Math.round((max / 100) * percentage); } const MAX_HUE = 360; const ULTRAMARINE_ISH_VALUES = { hue: 220, saturation: 84, }; const ULTRAMARINE_ISH: CustomColorType = { start: ULTRAMARINE_ISH_VALUES, deg: 180, }; export function CustomColorEditor({ customColor = ULTRAMARINE_ISH, i18n, onClose, onSave, }: PropsType): JSX.Element { const [color, setColor] = useState(customColor); const [selectedColorKnob, setSelectedColorKnob] = useState( KnobType.start ); const { hue, saturation } = color[selectedColorKnob] || ULTRAMARINE_ISH_VALUES; return ( { if (selectedTab === TabViews.Gradient && !color.end) { setColor({ ...color, end: ULTRAMARINE_ISH_VALUES, }); } if (selectedTab === TabViews.Solid && color.end) { setColor({ ...color, end: undefined, }); } }} tabs={[ { id: TabViews.Solid, label: i18n('icu:CustomColorEditor__solid'), }, { id: TabViews.Gradient, label: i18n('icu:CustomColorEditor__gradient'), }, ]} > {({ selectedTab }) => ( <>
{selectedTab === TabViews.Gradient && (
{ setColor({ ...color, deg, }); }} onClick={knob => setSelectedColorKnob(knob)} selectedKnob={selectedColorKnob} />
)}
{i18n('icu:CustomColorEditor__hue')} { setColor({ ...color, [selectedColorKnob]: { ...ULTRAMARINE_ISH_VALUES, ...color[selectedColorKnob], hue: getValue(percentage, MAX_HUE), }, }); }} value={getPercentage(hue, MAX_HUE)} />
{i18n('icu:CustomColorEditor__saturation')} { setColor({ ...color, [selectedColorKnob]: { ...ULTRAMARINE_ISH_VALUES, ...color[selectedColorKnob], saturation: value, }, }); }} value={saturation} />
)}
); }