[Codewars] RGB To Hex Conversion

RGB To Hex Conversion

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class RGBToHex
{
public:
static std::string rgb(int r, int g, int b);
};
std::string hex(int x) {
x = std::clamp(x,0,255);
auto toChar = [](int x) -> char {
return x >= 10 ? 'A' + x - 10 : '0' + x;
};
return std::string{toChar(x / 16), toChar(x % 16)};
}
std::string RGBToHex::rgb(int r, int g, int b) {
return hex(r) + hex(g) + hex(b);
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/05/01/PS/Codewars/rgb-to-hex-conversion/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.