Skip to content

Commit 4b47e32

Browse files
committed
RGB To Hex Conversion
1 parent e8e56d2 commit 4b47e32

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ My Codewars solutions in various languages.
2121
- [Not so black box (8 kyu)](https://www.codewars.com/kata/not-so-black-box) [[solution](https://github.com/PaulNoth/codewars/blob/master/not-so-black-box/solution.md)] [[javascript](https://github.com/PaulNoth/codewars/blob/master/not-so-black-box/kata.js)]
2222
- [Arguments to Binary addition (8 kyu)](https://www.codewars.com/kata/arguments-to-binary-addition) [[solution](https://github.com/PaulNoth/codewars/blob/master/arguments-to-binary-addition/solution.md)] [[javascript](https://github.com/PaulNoth/codewars/blob/master/arguments-to-binary-addition/kata.js)]
2323
- [Mr. Freeze (8 kyu)](https://www.codewars.com/kata/mr-freeze) [[solution](https://github.com/PaulNoth/codewars/blob/master/mr-freeze/solution.md)] [[javascript](https://github.com/PaulNoth/codewars/blob/master/mr-freeze/kata.js)]
24+
- [RGB To Hex Conversion (5 kyu)](https://www.codewars.com/kata/rgb-to-hex-conversion) [[solution](https://github.com/PaulNoth/codewars/blob/master/rgb-to-hex-conversion/solution.md)] [[javascript](https://github.com/PaulNoth/codewars/blob/master/rgb-to-hex-conversion/kata.js)]

rgb-to-hex-conversion/kata.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function rgb(r, g, b) {
2+
const convert = function(val) {
3+
if(val < 0) {
4+
return '00';
5+
}
6+
if(val > 255) {
7+
return 'FF';
8+
}
9+
return (val > 15 ? val.toString(16) : '0' + val.toString(16)).toUpperCase();
10+
}
11+
return convert(r) + convert(g) + convert(b);
12+
}

rgb-to-hex-conversion/solution.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### RGB To Hex Conversion
2+
3+
Used built-in conversion function. Only need to take care when the color value is out of bounds `0` and `255`.

0 commit comments

Comments
 (0)