index.js 2.8 KB

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