-
Notifications
You must be signed in to change notification settings - Fork 1
/
rgbToHex.js
42 lines (37 loc) · 1.14 KB
/
rgbToHex.js
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
var convertNumberToHex = function( num ) {
var hexDigits = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];
var current = ['0','0'];
var hexCounter = 1;
var placeValue = 1;
for ( var i = 1; i <= num; i++ ) {
// if number is not at F
if( hexCounter !== 16 ) {
// replace it with the next highest
current.pop();
current.push( hexDigits[hexCounter] );
hexCounter++;
// if number is maxed out at F
} else {
// if it's not time to increase place values
if( current[placeValue - 1] !== 'F' ) {
hexCounter = 0;
// increase the value of the digit previous to the PV we're changing
current[placeValue - 1] = hexDigits[ hexDigits.indexOf( current[placeValue - 1] ) + 1 ];
current.pop();
current.push( hexDigits[hexCounter] );
hexCounter++;
} else {
// Highest colors go is FF
return 'FF';
}
}
};
return current.join('');
};
var rgb = function(n1, n2, n3) {
var output = [];
for (var i = 0; i < arguments.length; i++) {
output.push( convertNumberToHex(arguments[i]) );
};
return output.join('');
};