123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 'use strict';
- var colornames = require('colornames');
- function Kuler(text, color) {
- if (color) return (new Kuler(text)).style(color);
- if (!(this instanceof Kuler)) return new Kuler(text);
- this.text = text;
- }
- Kuler.prototype.prefix = '\x1b[';
- Kuler.prototype.suffix = 'm';
- Kuler.prototype.hex = function hex(color) {
- color = color[0] === '#' ? color.substring(1) : color;
-
-
-
- if (color.length === 3) {
- color = color.split('');
- color[5] = color[2];
- color[4] = color[2];
- color[3] = color[1];
- color[2] = color[1];
- color[1] = color[0];
- color = color.join('');
- }
- var r = color.substring(0, 2)
- , g = color.substring(2, 4)
- , b = color.substring(4, 6);
- return [ parseInt(r, 16), parseInt(g, 16), parseInt(b, 16) ];
- };
- Kuler.prototype.rgb = function rgb(r, g, b) {
- var red = r / 255 * 5
- , green = g / 255 * 5
- , blue = b / 255 * 5;
- return this.ansi(red, green, blue);
- };
- Kuler.prototype.ansi = function ansi(r, g, b) {
- var red = Math.round(r)
- , green = Math.round(g)
- , blue = Math.round(b);
- return 16 + (red * 36) + (green * 6) + blue;
- };
- Kuler.prototype.reset = function reset() {
- return this.prefix +'39;49'+ this.suffix;
- };
- Kuler.prototype.style = function style(color) {
-
-
-
-
-
- if (!/^#?(?:[0-9a-fA-F]{3}){1,2}$/.test(color)) {
- color = colornames(color);
- }
- return this.prefix +'38;5;'+ this.rgb.apply(this, this.hex(color)) + this.suffix + this.text + this.reset();
- };
- module.exports = Kuler;
|