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