Scroom 0.14-49-gb7ae7a6d
Loading...
Searching...
No Matches
colormappable.hh
Go to the documentation of this file.
1/*
2 * Scroom - Generic viewer for 2D data
3 * Copyright (C) 2009-2026 Kees-Jan Dijkzeul
4 *
5 * SPDX-License-Identifier: LGPL-2.1
6 */
7
8#pragma once
9
10#include <memory>
11#include <string>
12#include <vector>
13
14#include <scroom/assertions.hh>
15#include <scroom/color.hh>
16#include <scroom/observable.hh>
18
19const std::string COLORMAPPABLE_PROPERTY_NAME = "Colormappable";
20const std::string MONOCHROME_COLORMAPPABLE_PROPERTY_NAME = "Monochrome Colormappable";
21const std::string TRANSPARENT_BACKGROUND_PROPERTY_NAME = "Transparent Background";
22
27{
28public:
29 using Ptr = std::shared_ptr<Colormap>;
30 using ConstPtr = std::shared_ptr<const Colormap>;
31 using WeakPtr = std::weak_ptr<Colormap>;
32
33public:
34 std::string name;
35 std::vector<Color> colors;
37private:
40 : name("Empty")
41 {
42 }
43
44public:
46 static Colormap::Ptr create() { return Ptr(new Colormap()); }
47
50 {
52 result->name = "Default";
53 result->colors.reserve(n);
54 result->colors.clear();
55 const double max = n - 1;
56 for(int i = 0; i < n; i++)
57 {
58 result->colors.emplace_back(i / max); // Min is black
59 }
60
61 return result;
62 }
63
66 {
68 result->name = "0 is white";
69 result->colors.reserve(n);
70 result->colors.clear();
71 const double max = n - 1;
72 for(int i = 0; i < n; i++)
73 {
74 result->colors.emplace_back((max - i) / max); // Min is white
75 }
76
77 return result;
78 }
79
80 [[nodiscard]] Ptr clone() const { return std::make_shared<Colormap>(*this); }
81
82 void setAlpha(double alpha)
83 {
84 for(Color& c: colors)
85 {
86 c.setAlpha(alpha);
87 }
88 }
89
90 [[nodiscard]] Ptr setAlpha(double alpha) const
91 {
92 Ptr result = clone();
93 result->setAlpha(alpha);
94 return result;
95 }
96};
97
112{
113public:
114 using Ptr = std::shared_ptr<Colormappable>;
115 using WeakPtr = std::weak_ptr<Colormappable>;
116
119
122
124 virtual int getNumberOfColors() = 0;
125
131 virtual void setMonochromeColor(const Color& c) = 0;
138 virtual void setTransparentBackground() = 0;
140 virtual bool getTransparentBackground() = 0;
142};
143
145{
146public:
147 using Ptr = std::shared_ptr<ColormapProvider>;
148
149public:
151};
152
154 : public ColormapProvider
155 , public Colormappable
156{
157public:
158 using Ptr = std::shared_ptr<ColormapHelperBase>;
159
160public:
163
164public:
166 virtual std::map<std::string, std::string> getProperties() = 0;
167
169 // Colormappable
171 void setColormap(Colormap::Ptr colormap) override;
173 int getNumberOfColors() override;
174 Color getMonochromeColor() override;
175 void setMonochromeColor(const Color& c) override;
176 void setTransparentBackground() override;
177 void disableTransparentBackground() override;
178 bool getTransparentBackground() override;
179
181 // ColormapProvider
183 Colormap::Ptr getColormap() override;
184
186 // Helpers
189
190private:
191 [[noreturn]] static void OperationNotSupported();
192};
193
195{
196public:
197 static Ptr create(int numberOfColors);
198 static Ptr createInverted(int numberOfColors);
199 static Ptr create(Colormap::Ptr const& colormap);
200
201 std::map<std::string, std::string> getProperties() override;
202
203private:
204 explicit ColormapHelper(Colormap::Ptr const& colormap);
205};
206
208{
209public:
210 static Ptr create(int numberOfColors);
212
213 std::map<std::string, std::string> getProperties() override;
214
216 // Colormappable
218
219 Color getMonochromeColor() override;
220 void setMonochromeColor(const Color& c) override;
221 void setTransparentBackground() override;
222 void disableTransparentBackground() override;
223 bool getTransparentBackground() override;
224
225private:
227 void regenerateColormap();
229
230private:
236};
Definition color.hh:37
Definition colormappable.hh:156
Colormap::Ptr getOriginalColormap() override
Definition colormap-helpers.cc:28
int getNumberOfColors() override
Definition colormap-helpers.cc:30
Colormap::Ptr getColormap() override
Definition colormap-helpers.cc:42
Colormap::Ptr originalColormap
Definition colormappable.hh:162
void setMonochromeColor(const Color &c) override
Definition colormap-helpers.cc:34
Color getMonochromeColor() override
Definition colormap-helpers.cc:32
virtual std::map< std::string, std::string > getProperties()=0
void setColormap(Colormap::Ptr colormap) override
Definition colormap-helpers.cc:24
virtual void setOriginalColormap(Colormap::Ptr colormap)
Definition colormap-helpers.cc:26
std::shared_ptr< ColormapHelperBase > Ptr
Definition colormappable.hh:158
bool getTransparentBackground() override
Definition colormap-helpers.cc:40
static void OperationNotSupported()
Definition colormap-helpers.cc:44
void disableTransparentBackground() override
Definition colormap-helpers.cc:38
Colormap::Ptr colormap
Definition colormappable.hh:161
void setTransparentBackground() override
Definition colormap-helpers.cc:36
Definition colormappable.hh:195
static Ptr create(int numberOfColors)
Definition colormap-helpers.cc:51
static Ptr createInverted(int numberOfColors)
Definition colormap-helpers.cc:53
std::map< std::string, std::string > getProperties() override
Definition colormap-helpers.cc:63
Definition colormappable.hh:145
std::shared_ptr< ColormapProvider > Ptr
Definition colormappable.hh:147
virtual Colormap::Ptr getColormap()=0
Definition colormappable.hh:27
void setAlpha(double alpha)
Definition colormappable.hh:82
std::vector< Color > colors
Definition colormappable.hh:35
Ptr clone() const
Definition colormappable.hh:80
Ptr setAlpha(double alpha) const
Definition colormappable.hh:90
std::string name
Definition colormappable.hh:34
static Colormap::Ptr createDefaultInverted(int n)
Definition colormappable.hh:65
static Colormap::Ptr create()
Definition colormappable.hh:46
std::weak_ptr< Colormap > WeakPtr
Definition colormappable.hh:31
std::shared_ptr< const Colormap > ConstPtr
Definition colormappable.hh:30
std::shared_ptr< Colormap > Ptr
Definition colormappable.hh:29
Colormap()
Definition colormappable.hh:39
static Colormap::Ptr createDefault(int n)
Definition colormappable.hh:49
Definition colormappable.hh:112
virtual Colormap::Ptr getOriginalColormap()=0
virtual int getNumberOfColors()=0
virtual void setTransparentBackground()=0
std::shared_ptr< Colormappable > Ptr
Definition colormappable.hh:114
virtual Color getMonochromeColor()=0
virtual void disableTransparentBackground()=0
std::weak_ptr< Colormappable > WeakPtr
Definition colormappable.hh:115
virtual void setColormap(Colormap::Ptr colormap)=0
virtual void setMonochromeColor(const Color &c)=0
virtual bool getTransparentBackground()=0
Definition interface.hh:11
Definition colormappable.hh:208
std::map< std::string, std::string > getProperties() override
Definition colormap-helpers.cc:145
int numberOfColors
Definition colormappable.hh:231
static Ptr create(int numberOfColors)
Definition colormap-helpers.cc:68
Color getMonochromeColor() override
Definition colormap-helpers.cc:102
void setTransparentBackground() override
Definition colormap-helpers.cc:131
void setMonochromeColor(const Color &c) override
Definition colormap-helpers.cc:88
void disableTransparentBackground() override
Definition colormap-helpers.cc:137
Color blackish
Definition colormappable.hh:233
void regenerateColormap()
Definition colormap-helpers.cc:104
bool transparentBackground
Definition colormappable.hh:235
static Colormap::Ptr generateInitialColormap(int numberOfColors, bool inverted)
Definition colormap-helpers.cc:126
static Ptr createInverted(int numberOfColors)
Definition colormap-helpers.cc:73
bool getTransparentBackground() override
Definition colormap-helpers.cc:143
Color whitish
Definition colormappable.hh:234
bool inverted
Definition colormappable.hh:232
Colormap::Ptr const colormap
Definition colormaphelpers_test.cc:55
const std::string TRANSPARENT_BACKGROUND_PROPERTY_NAME
Definition colormappable.hh:21
const std::string COLORMAPPABLE_PROPERTY_NAME
Definition colormappable.hh:19
const std::string MONOCHROME_COLORMAPPABLE_PROPERTY_NAME
Definition colormappable.hh:20
SampleIterator< const uint8_t > result
Definition sampleiterator-tests.cc:94
Semaphore c(0)