qualpal 3.3.0
Loading...
Searching...
No Matches
color_difference.h
Go to the documentation of this file.
1
12#pragma once
13
14#include <cmath>
15#include <qualpal/matrix.h>
16#include <qualpal/metrics.h>
17#include <qualpal/threads.h>
18#include <vector>
19
20#ifdef _OPENMP
21#include <omp.h>
22#endif
23
24namespace qualpal {
25namespace detail {
26
27inline std::size_t
28estimateMatrixMemory(std::size_t n)
29{
30 return n * n * sizeof(double);
31}
32
33inline bool
34checkMatrixSize(std::size_t n, double max_gb = 1.0)
35{
36 double estimated_gb = estimateMatrixMemory(n) / (1024.0 * 1024.0 * 1024.0);
37 return estimated_gb <= max_gb;
38}
39} // namespace detail
40
63template<typename ColorType, typename Metric = metrics::DIN99d>
64Matrix<double>
65colorDifferenceMatrix(const std::vector<ColorType>& colors,
66 const Metric& metric = Metric{},
67 const double max_memory = 1)
68{
69 using namespace detail;
70
71 const std::size_t n_colors = colors.size();
72
73 if (n_colors < 1) {
74 throw std::invalid_argument("At least one color is required to compute "
75 "a color difference matrix.");
76 }
77
78 if (!checkMatrixSize(n_colors, max_memory)) {
79 throw std::runtime_error(
80 "Color difference matrix would require " +
81 std::to_string(estimateMatrixMemory(n_colors) /
82 (1024.0 * 1024.0 * 1024.0)) +
83 " GB, which exceeds the limit of " + std::to_string(max_memory) +
84 " GB. Reduce the number of colors or increase the memory limit.");
85 }
86
87 Matrix<double> result(n_colors, n_colors);
88
89#ifdef _OPENMP
90#pragma omp parallel for num_threads(Threads::get())
91#endif
92 for (std::size_t i = 0; i < n_colors; ++i) {
93 result(i, i) = 0.0;
94#ifdef _OPENMP
95#pragma omp simd
96#endif
97 for (std::size_t j = i + 1; j < n_colors; ++j) {
98 double d = metric(colors[i], colors[j]);
99 result(i, j) = d;
100 result(j, i) = d;
101 }
102 }
103 return result;
104}
105
128Matrix<double>
129colorDifferenceMatrix(const std::vector<colors::XYZ>& colors,
130 const metrics::MetricType& metric_type,
131 const double max_memory = 1);
132
133} // namespace qualpal
Matrix classes for qualpal.
Color difference metrics for qualpal.
MetricType
Supported color difference metrics for palette generation and analysis.
Definition metrics.h:91
Qualitative color palette generation library.
Definition analyze.h:21
Matrix< double > colorDifferenceMatrix(const std::vector< ColorType > &colors, const Metric &metric=Metric{}, const double max_memory=1)
Generate a symmetric color difference matrix for a set of colors.
Thread management for parallel computations.