Skip to content

Instantly share code, notes, and snippets.

@phlipper
Created December 30, 2009 01:21
Show Gist options
  • Save phlipper/265778 to your computer and use it in GitHub Desktop.
Save phlipper/265778 to your computer and use it in GitHub Desktop.
function rgbToHex(r, g, b) {
return toHex(r) + toHex(g) + toHex(b);
}
function toHex(n) {
if (n == null) { return '00'; }
n = parseInt(n);
if (n == 0 || isNaN(n)) { return '00'; }
n = Math.max(0, n); n = Math.min(n, 255); n = Math.round(n);
chars = '0123456789ABCDEF';
return chars.charAt((n - n % 16) / 16) + chars.charAt(n % 16);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment