cgv
Loading...
Searching...
No Matches
color_scale.h
1#pragma once
2
3#include <vector>
4
5#include <cgv/data/time_stamp.h>
6#include <cgv/math/fvec.h>
7
8#include "color.h"
9#include "color_scheme.h"
10
11#include "lib_begin.h"
12
13namespace cgv {
14namespace media {
15
22class CGV_API color_scale {
23public:
25 virtual ~color_scale() {}
26
32 virtual bool is_discrete() const = 0;
33
39 virtual bool is_opaque() const {
40 return true;
41 }
42
47 virtual void set_domain(cgv::vec2 domain);
48
53 return domain_;
54 }
55
59 virtual void set_clamped(bool clamped);
60
64 bool is_clamped() const {
65 return is_clamped_;
66 }
67
71 virtual void set_reversed(bool reverse);
72
76 bool is_reversed() const {
77 return is_reversed_;
78 }
79
84 if(unknown_color_ != color) {
85 unknown_color_ = color;
86 modified();
87 }
88 }
89
94 return unknown_color_;
95 }
96
101 virtual float normalize_value(float value) const {
102 return 0.0f;
103 };
104
109 virtual cgv::rgba map_value(float value) const {
110 return { get_mapped_color(value), get_mapped_opacity(value) };
111 }
112
117 virtual cgv::rgb get_mapped_color(float value) const {
118 return cgv::rgb(unknown_color_);
119 }
120
125 virtual float get_mapped_opacity(float value) const {
126 return 1.0f;
127 }
128
134 virtual cgv::rgba get_indexed_color(size_t index) const {
135 return unknown_color_;
136 }
137
142 virtual size_t get_indexed_color_count() const {
143 return 0;
144 }
145
153 virtual std::vector<cgv::rgba> quantize(size_t count) const = 0;
154
159
166 virtual std::vector<float> get_ticks(size_t request_count) const = 0;
167
171 cgv::data::time_point get_modified_time() const {
172 return time_.get_modified_time();
173 }
174
175protected:
177 void modified() {
178 time_.modified();
179 };
180
185 bool is_unknown(float value) const;
186
196 float map_range_safe(float value, float in_left, float in_right, float out_left, float out_right) const;
197
198private:
201
203 cgv::vec2 domain_ = { 0.0f, 1.0f };
205 bool is_clamped_ = true;
207 bool is_reversed_ = false;
209 cgv::rgba unknown_color_ = { 0.0f, 0.0f, 0.0f, 1.0f };
210};
211
213enum class ContinuousMappingTransform {
214 kLinear = 0, // Linear: y = m * x + b
215 kPow, // Power: y = m * x^k + b
216 kLog // Logarithmic: y = m * log(x) + b
217};
218
221class CGV_API continuous_color_scale : public color_scale {
222public:
225
229 continuous_color_scale(const continuous_color_scheme& scheme) : scheme_(scheme) {};
230
233 bool is_discrete() const override {
234 return false;
235 }
236
239 bool is_opaque() const override {
240 return true;
241 }
242
244 void set_domain(cgv::vec2 domain) override;
245
253 void set_transform(ContinuousMappingTransform transform);
254
259 ContinuousMappingTransform get_transform() const {
260 return mapping_transform_;
261 }
262
268 virtual void set_pow_exponent(float exponent);
269
273 float get_pow_exponent() const {
274 return pow_exponent_;
275 }
276
282 virtual void set_log_base(float base);
283
287 float get_log_base() const {
288 return log_base_;
289 }
290
295 virtual void set_diverging(bool diverging);
296
300 bool is_diverging() const {
301 return is_diverging_;
302 }
303
308 virtual void set_midpoint(float midpoint);
309
313 float get_midpoint() const {
314 return diverging_midpoint_;
315 }
316
318 float normalize_value(float value) const override;
319
321 cgv::rgb get_mapped_color(float value) const override;
322
324 std::vector<cgv::rgba> quantize(size_t count) const override;
325
327 std::vector<float> get_ticks(size_t request_count) const override;
328
332 void set_scheme(const continuous_color_scheme& scheme);
333
338 return scheme_;
339 }
340
341private:
343 void update_log_invariants();
344
348 ContinuousMappingTransform mapping_transform_ = ContinuousMappingTransform::kLinear;
350 bool is_diverging_ = false;
352 float diverging_midpoint_ = 0.5f;
354 float pow_exponent_ = 1.0f;
356 float log_base_ = 10.0f;
357
359 float log_of_base_ = 1.0f;
361 float log_of_midpoint_ = 0.5f;
363 float log_of_lower_bound_ = 0.0f;
365 float log_of_upper_bound_ = 1.0f;
367 float log_value_sign_ = 1.0f;
368};
369
373class CGV_API discrete_color_scale : public color_scale {
374public:
375 // @brief Construct using default arguments. The scale will have a single black indexed color.
376 discrete_color_scale() : colors_({ { 0.0f } }) {}
377
382 discrete_color_scale(const discrete_color_scheme& scheme, size_t size) {
383 set_scheme(scheme, size);
384 }
385
388 bool is_discrete() const override {
389 return true;
390 }
391
394 bool is_opaque() const override {
395 return true;
396 }
397
399 float normalize_value(float value) const override;
400
406 cgv::rgb get_mapped_color(float value) const override;
407
412 cgv::rgba get_indexed_color(size_t index) const override;
413
415 size_t get_indexed_color_count() const override {
416 return colors_.size();
417 }
418
424 std::vector<cgv::rgba> quantize(size_t count) const override;
425
430 std::vector<float> get_ticks(size_t request_count) const override;
431
435 void set_scheme(const discrete_color_scheme& scheme, size_t size);
436
443
444private:
446 std::vector<cgv::rgb> colors_;
447};
448
449} // namespace media
450} // namespace cgv
451
452#include <cgv/config/lib_end.h>
Keep a time stamp to store modified time of objects.
Definition time_stamp.h:11
Base class defining an interface for color scales.
Definition color_scale.h:22
virtual float get_mapped_opacity(float value) const
Map a value through the scale and return an opacity.
virtual bool is_opaque() const
Return whether the color scale maps to opacity.
Definition color_scale.h:39
virtual cgv::rgba get_indexed_color(size_t index) const
Map a discrete index through the lookup table and return a RGBA color.
virtual bool is_discrete() const =0
Return whether the color scale represents a discrete mapping.
bool is_reversed() const
Get whether the output color ramp is reversed.
Definition color_scale.h:76
virtual ~color_scale()
Destruct this color scale.
Definition color_scale.h:25
cgv::rgba get_unknown_color() const
Get the color returned for scalars outside the domain if clamping is disabled.
Definition color_scale.h:93
cgv::vec2 get_domain() const
Get the input domain of scalars that will be mapped.
Definition color_scale.h:52
virtual std::vector< cgv::rgba > quantize(size_t count) const =0
Return count uniformly spaced samples from the color ramp or all indexed colors.
virtual size_t get_indexed_color_count() const
return the number of available indexed colors.
virtual cgv::rgb get_mapped_color(float value) const
Map a value through the scale and return a RGB color.
void modified()
Update the object's modified time.
virtual float normalize_value(float value) const
Map a value through the scale and return a normalized value in the range [0,1].
virtual void set_unknown_color(cgv::rgba color)
Set the color returned for scalars outside the domain if clamping is disabled.
Definition color_scale.h:83
bool is_clamped() const
Get whether the input values are clamped to the domain before mapping.
Definition color_scale.h:64
cgv::data::time_point get_modified_time() const
Get the time point of the last modification of this object.
virtual cgv::rgba map_value(float value) const
Map a value through the scale and return a RGBA color.
virtual std::vector< float > get_ticks(size_t request_count) const =0
Evaluate the color scheme at n uniformly-spaced positions within the range [0,1].
Implementation of a color_scale with a continuous input domain and output range using a continuous_co...
float get_log_base() const
Get the base for logarithmic scales.
bool is_diverging() const
Get whether the scale uses a diverging mapping.
continuous_color_scheme get_scheme() const
Get the color scheme used as this scale's color ramp.
bool is_opaque() const override
See color_scale::is_opaque().
float get_midpoint() const
Get the midpoint for diverging scales.
ContinuousMappingTransform get_transform() const
Get the transform used to map scalars to colors.
continuous_color_scale(const continuous_color_scheme &scheme)
Construct using the given color scheme.
float get_pow_exponent() const
Get the exponent for power scales.
continuous_color_scale()
Construct using default arguments.
bool is_discrete() const override
See color_scale::is_discrete().
This class represents a continuous color scheme using an interpolator to convert continuous scalar va...
Implementation of a color_scale with a discrete input domain and output range using a discrete_color_...
discrete_color_scheme get_scheme() const
Get the color scheme used as this scale's indexed color source.
bool is_opaque() const override
See color_scale::is_opaque().
discrete_color_scale(const discrete_color_scheme &scheme, size_t size)
Construct using the given color scheme and size.
bool is_discrete() const override
See color_scale::is_discrete().
size_t get_indexed_color_count() const override
See color_scale::get_indexed_color_count().
This class represents a discrete color scheme using sets of colors to convert discrete scalars to col...
this header is dependency free
Definition print.h:11
cgv::media::color< float, cgv::media::RGB > rgb
declare rgb color type with 32 bit components
Definition color.h:896