a zip code crypto-currency system good for red ONLY

shorten-hsl.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // HSL to RGB converter. Both methods adapted from:
  2. // http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
  3. function hslToRgb(h, s, l) {
  4. var r, g, b;
  5. // normalize hue orientation b/w 0 and 360 degrees
  6. h = h % 360;
  7. if (h < 0)
  8. h += 360;
  9. h = ~~h / 360;
  10. if (s < 0)
  11. s = 0;
  12. else if (s > 100)
  13. s = 100;
  14. s = ~~s / 100;
  15. if (l < 0)
  16. l = 0;
  17. else if (l > 100)
  18. l = 100;
  19. l = ~~l / 100;
  20. if (s === 0) {
  21. r = g = b = l; // achromatic
  22. } else {
  23. var q = l < 0.5 ?
  24. l * (1 + s) :
  25. l + s - l * s;
  26. var p = 2 * l - q;
  27. r = hueToRgb(p, q, h + 1/3);
  28. g = hueToRgb(p, q, h);
  29. b = hueToRgb(p, q, h - 1/3);
  30. }
  31. return [~~(r * 255), ~~(g * 255), ~~(b * 255)];
  32. }
  33. function hueToRgb(p, q, t) {
  34. if (t < 0) t += 1;
  35. if (t > 1) t -= 1;
  36. if (t < 1/6) return p + (q - p) * 6 * t;
  37. if (t < 1/2) return q;
  38. if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
  39. return p;
  40. }
  41. function shortenHsl(hue, saturation, lightness) {
  42. var asRgb = hslToRgb(hue, saturation, lightness);
  43. var redAsHex = asRgb[0].toString(16);
  44. var greenAsHex = asRgb[1].toString(16);
  45. var blueAsHex = asRgb[2].toString(16);
  46. return '#' +
  47. ((redAsHex.length == 1 ? '0' : '') + redAsHex) +
  48. ((greenAsHex.length == 1 ? '0' : '') + greenAsHex) +
  49. ((blueAsHex.length == 1 ? '0' : '') + blueAsHex);
  50. }
  51. module.exports = shortenHsl;