-
Notifications
You must be signed in to change notification settings - Fork 12
Description
When a user selects a unique color the rgb value is entered.
Nobody knows what those values mean in terms of color (or most people don't)
It would be nice to have selection of colors from a longer defined list of colors with Names, using a color lookup table with names and rgb values.
Leamas is doing this in Opencpn, don't know if it ends up with rgb names.
https://github.com/OpenCPN/OpenCPN/pull/4977/files
You’ve got two solid paths here, depending on whether you want a ready‑made lookup table or C++ code that performs the lookup. Let’s walk through both so you can drop them straight into your workflow.
🎨 Option 1: Use an existing color lookup table
If you just want a comprehensive list of color names and their RGB values, several public tables exist. For example:
• RapidTables provides a large RGB color table with names, hex, and decimal RGB values.
• Utils.com offers a similarly extensive reference of named colors with RGB and hex values.
• Some color databases include 2000+ named colors, searchable by name.
These tables are easy to scrape or convert into a header file if you want a static lookup.
🧰 Option 2: C++ color lookup function
If you'd rather embed the functionality directly in your code, here’s a clean, extensible approach using .
✅ Minimal C++ Example (drop‑in ready)
#include <string>
#include <unordered_map>
#include <optional>
#include <tuple>
using RGB = std::tuple<int, int, int>;
class ColorLookup {
public:
ColorLookup() {
// Basic set — expand as needed
colors_ = {
{"black", {0, 0, 0}},
{"white", {255, 255, 255}},
{"red", {255, 0, 0}},
{"lime", {0, 255, 0}},
{"blue", {0, 0, 255}},
{"yellow", {255, 255, 0}},
{"cyan", {0, 255, 255}},
{"magenta", {255, 0, 255}},
{"gray", {128, 128, 128}},
{"maroon", {128, 0, 0}},
{"olive", {128, 128, 0}},
{"green", {0, 128, 0}},
{"purple", {128, 0, 128}},
{"teal", {0, 128, 128}},
{"navy", {0, 0, 128}}
};
}
std::optional<RGB> get(const std::string& name) const {
auto it = colors_.find(toLower(name));
if (it != colors_.end()) {
return it->second;
}
return std::nullopt;
}
private:
std::unordered_map<std::string, RGB> colors_;
static std::string toLower(std::string s) {
for (auto& c : s) c = std::tolower(c);
return s;
}
};
✅ Usage
ColorLookup lookup;
auto rgb = lookup.get("Magenta");
if (rgb) {
auto [r, g, b] = *rgb;
std::cout << "RGB = " << r << ", " << g << ", " << b << "\n";
} else {
std::cout << "Color not found\n";
}
🚀 Want a full 2000‑color C++ header?
If you want, I can generate:
• A header‑only C++ library with thousands of named colors
• A constexpr lookup table
• A binary‑search or perfect‑hash implementation
• A JSON or CSV version you can import into your own tooling
Just tell me the format you prefer and how large you want the table to be.