Skip to content

Instantly share code, notes, and snippets.

@domeniko-gentner
Last active May 29, 2019 16:42
Show Gist options
  • Save domeniko-gentner/61d1812014ac4ee34db4d767c5d9f9a8 to your computer and use it in GitHub Desktop.
Save domeniko-gentner/61d1812014ac4ee34db4d767c5d9f9a8 to your computer and use it in GitHub Desktop.
HTML color to std::byte
#include <iostream>
#include <cstddef>
#include <algorithm>
#include <string>
#include <exception>
struct Color{
Color(const std::string& html_col)
{
if (html_col.length() < 9){
throw std::exception("String too short");
}
r = std::byte(std::stoi(html_col.substr(1,2), nullptr, 16));
g = std::byte(std::stoi(html_col.substr(3,2), nullptr, 16));
b = std::byte(std::stoi(html_col.substr(5,2), nullptr, 16));
a = std::byte(std::stoi(html_col.substr(7,2), nullptr, 16));
}
std::byte r;
std::byte g;
std::byte b;
std::byte a;
};
int main(int argc, char** argv)
{
Color c(argv[1]);
std::cout << "Color is RGB("<< std::to_integer<int>(c.r) << ", "
<< std::to_integer<int>(c.g) << ", "
<< std::to_integer<int>(c.b) << ", "
<< std::to_integer<int>(c.a) << ")"
<< std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment