index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use strict';
  2. /**
  3. * Kuler: Color text using CSS colors
  4. *
  5. * @constructor
  6. * @param {String} text The text that needs to be styled
  7. * @param {String} color Optional color for alternate API.
  8. * @api public
  9. */
  10. function Kuler(text, color) {
  11. if (color) return (new Kuler(text)).style(color);
  12. if (!(this instanceof Kuler)) return new Kuler(text);
  13. this.text = text;
  14. }
  15. /**
  16. * ANSI color codes.
  17. *
  18. * @type {String}
  19. * @private
  20. */
  21. Kuler.prototype.prefix = '\x1b[';
  22. Kuler.prototype.suffix = 'm';
  23. /**
  24. * Parse a hex color string and parse it to it's RGB equiv.
  25. *
  26. * @param {String} color
  27. * @returns {Array}
  28. * @api private
  29. */
  30. Kuler.prototype.hex = function hex(color) {
  31. color = color[0] === '#' ? color.substring(1) : color;
  32. //
  33. // Pre-parse for shorthand hex colors.
  34. //
  35. if (color.length === 3) {
  36. color = color.split('');
  37. color[5] = color[2]; // F60##0
  38. color[4] = color[2]; // F60#00
  39. color[3] = color[1]; // F60600
  40. color[2] = color[1]; // F66600
  41. color[1] = color[0]; // FF6600
  42. color = color.join('');
  43. }
  44. var r = color.substring(0, 2)
  45. , g = color.substring(2, 4)
  46. , b = color.substring(4, 6);
  47. return [ parseInt(r, 16), parseInt(g, 16), parseInt(b, 16) ];
  48. };
  49. /**
  50. * Transform a 255 RGB value to an RGV code.
  51. *
  52. * @param {Number} r Red color channel.
  53. * @param {Number} g Green color channel.
  54. * @param {Number} b Blue color channel.
  55. * @returns {String}
  56. * @api public
  57. */
  58. Kuler.prototype.rgb = function rgb(r, g, b) {
  59. var red = r / 255 * 5
  60. , green = g / 255 * 5
  61. , blue = b / 255 * 5;
  62. return this.ansi(red, green, blue);
  63. };
  64. /**
  65. * Turns RGB 0-5 values into a single ANSI code.
  66. *
  67. * @param {Number} r Red color channel.
  68. * @param {Number} g Green color channel.
  69. * @param {Number} b Blue color channel.
  70. * @returns {String}
  71. * @api public
  72. */
  73. Kuler.prototype.ansi = function ansi(r, g, b) {
  74. var red = Math.round(r)
  75. , green = Math.round(g)
  76. , blue = Math.round(b);
  77. return 16 + (red * 36) + (green * 6) + blue;
  78. };
  79. /**
  80. * Marks an end of color sequence.
  81. *
  82. * @returns {String} Reset sequence.
  83. * @api public
  84. */
  85. Kuler.prototype.reset = function reset() {
  86. return this.prefix +'39;49'+ this.suffix;
  87. };
  88. /**
  89. * Colour the terminal using CSS.
  90. *
  91. * @param {String} color The HEX color code.
  92. * @returns {String} the escape code.
  93. * @api public
  94. */
  95. Kuler.prototype.style = function style(color) {
  96. return this.prefix +'38;5;'+ this.rgb.apply(this, this.hex(color)) + this.suffix + this.text + this.reset();
  97. };
  98. //
  99. // Expose the actual interface.
  100. //
  101. module.exports = Kuler;