A handy function for converting RGB values to their hex equivalents. Note: only intended for 0-255. Will not work with large numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function base10_to_16(num)
{
  var conversion = new Object({"10":"a", "11":"b", "12":"c", "13":"d", "14":"e", "15":"f"});
  var return_val;

  if (num < 16)
    return_val = (conversion[num] != undefined) ? conversion[num] : num;
  else
  {
    var mod = num % 16;
    var new_num = (num - mod) / 16;
    mod = (conversion[mod] != undefined) ? conversion[mod] : mod;
    new_num = (conversion[new_num] != undefined) ? conversion[new_num] : new_num;
 
    return_val = "" + new_num + mod;
  }

  return return_val;
}