-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhex2gba.cpp
74 lines (68 loc) · 2.71 KB
/
hex2gba.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Convert a RGB888 color value to RGB555 for GBA
// See also: http://www.budmelvin.com/dev/15bitconverter.html
// and: https://en.wikipedia.org/wiki/High_color
#include <iomanip>
#include <iostream>
#include <string>
#include <Magick++.h>
#include <cxxopts/include/cxxopts.hpp>
Magick::Color m_color;
bool readArguments(int argc, const char *argv[])
{
cxxopts::Options options("hextogba", "Convert a RGB888 color value to RGB555 and BGR555 for GBA");
options.allow_unrecognised_options();
options.add_options()("h,help", "Print help")("c,color", "Color must be a RGB888 hex value like \"abc012\" or \"#abc012\"", cxxopts::value<std::string>())("positional", "", cxxopts::value<std::vector<std::string>>());
options.parse_positional({"color", "positional"});
auto result = options.parse(argc, argv);
// check if help was requested
if (result.count("h"))
{
return false;
}
// color value
if (result.count("color"))
{
// try converting argument to a color
auto colorArg = result["color"].as<std::string>();
auto colorString = colorArg.front() == '#' ? colorArg : (std::string("#") + colorArg);
try
{
m_color = Magick::Color(colorString);
}
catch (const Magick::Exception &ex)
{
std::cerr << colorArg << " is not a valid color. Format must be \"abc012\" or \"#abc012\". Aborting. " << std::endl;
return false;
}
}
return true;
}
void printUsage()
{
std::cout << "Convert a RGB888 color value to RGB555 and BGR555 for GBA" << std::endl;
std::cout << "Usage: hex2gba COLOR" << std::endl;
std::cout << "COLOR must be a RGB888 hex value like \"abc012\" or \"#abc012\"" << std::endl;
}
int main(int argc, const char *argv[])
{
// check arguments
if (argc < 2 || !readArguments(argc, argv))
{
printUsage();
return 2;
}
// convert color
auto colorRGB = Magick::ColorRGB(m_color);
auto b = static_cast<uint16_t>(std::round(31.0 * colorRGB.blue()));
auto g = static_cast<uint16_t>(std::round(31.0 * colorRGB.green()));
auto r = static_cast<uint16_t>(std::round(31.0 * colorRGB.red()));
uint16_t rgb = (r << 10 | g << 5 | b);
std::cout << "RGB555 = #" << std::hex << std::setfill('0') << std::setw(4) << rgb << ", ";
std::cout << std::hex << std::setfill('0') << std::setw(4) << rgb << "h, ";
std::cout << std::dec << rgb << "d" << std::endl;
uint16_t bgr = (b << 10 | g << 5 | r);
std::cout << "BGR555 = #" << std::hex << std::setfill('0') << std::setw(4) << bgr << ", ";
std::cout << std::hex << std::setfill('0') << std::setw(4) << bgr << "h, ";
std::cout << std::dec << bgr << "d" << std::endl;
return 0;
}