SMACC2
Loading...
Searching...
No Matches
colour.cpp
Go to the documentation of this file.
1// Copyright 2026 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include <cl_rgb/colour.hpp>
16
17#include <cmath>
18#include <limits>
19
20namespace cl_rgb
21{
22
23const std::vector<NamedColour> & basicColours()
24{
25 static const std::vector<NamedColour> colours = {
26 {"red", {53.2, 80.1, 67.2}}, {"orange", {74.9, 23.9, 78.9}},
27 {"yellow", {97.1, -21.6, 94.5}}, {"green", {87.7, -86.2, 83.2}},
28 {"cyan", {91.1, -48.1, -14.1}}, {"blue", {32.3, 79.2, -107.9}},
29 {"purple", {29.8, 58.9, -36.5}}, {"magenta", {60.3, 98.3, -60.8}},
30 {"pink", {83.6, 24.1, 3.3}}, {"brown", {37.5, 26.4, 40.9}},
31 };
32 return colours;
33}
34
35std::string colourName(const cv::Vec3d & lab)
36{
37 const double chroma = std::hypot(lab[1], lab[2]);
38 if (chroma < 12.0)
39 {
40 return lab[0] < 25.0 ? "black" : (lab[0] > 75.0 ? "white" : "gray");
41 }
42 std::string best;
43 double bestCost = std::numeric_limits<double>::max();
44 for (const auto & colour : basicColours())
45 {
46 const double cost = colourCost(lab, colour.name);
47 if (cost < bestCost)
48 {
49 bestCost = cost;
50 best = colour.name;
51 }
52 }
53 return best;
54}
55
56double colourCost(const cv::Vec3d & lab, const std::string & name)
57{
58 for (const auto & colour : basicColours())
59 {
60 if (colour.name != name)
61 {
62 continue;
63 }
64 const double chroma = std::hypot(lab[1], lab[2]);
65 double difference =
66 std::abs(std::atan2(lab[2], lab[1]) - std::atan2(colour.lab[2], colour.lab[1]));
67 if (difference > CV_PI)
68 {
69 difference = 2.0 * CV_PI - difference;
70 }
71 // Hue decides; lightness and chroma separate the words that share a hue (orange and
72 // brown, red and pink, blue and purple).
73 return difference * 60.0 + 0.5 * std::abs(lab[0] - colour.lab[0]) +
74 0.25 * std::abs(chroma - std::hypot(colour.lab[1], colour.lab[2]));
75 }
76 return std::numeric_limits<double>::infinity();
77}
78
79namespace
80{
81void assign(
82 const std::vector<std::vector<double>> & cost, size_t name, double sum,
83 std::vector<int> & current, std::vector<bool> & used, double & bestSum, std::vector<int> & best)
84{
85 if (sum >= bestSum)
86 {
87 return;
88 }
89 if (name == cost.size())
90 {
91 bestSum = sum;
92 best = current;
93 return;
94 }
95 for (size_t i = 0; i < used.size(); ++i)
96 {
97 if (!used[i])
98 {
99 used[i] = true;
100 current[name] = static_cast<int>(i);
101 assign(cost, name + 1, sum + cost[name][i], current, used, bestSum, best);
102 used[i] = false;
103 }
104 }
105}
106} // namespace
107
108std::vector<int> matchColours(
109 const std::vector<cv::Vec3d> & seen, const std::vector<std::string> & names)
110{
111 if (seen.size() < names.size() || names.empty())
112 {
113 return {};
114 }
115 std::vector<std::vector<double>> cost(names.size(), std::vector<double>(seen.size(), 0.0));
116 for (size_t n = 0; n < names.size(); ++n)
117 {
118 for (size_t i = 0; i < seen.size(); ++i)
119 {
120 cost[n][i] = colourCost(seen[i], names[n]);
121 }
122 }
123 std::vector<int> current(names.size(), -1);
124 std::vector<int> best;
125 std::vector<bool> used(seen.size(), false);
126 double bestSum = std::numeric_limits<double>::max();
127 assign(cost, 0, 0.0, current, used, bestSum, best);
128 return best;
129}
130
131} // namespace cl_rgb
void assign(const std::vector< std::vector< double > > &cost, size_t name, double sum, std::vector< int > &current, std::vector< bool > &used, double &bestSum, std::vector< int > &best)
Definition colour.cpp:81
double colourCost(const cv::Vec3d &lab, const std::string &name)
Definition colour.cpp:56
std::vector< int > matchColours(const std::vector< cv::Vec3d > &seen, const std::vector< std::string > &names)
Definition colour.cpp:108
std::string colourName(const cv::Vec3d &lab)
Definition colour.cpp:35
const std::vector< NamedColour > & basicColours()
Definition colour.cpp:23