You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.2 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. import React, {useCallback, useContext, useEffect, useState} from "react";
  2. import {ColorPreset} from "../models/Colors";
  3. import DialogContext from "../contexts/DialogContext";
  4. import {DialogType} from "../models/Dialog";
  5. import {Dialog} from "../primitives/Layout";
  6. import {LuciferIcon} from "../models/Icons";
  7. import {HSColorPicker, Input, KelvinColorPicker} from "../primitives/Forms";
  8. interface ColorDialogProps {
  9. data?: ColorPreset
  10. }
  11. enum ColorType {
  12. HueSat,
  13. Kelvin,
  14. HueSatKelvin,
  15. }
  16. const COLOR_TYPE_OPTIONS = [
  17. {value: ColorType.HueSat, name: "Bare Nyanse (H) og metning (S)"},
  18. {value: ColorType.Kelvin, name: "Bare temperatur (K)"},
  19. {value: ColorType.HueSatKelvin, name: "Nyanse (H), metning (K) og kelvin (S)"},
  20. ];
  21. export default function ColorDialog({data}: ColorDialogProps) {
  22. const {setDialog, clearDialog} = useContext(DialogContext);
  23. const [name, setName] = useState(data?.name || "");
  24. const [hue, setHue] = useState(data?.value?.h || 0);
  25. const [sat, setSat] = useState(data?.value?.s || 0);
  26. const [kelvin, setKelvin] = useState(data?.value?.kelvin || 0);
  27. const [colorType, setColorType] = useState(COLOR_TYPE_OPTIONS[0].value);
  28. const [isInit, setIsInit] = useState(false);
  29. const onSave = useCallback(() => {
  30. setDialog({type: DialogType.None});
  31. }, [setDialog]);
  32. useEffect(() => {
  33. if (!isInit && data) {
  34. const v = data.value;
  35. if (v.kelvin !== undefined && v.kelvin > 0) {
  36. setColorType((v.h || v.s) ? ColorType.HueSatKelvin : ColorType.Kelvin);
  37. }
  38. }
  39. if (!isInit) {
  40. setIsInit(true);
  41. }
  42. }, [isInit, data, colorType]);
  43. return (
  44. <Dialog title={data ? "Endre farge" : "Ny farge"} buttons={[
  45. {icon: LuciferIcon.Check, text: "Lagre", onClick: onSave},
  46. {icon: LuciferIcon.Cross, text: "Avbryt", onClick: clearDialog},
  47. ]}>
  48. <Input label="Navn" value={name} onChange={setName}/>
  49. {colorType !== ColorType.Kelvin && (
  50. <HSColorPicker h={hue} s={sat} onChange={(h, s) => {
  51. setHue(h);
  52. setSat(s);
  53. }}/>
  54. )}
  55. {colorType !== ColorType.HueSat && (
  56. <KelvinColorPicker kelvin={kelvin} onChange={setKelvin}/>
  57. )}
  58. </Dialog>
  59. );
  60. }