|
18 | 18 |
|
19 | 19 | #include <algorithm>
|
20 | 20 | #include <cmath>
|
21 |
| -#include <cstdlib> |
22 | 21 | #include <map>
|
| 22 | +#include <utility> |
23 | 23 | #include <vector>
|
24 | 24 |
|
25 |
| -#include "cpp/cam/cam.h" |
| 25 | +#include "cpp/cam/hct.h" |
26 | 26 | #include "cpp/utils/utils.h"
|
27 | 27 |
|
28 | 28 | namespace material_color_utilities {
|
29 | 29 |
|
30 |
| -constexpr double kCutoffChroma = 15.0; |
31 |
| -constexpr double kCutoffExcitedProportion = 0.01; |
32 |
| -constexpr double kCutoffTone = 10.0; |
33 | 30 | constexpr double kTargetChroma = 48.0; // A1 Chroma
|
34 | 31 | constexpr double kWeightProportion = 0.7;
|
35 | 32 | constexpr double kWeightChromaAbove = 0.3;
|
36 | 33 | constexpr double kWeightChromaBelow = 0.1;
|
| 34 | +constexpr double kCutoffChroma = 5.0; |
| 35 | +constexpr double kCutoffExcitedProportion = 0.01; |
37 | 36 |
|
38 |
| -struct AnnotatedColor { |
39 |
| - Argb argb = 0; |
40 |
| - Cam cam; |
41 |
| - double excited_proportion = 0.0; |
42 |
| - double score = 0.0; |
43 |
| -}; |
44 |
| - |
45 |
| -bool ArgbAndScoreComparator(const AnnotatedColor& a, const AnnotatedColor& b) { |
46 |
| - return a.score > b.score; |
47 |
| -} |
48 |
| - |
49 |
| -bool IsAcceptableColor(const AnnotatedColor& color) { |
50 |
| - return color.cam.chroma >= kCutoffChroma && |
51 |
| - LstarFromArgb(color.argb) >= kCutoffTone && |
52 |
| - color.excited_proportion >= kCutoffExcitedProportion; |
53 |
| -} |
54 |
| - |
55 |
| -bool ColorsAreTooClose(const AnnotatedColor& color_one, |
56 |
| - const AnnotatedColor& color_two) { |
57 |
| - return DiffDegrees(color_one.cam.hue, color_two.cam.hue) < 15; |
| 37 | +bool CompareScoredHCT(const std::pair<Hct, double>& a, |
| 38 | + const std::pair<Hct, double>& b) { |
| 39 | + return a.second > b.second; |
58 | 40 | }
|
59 | 41 |
|
60 | 42 | std::vector<Argb> RankedSuggestions(
|
61 |
| - const std::map<Argb, int>& argb_to_population) { |
| 43 | + const std::map<Argb, int>& argb_to_population, |
| 44 | + const ScoreOptions& options) { |
| 45 | + // Get the HCT color for each Argb value, while finding the per hue count and |
| 46 | + // total count. |
| 47 | + std::vector<Hct> colors_hct; |
| 48 | + std::vector<int> hue_population(360, 0); |
62 | 49 | double population_sum = 0;
|
63 |
| - int input_size = argb_to_population.size(); |
64 |
| - |
65 |
| - std::vector<Argb> argbs; |
66 |
| - std::vector<int> populations; |
67 |
| - |
68 |
| - argbs.reserve(input_size); |
69 |
| - populations.reserve(input_size); |
70 |
| - |
71 |
| - for (auto const& pair : argb_to_population) { |
72 |
| - argbs.push_back(pair.first); |
73 |
| - populations.push_back(pair.second); |
74 |
| - } |
75 |
| - |
76 |
| - for (int i = 0; i < input_size; i++) { |
77 |
| - population_sum += populations[i]; |
| 50 | + for (const auto& [argb, population] : argb_to_population) { |
| 51 | + Hct hct(argb); |
| 52 | + colors_hct.push_back(hct); |
| 53 | + int hue = floor(hct.get_hue()); |
| 54 | + hue_population[hue] += population; |
| 55 | + population_sum += population; |
78 | 56 | }
|
79 | 57 |
|
80 |
| - double hue_proportions[361] = {}; |
81 |
| - std::vector<AnnotatedColor> colors; |
82 |
| - |
83 |
| - for (int i = 0; i < input_size; i++) { |
84 |
| - double proportion = populations[i] / population_sum; |
85 |
| - |
86 |
| - Cam cam = CamFromInt(argbs[i]); |
87 |
| - |
88 |
| - int hue = SanitizeDegreesInt(round(cam.hue)); |
89 |
| - hue_proportions[hue] += proportion; |
90 |
| - |
91 |
| - colors.push_back({argbs[i], cam, 0, -1}); |
92 |
| - } |
93 |
| - |
94 |
| - for (int i = 0; i < input_size; i++) { |
95 |
| - int hue = round(colors[i].cam.hue); |
96 |
| - for (int j = (hue - 15); j < (hue + 15); j++) { |
97 |
| - int sanitized_hue = SanitizeDegreesInt(j); |
98 |
| - colors[i].excited_proportion += hue_proportions[sanitized_hue]; |
| 58 | + // Hues with more usage in neighboring 30 degree slice get a larger number. |
| 59 | + std::vector<double> hue_excited_proportions(360, 0.0); |
| 60 | + for (int hue = 0; hue < 360; hue++) { |
| 61 | + double proportion = hue_population[hue] / population_sum; |
| 62 | + for (int i = hue - 14; i < hue + 16; i++) { |
| 63 | + int neighbor_hue = SanitizeDegreesInt(i); |
| 64 | + hue_excited_proportions[neighbor_hue] += proportion; |
99 | 65 | }
|
100 | 66 | }
|
101 | 67 |
|
102 |
| - for (int i = 0; i < input_size; i++) { |
103 |
| - double proportion_score = |
104 |
| - colors[i].excited_proportion * 100.0 * kWeightProportion; |
105 |
| - |
106 |
| - double chroma = colors[i].cam.chroma; |
107 |
| - double chroma_weight = |
108 |
| - (chroma > kTargetChroma ? kWeightChromaAbove : kWeightChromaBelow); |
109 |
| - double chroma_score = (chroma - kTargetChroma) * chroma_weight; |
110 |
| - |
111 |
| - colors[i].score = chroma_score + proportion_score; |
112 |
| - } |
113 |
| - |
114 |
| - std::sort(colors.begin(), colors.end(), ArgbAndScoreComparator); |
115 |
| - |
116 |
| - std::vector<AnnotatedColor> selected_colors; |
117 |
| - |
118 |
| - for (int i = 0; i < input_size; i++) { |
119 |
| - if (!IsAcceptableColor(colors[i])) { |
| 68 | + // Scores each HCT color based on usage and chroma, while optionally |
| 69 | + // filtering out values that do not have enough chroma or usage. |
| 70 | + std::vector<std::pair<Hct, double>> scored_hcts; |
| 71 | + for (Hct hct : colors_hct) { |
| 72 | + int hue = SanitizeDegreesInt(round(hct.get_hue())); |
| 73 | + double proportion = hue_excited_proportions[hue]; |
| 74 | + if (options.filter && (hct.get_chroma() < kCutoffChroma || |
| 75 | + proportion <= kCutoffExcitedProportion)) { |
120 | 76 | continue;
|
121 | 77 | }
|
122 | 78 |
|
123 |
| - bool is_duplicate_color = false; |
124 |
| - for (size_t j = 0; j < selected_colors.size(); j++) { |
125 |
| - if (ColorsAreTooClose(selected_colors[j], colors[i])) { |
126 |
| - is_duplicate_color = true; |
127 |
| - break; |
| 79 | + double proportion_score = proportion * 100.0 * kWeightProportion; |
| 80 | + double chroma_weight = hct.get_chroma() < kTargetChroma |
| 81 | + ? kWeightChromaBelow |
| 82 | + : kWeightChromaAbove; |
| 83 | + double chroma_score = (hct.get_chroma() - kTargetChroma) * chroma_weight; |
| 84 | + double score = proportion_score + chroma_score; |
| 85 | + scored_hcts.push_back({hct, score}); |
| 86 | + } |
| 87 | + // Sorted so that colors with higher scores come first. |
| 88 | + sort(scored_hcts.begin(), scored_hcts.end(), CompareScoredHCT); |
| 89 | + |
| 90 | + // Iterates through potential hue differences in degrees in order to select |
| 91 | + // the colors with the largest distribution of hues possible. Starting at |
| 92 | + // 90 degrees(maximum difference for 4 colors) then decreasing down to a |
| 93 | + // 15 degree minimum. |
| 94 | + std::vector<Hct> chosen_colors; |
| 95 | + for (int difference_degrees = 90; difference_degrees >= 15; |
| 96 | + difference_degrees--) { |
| 97 | + chosen_colors.clear(); |
| 98 | + for (auto entry : scored_hcts) { |
| 99 | + Hct hct = entry.first; |
| 100 | + auto duplicate_hue = std::find_if( |
| 101 | + chosen_colors.begin(), chosen_colors.end(), |
| 102 | + [&hct, difference_degrees](Hct chosen_hct) { |
| 103 | + return DiffDegrees(hct.get_hue(), chosen_hct.get_hue()) < |
| 104 | + difference_degrees; |
| 105 | + }); |
| 106 | + if (duplicate_hue == chosen_colors.end()) { |
| 107 | + chosen_colors.push_back(hct); |
| 108 | + if (chosen_colors.size() >= options.desired) break; |
128 | 109 | }
|
129 | 110 | }
|
130 |
| - |
131 |
| - if (is_duplicate_color) { |
132 |
| - continue; |
133 |
| - } |
134 |
| - |
135 |
| - selected_colors.push_back(colors[i]); |
| 111 | + if (chosen_colors.size() >= options.desired) break; |
136 | 112 | }
|
137 |
| - |
138 |
| - // Use google blue if no colors are selected. |
139 |
| - if (selected_colors.empty()) { |
140 |
| - selected_colors.push_back({0xFF4285F4, {}, 0.0, 0.0}); |
| 113 | + std::vector<Argb> colors; |
| 114 | + if (chosen_colors.empty()) { |
| 115 | + colors.push_back(options.fallback_color_argb); |
141 | 116 | }
|
142 |
| - |
143 |
| - std::vector<Argb> return_value(selected_colors.size()); |
144 |
| - |
145 |
| - for (size_t j = 0; j < selected_colors.size(); j++) { |
146 |
| - return_value[j] = selected_colors[j].argb; |
| 117 | + for (auto chosen_hct : chosen_colors) { |
| 118 | + colors.push_back(chosen_hct.ToInt()); |
147 | 119 | }
|
148 |
| - |
149 |
| - return return_value; |
| 120 | + return colors; |
150 | 121 | }
|
151 | 122 |
|
152 | 123 | } // namespace material_color_utilities
|
0 commit comments