Skip to content

Commit c1be1c3

Browse files
authored
Create RGB To Hex Conversion.cpp
1 parent d528e44 commit c1be1c3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

5-kyu/RGB To Hex Conversion.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using namespace std;
2+
3+
class RGBToHex
4+
{
5+
public:
6+
static std::string rgb(int r, int g, int b);
7+
};
8+
9+
string toHexadecimal(int n)
10+
{
11+
char d[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
12+
string s = "";
13+
if ( n < 0 ) s += "00";
14+
else if ( n < 16 ) s += '0';
15+
else if ( n < 256 ) s += d[n/16];
16+
else s += "FF";
17+
if ( n >= 0 && n < 256 ) s += d[n%16];
18+
return s;
19+
}
20+
21+
string RGBToHex::rgb(int r, int g, int b)
22+
{
23+
return ( toHexadecimal(r) + toHexadecimal(g) + toHexadecimal(b));
24+
}

0 commit comments

Comments
 (0)