Skip to content

Instantly share code, notes, and snippets.

@ragingwind
Created May 13, 2021 23:31
Show Gist options
  • Save ragingwind/27ba842f17429e7a2a2d7f17ddd761a5 to your computer and use it in GitHub Desktop.
Save ragingwind/27ba842f17429e7a2a2d7f17ddd761a5 to your computer and use it in GitHub Desktop.
RGB Class for Modern C++
#include <iostream>
#include <string>
#include <map>
#include <cxxabi.h>
class RGB {
public:
using value_type = std::tuple<unsigned, unsigned, unsigned>;
RGB(unsigned r, unsigned g, unsigned b) : m_r(r), m_g(g), m_b(b) {}
[[nodiscard]] value_type rgb() const {return {m_r, m_g, m_b};}
value_type operator()() const {return rgb();}
private:
unsigned m_r = 0;
unsigned m_g = 0;
unsigned m_b = 0;
};
int main() {
auto red = RGB(255, 0, 0);
auto [r, g, b] = red.rgb();
std::cout << "red r: " << r << " g: " << g << " b: " << b << std::endl;
std::map<std::string, RGB> rgbs;
rgbs["red"] = red;
rgbs["blue"] = RGB(0, 0, 255);
rgbs["green"] = RGB(0, 255, 0);
int status = 0;
for (auto& rgb: rgbs) {
std::cout << abi::__cxa_demangle(typeid(rgb).name(), 0, 0, &status)<< std::endl;
}
for (auto& [name, rgb]: rgbs) {
auto [r, g, b] = rgb.rgb();
auto [r1, g1, b1] = rgb();
std::cout << name << " r: " << r << " g: " << g << " b: " << b << std::endl;
std::cout << name << " r: " << r1 << " g: " << g1 << " b: " << b1 << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment