MCPcopy Create free account
hub / github.com/chrxh/alien / getMatchedCellColor

Function getMatchedCellColor

source/Gui/ImageToPatternDialog.cpp:39–77  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

37namespace
38{
39 void getMatchedCellColor(ImColor const& color, int& matchedCellColor, float& matchedCellIntensity)
40 {
41 using Color = std::array<float,3>;
42 static std::vector<Color> cellColors;
43 auto toHsv = [](uint32_t color) {
44 float h, s, v;
45 AlienImGui::ConvertRGBtoHSV(color, h, s, v);
46 return Color{h, s, v};
47 };
48 if (cellColors.empty()) {
49 cellColors.emplace_back(toHsv(Const::IndividualCellColor1));
50 cellColors.emplace_back(toHsv(Const::IndividualCellColor2));
51 cellColors.emplace_back(toHsv(Const::IndividualCellColor3));
52 cellColors.emplace_back(toHsv(Const::IndividualCellColor4));
53 cellColors.emplace_back(toHsv(Const::IndividualCellColor5));
54 cellColors.emplace_back(toHsv(Const::IndividualCellColor6));
55 cellColors.emplace_back(toHsv(Const::IndividualCellColor7));
56 }
57
58 std::optional<int> bestMatchIndex;
59 std::optional<float> bestMatchDistance;
60 auto colorHsv = toHsv((ImU32)color);
61 for (auto const& [index, cellColor] : cellColors | boost::adaptors::indexed(0)) {
62 auto distance = colorHsv[0] - cellColor[0];
63 if (distance > 0.5f) {
64 distance -= 1.0f;
65 }
66 if (distance < -0.5f) {
67 distance += 1.0f;
68 }
69 distance = std::abs(distance) * colorHsv[1] + std::abs(colorHsv[1] - cellColor[1]);
70 if (!bestMatchDistance || *bestMatchDistance > distance) {
71 bestMatchIndex = toInt(index);
72 bestMatchDistance = distance;
73 }
74 }
75 matchedCellColor = *bestMatchIndex;
76 matchedCellIntensity = colorHsv[2];
77 }
78}
79
80void ImageToPatternDialog::show()

Callers 1

showMethod · 0.85

Calls 1

toIntFunction · 0.85

Tested by

no test coverage detected