1 // This file is part of Desktop App Toolkit,
2 // a set of libraries for developing nice desktop applications.
3 //
4 // For license and copyright information please follow this link:
5 // https://github.com/desktop-app/legal/blob/master/LEGAL
6 //
7 #pragma once
8 
9 #include "ui/style/style_core.h"
10 #include "styles/palette.h"
11 
12 namespace style {
13 
14 struct colorizer;
15 
16 class palette : public palette_data {
17 public:
18 	palette();
19 	palette(const palette &other) = delete;
20 	palette &operator=(const palette &other);
21 	~palette();
22 
23 	QByteArray save() const;
24 	bool load(const QByteArray &cache);
25 
26 	enum class SetResult {
27 		Ok,
28 		KeyNotFound,
29 		ValueNotFound,
30 		Duplicate,
31 	};
32 	SetResult setColor(QLatin1String name, uchar r, uchar g, uchar b, uchar a);
33 	SetResult setColor(QLatin1String name, const QColor &color);
34 	SetResult setColor(QLatin1String name, QLatin1String from);
35 	void reset(const colorizer &with);
36 	void reset();
37 
38 	// Created not inited, should be finalized before usage.
39 	void finalize(const colorizer &with);
40 	void finalize();
41 
42 	int indexOfColor(color c) const;
43 	color colorAtIndex(int index) const;
44 
45 private:
46 	struct FinalizeHelper;
47 	struct TempColorData { uchar r, g, b, a; };
48 	friend class palette_data;
49 
50 	[[nodiscard]] static auto PrepareFinalizeHelper(const colorizer &with)
51 		-> std::unique_ptr<FinalizeHelper>;
52 
53 	void clear();
54 	void compute(int index, int fallbackIndex, TempColorData value);
55 	void setData(int index, const internal::ColorData &value);
56 
57 	std::unique_ptr<FinalizeHelper> _finalizeHelper;
58 	bool _ready = false;
59 
60 };
61 
62 namespace main_palette {
63 
64 not_null<const palette*> get();
65 QByteArray save();
66 bool load(const QByteArray &cache);
67 palette::SetResult setColor(QLatin1String name, uchar r, uchar g, uchar b, uchar a);
68 palette::SetResult setColor(QLatin1String name, QLatin1String from);
69 void apply(const palette &other);
70 void reset();
71 void reset(const colorizer &with);
72 int indexOfColor(color c);
73 
74 } // namespace main_palette
75 } // namespace style
76